Use Maven to manage Eclipse Java projects (transfer)

This article can be reproduced at will, but please keep the original source:
http://www.webwork.cn/archives/50009640.html

Why use Maven and what are the benefits of using Maven 
Maven is a tool that can help you with project compilation, document management, report management, package dependency management, SCMs management, publishing, etc. It can assist you in completing the following tasks:
1. Quickly create A project file
2. Almost without any settings, you can compile, test, package and other work immediately. When using Ant in the past, it was very convenient to do these tasks, but writing the Ant build.xml file was a very cumbersome thing.
3. It is very easy to create project documents, you can automatically generate a web site to record your project situation, version information, change list between two versions, etc.
4. Very convenient remote release, scm (cvs) management functions.
5. Dependency management function. This feature is my favorite feature. When we are working on multiple different Java projects, the library files used in each project are basically the same. The usual practice is to copy these library files under each project, and even submit these library files to cvs up, the problem that this brings not only increases the hard disk space occupied by the project, but also makes the package management very confusing. When I need to work on another machine, I usually need to rebuild my project files. The form used by maven is similar to the package dependency management mode of Linux/Unix such as yum, apt, ports, etc. You only need to specify what kind of package you depend on in the configuration file. When compiling with maven, it will first check you Whether the library file exists in the local warehouse, if not, it will be downloaded automatically. By default, it will be downloaded from http://www.ibiblio.org/maven/ . For the company, you can create your own library server.

For getting started with Maven, please refer to the following document
Getting Started with maven:
http://maven.apache.org/guides/getting-started/index.html

Using Maven to manage Eclipse projects
Suppose a mydemo web project, here I divide the project into two modules, mydemo-core and mydemo-web, where the mydemo-core project is the core java program file, and the output is a jar file; mydemo-web is the web part, and it basically has no java program files. Except for some parts that are closely integrated with the web, it mainly uses the java lib generated by mydemo-core. We use maven to create a project to manage the project, and mydemo-core and mydemo-web are also eclipse projects, which can be opened directly in eclipse and program development.

1. Create mydemo project
cd d:\mywork
mvn archetype:create -DgroupId=com.example.mydemo -DartifactId=mydemo

Generate the mydemo directory, delete the src directory under the directory, modify pom.xml, and modify the packaging value to:
<packaging>pom</packaging>

2. Then create two modules, mydemo-core and mydemo-web respectively,
cd mydemo
mvn archetype:create -DgroupId=www.example.mydemo.core -DartifactId=mydemo-core
mvn archetype:create -DgroupId=www.example.mydemo. web -DartifactId=mydemo-web -DarchetypeArtifactId=maven-archetype-webapp

3. 修改d:\myworkpom.xml文件,添加
<modules>
 <module>mydemo-core</module>
 <module>mydemo-web</module>
</modules>

4. Add the following to the pom.xml files in the mydemo-core and mydemo-web directories:

<parent>
 <groupId>cn.webwork.mydemo</groupId>
 <artifactId>mydemo</artifactId>
 <version>1.0-SNAPSHOT</version>
</parent>

5. Then add the information that depends on the mydemo-core package in d:\mywork\mydemo-webpom.xml, as follows:

<dependency>
  <groupId>cn.webwork.mydemo.core</groupId>
  <artifactId>mydemo-core</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

关于pom.xml配置文件的详细情况,请参考:
Project Setting:http://maven.apache.org/maven-model/maven.html
Introduction to POM:http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

6. 创建完成后,就会分别生成两个目录,然后分别对其生成相应的eclipse工程文件,方法为:
cd mydemo-core
mvn eclipse:eclipse

cd ..
cd mydemo-web
mvn eclipse:eclipse -DdownloadSource=true -Dwtpversion=1.0 (指定wtpversion属性,用于生成wtp的工程文件)

创建完成这两个eclipse工程文件后,就可以在eclipse中打开了。

7. 回到 d:mywork目录下,执行mvn compile 就可以对这两个文件进行编译,还有mvn install ,mvn package等命令。

mydemo文件下载:
http://project.livedoor.cn/~roger/mydemo.zip

关于mvn命令:
在上面的过程中,大家看到诸如 mvn archetype:create / mvn eclipse:eclipse等这些命令,如何查询这些命令更多帮助信息?
请参考:http://maven.apache.org/plugins/index.html
其实象archetype:create eclipse:eclipse等都是maven的插件,默认安装的时候,这些插件都已经安装,还有很多第三方的插件,
具体使用方式,都可以从上面的地址中查到。

如,site插件, Generate a site for the current project 
当你执行mvn site的时候,就会自动生成一个web site,用于展示你的项目信息。


以上整理的比较混乱,估计对大家帮助不是很大,但希望能够通过这篇文章,能让大家知道maven是个非常好的工具,如果你想深入了解它,并在自己的项目中很好的使用,请参考下面的参考我文献。
参考:
Maven: http://maven.apache.org
Maven Eclipse Plugin: http://m2eclipse.codehaus.org/
http://maven.apache.org/guides/mini/guide-ide-eclipse.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326709682&siteId=291194637