http://yoancn.blogbus.com/logs/31289454.html
-
项目构建的生命周期的介绍
Maven 2是围绕 着构建生命周期概念设计的。这意味着,构建或者发布的过程已经被清晰的定义了。
当我们使用 Maven构 建工程时,我们只需要了解几个 Maven定义好的命令即可,其他的工作则交给 POM来完成。
以下给出 Maven提 供的构建生命周期列表:
validate | validate the project is correct and all necessary information is available. |
generate-sources | generate any source code for inclusion in compilation. |
process-sources | process the source code, for example to filter any values. |
generate-resources | generate resources for inclusion in the package. |
process-resources | copy and process the resources into the destination directory, ready for packaging. |
compile | compile the source code of the project. |
process-classes | post-process the generated files from compilation, for example to do bytecode enhancement on Java classes. |
generate-test-sources | generate any test source code for inclusion in compilation. |
process-test-sources | process the test source code, for example to filter any values. |
generate-test-resources | create resources for testing. |
process-test-resources | copy and process the resources into the test destination directory. |
test-compile | compile the test source code into the test destination directory |
test | run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed. |
package | take the compiled code and package it in its distributable format, such as a JAR. |
pre-integration-test | perform actions required before integration tests are executed. This may involve things such as setting up the required environment. |
integration-test | process and deploy the package if necessary into an environment where integration tests can be run. |
post-integration-test | perform actions required after integration tests have been executed. This may including cleaning up the environment. |
verify | run any checks to verify the package is valid and meets quality criteria. |
install | install the package into the local repository, for use as a dependency in other projects locally. |
deploy | done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects. |
因此,当我们构建一个项目时,只需要了解自己希望做什么,然后执行以上对应的生命周期即 可。
例如,我们希望编译我们的工程。在命令行状态下进入到工程的 pom.xml文件所在的目录中,使用命令 :mvn compile;希望构建打包我们的工程,使用 mvn package即 可。
当然了, maven的 构建生命周期也是可以扩展和自定义的,这里就先不做介绍了。
-
pom.xml 的 介绍
pom.xml包含了 一个项目的项目对象模型( POM)。项目对象模型( POM)是 Maven工作的基本单元。请记住,这个是非 常重要的,因为 POM包含了工程的非常重要的信息块,并且基本上包含了和项目相关的任何 要素。
让我们熟悉一下 pom.xml包含的基本项:
-
poject 这是 pom.xml的 顶级元素。
-
modelVersion 这是元素指出了这个 POM使 用的是那个版本的对象模型。这个模型的版本自身么是经常改变的,但这种改变是为了使模型更加的稳定。
-
groupId 这个元素指出创建这个工程的组 织或团队的唯一标识,并且这个也是一个项目的关键标识,推荐使用这个组织或团队的完整域名。例如: org.apache.maven.plugins是 为 Maven plug-ins定义的 groupId。
-
artifactId 这个元素指出这个工程的 主要制品的基本名称。一个工程的主要制品如果是 jar文件,次要制品如果是源码包,则次要制品的名称的一部分也使用 artifactId。 典型的制品名称使用这样的格式: <artifactId>-<version>.<extension>(例 如, myapp-1.0.jar)。
-
packaging 这个元素指出制品的类型 (例如: JAR, WAR, EAR等 等)。这个元素不仅仅指示出制品的类型,同时也指示出工程构建过程中的部分生命周期。 Packaging的默认值是 JAR。
-
version 这个元素指出这个项目产生的制 品的版本号, Maven在 帮助开发人员管理版本号时走了很长的路,以后你将经常看到 SNAPSHOT在一个版本中,这个表示一个工程仍然在开发状态。
-
name 这个元素指出这个工程显示的名称。这 个常用于 Maven产 生的文档中。
-
url 这个员算指出在哪里能发现工程的站点。 这个常用于 Maven产 生的文档中。
-
desription 这个元素提供了这个工程 基本的描述。这个也常用于 Maven产 生的文档中。
以上只是 pom.xml 中的一些基本项,完整的 pom.xml 的元素介绍请参考:
http://maven.apache.org/maven-model/maven.html
()
置管理
Maven的 配置管理是作为一个单独的 Maven子项目在做。叫做 SCM。他是这样介绍的:
MavenSCM支 持 Maven 2.x插件(例如, maven-release-plugin)和其他工具(例如, Continuum,这个是一个和 maven 2.x结合很好的一个持续集成工具。), Maven SCM提供给他们一个公共的 API去做配置管理的各种操作。当然了要执行配置管理操作,当然少不了配置滚里工具了。 Maven SCM给出了一个列表,说明 Maven SCM现在所支持的配置管理工具。
完全支持的配置管理工具 | CVS Subversion Perforce StarTeam |
部分支持的配置管理工具 | ClearCase File system Visual Source Safe |
不支持的配置管理工具 | Accurev Aegis Arch BitKeeper ClearCase Multisite CM Synergy Code Co-op Darcs Monotone OpenCM PureCM Serena PVCS / Dimension Starteam Enterprise Svk Vesta |
当你选中了上面的一个配置管理工具,你就可以利用 Maven 2.x的插件或者集成管理工具进行配置管理了。
-
持续集成工具 continuum
continuum是 Maven的一个子项目。他是一个构建基于 java的工程的持续集成服务器。他支持以下多种工程类型:
-
Maven 1
-
Maven 2
-
Ant
-
Shell scripts
continuum有以下特征
-
紧密整合 Maven 2.x
-
紧密整合 Maven SCM
-
Subversion
-
CVS
-
Starteam
-
Clearcase
-
Perforce
-
-
更易用的基于网络的设置和界面
-
基于 Quartz( Quartz-based)的时间计划表设置
-
添加新项目十分方便
-
邮件通知
-
其他 IM通 知
-
IRC
-
Jabber
-
MSN
-
-
责备机制( Blame Mechanism)
-
下载:
在以下连接处下载此软件
http://maven.apache.org/continuum/download.html |
安装:
Windows 2000/XP
解压缩 continuum-1.0.2-bin.zip到 你希望安装 Continuum 1.0.2的所在目录。这里假 定你安装在 C:/Program Files/Apache Software Foundation/continuum-1.0下。
运行:
-
bin/linux/run.sh 如果是 UNIX平台
-
bin/solaris/run.sh 如果是 Solaris平台
-
bin/win32/run.bat 如果是 Windows平台
-
bin/macosx/run.sh 如果是 MacOS X平台 .
-
bin/plexus.sh 如果是其他平台
当服务器启动成功后,打开浏览器访问:
http://localhost:8080/continuum/ |
注意: 由于 continuum判断一个工程是否构建成功,是使用一个命令行返回值。而 windows平台的用户,这个返回值有问题。因此需要修改以下 maven 2的 bin/mvn.bat文件。这里可以直接从以下地址下载修改后的文件替换掉即可。
http://maven.apache.org/continuum/scripts/mvn.bat |
-
添加一个项目到 continuum
要添加一个 maven 2的项目到 continuum,需要写入一个 pom url或者导入一个 pom.xml,当然导入的这个 pom.xml文 件中包含了 continuum需要的各种信息。
我们来看看导入的 pom.xml文件中具体需要包含哪些项:
-
配置管理信息( The SCM information)
<scm> <connection/> <developerConnection/> <tag/> <url/> </scm> |
-
开发者信息( The developers)
<ciManagement> <system>continuum</system> <notifiers> <notifier> ... </notifier> </notifiers> </ciManagement> |
-
集成后所有的通知人信息( The notifiers)
<notifier> <type>mail</type> <configuration> <address>dev@maven.apache.org</address> </configuration> <notifier> |
<notifier> <type>irc</type> <configuration> <host>irc.codehaus.org</host> <port>6667</port> <channel>#maven</channel> </configuration> </notifier> |
-
构建工程
-
按照定制的时间构建
-
点击本项目上的 build按钮进行构建。
- ()
-
依赖管理
这里我们通过使用外部依赖( external dependencies)来大家对 maven的依赖管理有一个简单而具体的了解。
当我们在做一个工程的时候,不可能一切都是 从空白开始,对于我们做 Web应用的来说,使用框架已经司空见惯,而这种对框架的引入使 用对于 Maven来说,就是工程的依赖。而我们的工程要进行测试,则不能少了对 Junit框架的依赖。
依赖管理是 maven的一个主要特征,这个是对于用户来说,是 Maven令 人振奋的一个最著名的特征。对于一个单一的工程来说,依赖管理没有什么困难的,但是当你开始处理多个模块的工程或由 10多个甚至上百个模块组成的应用程序时, Maven能 帮助你轻松稳定的控制这些大量的依赖。
在 pom.xml中 dependencies部分列出了所 有外部依赖,详细描述了在编译时,测试时,运行时是否需要这个依赖。现在,假定我们的工程只有对 Junit的 依赖。它的 pom.xml文件可能如下:
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
从以上 pom.xml文件可以看出,定 义一个外部依赖( external dependencies)至少需要 4个元素: groupId, artifactId, version, and scope。对于 groupId, artifactId, version的 意思,和前面我们在创建工程时提到的这几个元素的意义相同,这里就不再重复介绍, scope元素指出你的工程如何使用依赖,并且它的值有 compile, test和 runtime等 等。想要了解更多的依赖说明的信息,请看
http://maven.apache.org/maven-model/maven.html
要想了解完整的依赖机制,请看
http://maven.apache.org/guides/introduction/introduction-to-dependency-management.html
有了这些依赖信息, Maven将能在工程构建时引用依赖。
引用的过程是:
首先,在本地仓库(默认的本地仓库地址为: ~/.m2/repository) 中查找此依赖是否存在。
再次,如果在本地仓库中未发现,则在远程仓 库中下载此依赖,并下载到本地仓库中。
最后,通过以上两个步骤就能找到依赖。如果 远程仓库无法访问,则可以设置其他远程仓库。具体请看
http://maven.apache.org/guides/introduction/introduction-to-repositories.html
一个简单的例子。比如我们要添加一个 log4j到我们的工程中。
首先.需要了解 log4j的 groupId, artifactId, and version信息。可在 google上搜索“ site:www.ibiblio.org maven2 log4j”。这样在搜索结果里可以找到 /maven2/log4j/log4j (or /pub/packages/maven2/log4j/log4j)这样的目录,在这个目录中有一个文件叫做 maven-metadata.xml。 这个文件内容如下所示:
-
<metadata>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.1.3</version>
<versioning>
<versions>
<version>1.1.3</version>
<version>1.2.4</version>
<version>1.2.5</version>
<version>1.2.6</version>
<version>1.2.7</version>
<version>1.2.8</version>
<version>1.2.11</version>
<version>1.2.9</version>
<version>1.2.12</version>
</versions>
</versioning>
</metadata>
这样我们能找到 groupId为 log4j, artifactId为 log4j, version当然要用最新的,选择 1.2.12。 scope我们设置为 compile。
这样我们使用 mvn compile 编译工程时,会看到 mvn下 载了 log4j到我们的本地仓库。
-
-
发布管理 Releases
-
发布配置方法
要发布( Releases) 一个项目,需要在 pom.xml和 setting.xml中分别配置。
在 pom.xml中
<distributionManagement> <repository> <id>mycompany-repository</id> <name>MyCompany Repository</name> <url>scp://repository.mycompany.com/repository/maven2</url> </repository> </distributionManagement> |
在 setting.xml中
<settings> . . <servers> <server> <id>mycompany-repository</id> <username>jvanzyl</username> <!-- Default value is ~/.ssh/id_dsa --> <privateKey>/path/to/identity</privateKey> (default is ~/.ssh/id_dsa) <passphrase>my_key_passphrase</passphrase> </server> </servers> . . </settings> |
通过以上的设置,项目将可以发布到远程仓库,远程服务器。
项目发布现在支持 scp, ftp和 scpexe等方式。
-
Setting.xml 文 件
这个文件是一个 maven的配置文件。
他有下面的功能:
-
配置本地仓库
-
配置访问网络的代理服务器
-
安全和部署的设置。
-
为仓库设置景象
想要了解更多这个文件的信息可以参考一下以下文章:
http://maven.apache.org/guides/mini/guide-configuring-maven.html |
-
运行发布的命令
mvn deploy |
Maven 和 eclipse 工具的合作
当有了以上的工程目录,无论是 webapp工程,还是 app,我 们都希望在集成开发环境中开发。 Maven如何和 IDE集成。现在我们就来进行讲解。
-
首先要让 eclipse 知 道 Maven 2 的 repository (仓 库)的路径。
因此要设置一个 M2_REPO的变量为 classpath。
可以使用命令行的方式来设置:
-
mvn -Declipse.workspace=<path-to-eclipse-workspace> eclipse:add-maven-repo
还可以在 eclipse中 定义一个新的 classpath变量通过在 eclipse的菜单上选择 Window> Preferences. 选择 Java > Build Path > Classpath Variables page.
-
生成 eclipse 的工程文件。
使用命令行
-
mvn eclipse:eclipse
这样通过在 eclipse的 菜单上选择 File >Import >Existing Projects into Workspace, 就可以将工程导入 eclipse中。
通过以上 2个步骤,我们就可以利用 eclipse来进行开发工作了。
-
Eclipse 插 件 mavenide
同时在 eclipse中我们可以下载一个 maven的插件,用来辅助 eclipse进 行 maven工程的开发工作。这个插件名称叫做 mavenide。
这个插件可使用以下更新站点: http://m2eclipse.codehaus.org/
并且非常方便的是,这里还提供了一个 flash的演示来介绍如何安装此插件,这个 flash演示的观看地址在:
http://m2eclipse.codehaus.org/Installing_Maven_2.0_plugin_for_Eclipse.html
要正常使用此插件请注意以下问题:
eclipse使用 3.1或以上版本。
Mavenide的插件为 0.0.5不要安装错误了。
通过笔者在安装好此插件的一些使用后,看到 了此插件的以下一些特性。
-
可以方便的添加依赖。
-
在编辑完 pom.xml文 档后,会自动检查此文档的格式是否正确。
-
可以将 mvn的 一些命令通过扩展工具的方式在 eclipse中轻松执行。
-
文档编制
-
文档创建:
对于如何创建和编制文档, maven有一个简单的示例命令:
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-site |
执行了以上命令后,我们将得到这样一个目录结构:
my-app |-- pom.xml `-- src |-- site |-- apt | |-- format.apt | `-- index.apt |-- fml | `-- faq.fml |-- fr | |-- apt | | |-- format.apt | | `-- index.apt | |-- fml | | `-- faq.fml | `-- xdoc | `-- xdoc.xml |-- site.xml |-- site_fr.xml `-- xdoc `-- xdoc.xml |
你现在可以看到一个 $basedir/src/site 目录,以及它包含的一些站点描述文件( site.xml,site_fr_xml ),和各种 maven 支持的文档格式相对应的目 录和示例文档都已经产生。
以上的创建只是示例,我们自己创建时就没有命令行使用 了,只能按照上面的目录结构创建我们需要的文档,并在文档中写入我们工程的信息。
让我们再来看看 maven 所支持的文 档格式。
-
文档格式:
Maven 支持 3 种文档格式:
-
Xdoc format
这 个是一种简单快捷的,基于 original Anakia format的文件格式。
-
APT format
“ Almost Plain Text”, (接近普通文本格式),这是一种允许你采用接近普通文本格式的方式简单的写出类似于 wiki 格式的结构性文档。
如果你对此很感兴趣,请参考完整的 APT format 的 书写规范
http://maven.apache.org/guides/mini/guide-apt-format.html
-
FML formate
这个是一种 FAQ结 构形式的文档格式。
了解了以上的文档格式,我们就可以按照以上文档格式的要求,选用我们喜欢的文档格式编写 我们的文档。当编写完成后,我们需要生成所有文档。这里生成文档, maven的 处理是生成站点( site),也就是身成 html页面,这样所有对此项目感兴趣的人员都可以通过访问此站点来了解所 有的信息。生成站点的命令是:
mvn site |
-
文档国际化:
当然,你可能早就想到文档国际化的问题,这里 maven也早就处理了。在 pom.xml中
<project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-site-plugin</artifactId> <configuration> <locales>en,fr</locales> </configuration> </plugin> </plugins> ... |
注意到 <locales>en,fr</locales> 了吗?这里就支持了英语( en ) 和法语( fr )两种语言的文档。请注意以下生成 的目录,由于英语是在第一个,属于默认语言,所以 $basedir/src/site 目录下并没有 en 的 文件夹,而有 fr 的文件夹,而且这个文件夹里包含了 maven 支持的文档格式相对应的目录和示例文档。
(0
报告设置
Maven有 多个报告能添加在你的文档站点中,来显示项目当前的状态,这些报告采用插件的形式可在项目中配置。
为了为你的文档站点增加这些报告,必须增加 reporting部分在 pom.xml中, 下面这个为标准的项目信息报告插件在 pom.xml中的配置。
<project> ... <reporting> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-project-info-reports-plugin</artifactId> </plugin> </plugins> </reporting> ... |
Maven在 执行 mvn site命令时,除了产生开发人员编制的文档信息 外,根据 pom.xml中设置的报告,会同时生成这个项目信息 的报告文档。并且这个是默认的生成项。
这个默认的生成项将根据项目的信息生成以下报告:
-
持续集成信息( Continuous Integration)
-
依赖性信息( Dependencies)
-
发布追踪信息( Issue Tracking)
-
邮件列表信息( Mailing Lists)
-
工程协议信息( Project License)
-
项目团队信息( Project Team)
-
源码库信息( Source Repository)
根据笔者测试,以上信息均是在 pom.xml进行设置的。
-
持续集成信息根据以下配置信息生成:
<ciManagement>
<system/>
<url/>
<notifiers>
<notifier>
<type/>
<sendOnError/>
<sendOnFailure/>
<sendOnSuccess/>
<sendOnWarning/>
<address/>
<configuration/>
</notifier>
</notifiers>
</ciManagement>
-
依赖性信息根据以下配置信息有关
<dependencies> <dependency> <groupId/> <artifactId/> <version/> <type/> <classifier/> <scope/> <systemPath/> <exclusions> <exclusion> <artifactId/> <groupId/> </exclusion> </exclusions> <optional/> </dependency> </dependencies> |
-
发布追踪信息
<issueManagement> <system/> <url/> </issueManagement> |
-
邮件列表信息
<mailingLists> <mailingList> <name/> <subscribe/> <unsubscribe/> <post/> <archive/> <otherArchives/> </mailingList> </mailingLists> |
笔者在 pom.xml中 设置以上信息后,运行 mvn site总会报错。如果哪位了解 到报错原因请告诉我,谢谢。 zhangxl@sintal.cn
-
工程协议信息
<licenses> <license> <name/> <url/> <distribution/> <comments/> </license> </licenses> |
-
项目团队信息
<organization> <name/> <url/> </organization> |
<developers> <developer> <id/> <name/> <email/> <url/> <organization/> <organizationUrl/> <roles/> <timezone/> <properties/> </developer> </developers>
|
<contributors> <contributor> <name/> <email/> <url/> <organization/> <organizationUrl/> <roles/> <timezone/> <properties/> </contributor> </contributors> |
-
源码库信息
<scm> <connection/> <developerConnection/> <tag/> <url/> </scm> |
默认情况下这些文档将生成在 $basedir/src/site 目录下。
需要得到其他的报告,则需要配置其他的报告插件。
要想了解更多的信息,请参考以下网页:
http://cvs.peopleware.be/training/maven/maven2/morePlugins.html