为什么要使用Maven,使用Maven有什么好处
maven是一个可以帮助你进行项目编译、文档管理、报告管理、包依赖管理、SCMs管理、发布等的工具,它可以协助你完成以下工 作:
1.快速创建一个工程文件
2.几乎不用做任何设置,可以立即进行编译、测试、打包等工作。以前用Ant的时候,也可以很方便的做这些 工作,但是写Ant的build.xml文件是一件很烦琐的事情。
3.创建项目文档非常容易,可以自动生成一个web site,记录你的项目情况、版本信息、两个版本之间的change list等。
4.非常方便的远程发布、scm(cvs)管理功能。
5. 依赖管理功能。这个功能是我非常喜欢的功能。我们在做多个不同的Java项目时,每个项目所用到的库文件基本都是大同小异的,通常的做法是每个项目下面都 会复制一份这些库文件,甚至会把这些库文件都提交到cvs上去,这样带来的问题不仅是增大了项目占用的硬盘空间,而且也使包管理很混乱,当我需要在另外的 机器上进行工作时,我通常需要重新建立我的工程文件。maven所采用的形式,类似于yum、apt、ports等Linux/Unix的包依赖管理模 式,你只需要在配置文件中指明你依赖什么样的包,在用maven编译的时候,它首先会检查你的本地仓库中是否存在该库文件,如果没有,它就会自动去下载, 默认从http://www.ibiblio.org/maven/ 上下载,对于公司内部,可以创建 一个自己的库服务器,
Maven使用入门,请参考下面的文档
Getting Started with maven:
http://maven.apache.org/guides/getting-started/index.html
使用Maven管理Eclipse项目
假设一个mydemo的web项目,在这里我将该项目分为 两个模块,分别为mydemo-core和mydemo-web,其中mydemo-core项目为核心java程序文件,输出为jar文 件;mydemo-web为web部分,自身基本没有java程序文件,除了一部分与web集成较为紧密的,它主要使用mydemo-core生成的 java lib。我们用maven创建一个project,来管理该项目,而mydemo-core和mydemo-web同时又是eclipse project,可以直接在eclipse中打开,并进行程序开发。
1.创建mydemo项目
cd d:/mywork
mvn archetype:create -DgroupId=com.example.mydemo -DartifactId=mydemo
生成mydemo目录,删除该目录下src目录,修改pom.xml,将packaging值修改为 :
<packaging>pom</packaging>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
将groupId 值修改为:
<groupId>cn.webwork.mydemo</groupId> ,原因见第六步后的说明。
2. 然后分别创建mydemo-core、mydemo-web两个模块
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>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> 让父工程 mydemo 包含子模块 mydemo-core 和 mydemo-web
4. 分别在mydemo-core和mydemo-web目录下的pom.xml文件中添加:
<parent>
<groupId>cn.webwork.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
为子模块指定父模块
5. 然后在d:/mywork/mydemo-webpom.xml中添加依赖 mydemo-core包的信息,如下:
<dependency>
<groupId>cn.webwork.mydemo.core</groupId>
<artifactId>mydemo-core</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} --> 指明依赖关系: mydemo-web 模块依赖 mydemo-core 模块
关于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
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;} p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-paper-source:0;} div.Section1 {page:Section1;} -->
说明 :
该命令将无法执行成功,因为第一步操作中-DgroupId=com.example.mydemo ,所以在d:/mydemo/pom.xml 中对应<groupId>com.example.mydemo</groupId> ,而第四步中
<parent>
<groupId>cn.webwork.mydemo</groupId>
<artifactId>mydemo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
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
转自:http://www.blogjava.net/lostfire/articles/58428.html