maven的生命周期是根据我们项目中常见的流程来定义的:清理、编译、测试、打包、集成测试、验证、部署等功能。maven的每个生命周期对应不同的阶段,每个阶段都对应不同的插件。
maven定义了三套生命周期:clean、default、site。每个生命周期都包含了一些阶段(phase),三套生命周期相互独立,但各个生命周期中的phase却是有顺序的,且后面的phase依赖于前面的phase。执行某个phase时,其前面的phase会依顺序执行,但不会触发另外两套生命周期中的任何phase。
每个阶段会包含一个或多个目标。Maven 插件提供目标:一个插件可以有一个或多个目标
1 clean生命周期:
pre-clean :执行清理前的工作;
clean :清理上一次构建生成的所有文件;
post-clean :执行清理后的工作
使用 maven-clean-plugin插件,下面日志是执行mvn clean后的结果,分别对应clean的三个生命周期
[INFO] -----------------------------------------------------------------------
pre-clean:执行清理前的工作
mvn clean
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for
com.common.item.base:common-base:jar:0.0.1
[INFO] ------------------------------------------------------------------------
clean:清理上一次构建生成的所有文件
[INFO] Reactor Build Order:
[INFO]
[INFO] common-pom
[INFO] common-root
[INFO] common-base
[INFO] common-util
[INFO] common-deploy
[INFO]
[INFO] ------------------------------------------------------------------------
post-clean:执行清理后的工作
[INFO] Building common-pom 0.0.1
[INFO] ------------------------------------------------------------------------
2 default生命周期:default生命周期是最核心的,它包含了构建项目时真正需要执行的所有步骤。
mvn help:describe -Dcmd=compile
1 [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ common-util ---
2 [INFO] 'compile' is a phase corresponding to this plugin:
3 org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
5 It is a part of the lifecycle for the POM packaging 'jar'. This lifecycle includ
6 es the following phases:
7 * validate: Not defined
8 * initialize: Not defined
9 * generate-sources: Not defined
10 * process-sources: Not defined
11 * generate-resources: Not defined
12 * process-resources: org.apache.maven.plugins:maven-resources-plugin:2.4.3:resources
14 * compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile
15 * process-classes: Not defined
16 * generate-test-sources: Not defined
17 * process-test-sources: Not defined
18 * generate-test-resources: Not defined
19 * process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.4.3:testResources
21 * test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
22
23 * process-test-classes: Not defined
24 * test: org.apache.maven.plugins:maven-surefire-plugin:2.7.2:test
25 * prepare-package: Not defined
26 * package: org.apache.maven.plugins:maven-jar-plugin:2.3.1:jar
27 * pre-integration-test: Not defined
28 * integration-test: Not defined
29 * post-integration-test: Not defined
30 * verify: Not defined
31 * install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
32 * deploy: org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy
- validate:验证项目是否正确,以及所有为了完整构建必要的信息是否可用
执行mvn validate命令,验证生成项目所依赖的pom,root信息
1 [INFO] -----------------------------------------
2 mvn validate
3 [INFO] Scanning for projects...
4 [WARNING]
5 [WARNING] Some problems were encountered while building the effective model for
6 com.common.item.base:common-base:jar:0.0.1
7 [INFO] ------------------------------------------------------------------------
8 [INFO] Reactor Build Order:
9 [INFO]
10 [INFO] common-pom
11 [INFO] common-root
12 [INFO] common-base
13 [INFO] common-util
14 [INFO] common-deploy
15 [INFO]
16 [INFO] ------------------------------------------------------------------------
17 [INFO] Building common-pom 0.0.1
18 [INFO] ------------------------------------------------------------------------
19 [INFO] ------------------------------------------------------------------------
20 [INFO] Building common-deploy 0.0.1
21 [INFO] ------------------------------------------------------------------------
22 [INFO] ------------------------------------------------------------------------
23 [INFO] Reactor Summary:
24 [INFO]
25 [INFO] common-pom ........................................ SUCCESS [0.415s]
26 [INFO] common-root ....................................... SUCCESS [0.185s]
27 [INFO] common-base ....................................... SUCCESS [0.173s]
28 [INFO] common-util ....................................... SUCCESS [0.182s]
29 [INFO] common-deploy ..................................... SUCCESS [0.002s]
30 [INFO] ------------------------------------------------------------------------
31 [INFO] BUILD SUCCESS
32 [INFO] ------------------------------------------------------------------------
33 [INFO] Total time: 1.247s
34 [INFO] Finished at: Mon Apr 27 17:32:35 CST 2015
35 [INFO] Final Memory: 3M/59M
- initialize
- generate-sources:生成所有需要包含在编译过程中的源代码
- process-sources:处理源代码,比如过滤一些值
- generate-resources:生成所有需要包含在打包过程中的资源文件,使用插件maven-resources-plugin
- process-resources :复制和处理资源文件到target目录,准备打包;
- compile :编译项目的源代码,使用插件maven-compiler-plugin
- process-classes:后处理编译生成的文件,例如对Java类进行字节码增强(bytecode enhancement)
- generate-test-sources:生成所有包含在测试编译过程中的测试源码
- process-test-sources:处理测试源码,比如过滤一些值
- generate-test-resources:生成测试需要的资源文件
- process-test-resources:复制并处理测试资源文件至测试目标目录
- test-compile :编译测试源代码;
- process-test-classes:编译测试源码至测试目标目录
- test :运行测试代码,使用合适的单元测试框架运行测试。这些测试应该不需要代码被打包或发布
- prepare-package:在真正的打包之前,执行一些准备打包必要的操作。这通常会产生一个包的展开的处理过的版本(将会在Maven 2.1+中实现)
- package:打包成jar或者war或者其他格式的分发包;将编译好的代码打包成可分发的格式,如JAR,WAR,或者EAR
当执行 mvn package 命令,maven会执行它整个生命周期中的六个阶段
process-resources, compile, process-test-resources, test-compile, test and package - pre-integration-test:执行一些在集成测试运行之前需要的动作。如建立集成测试需要的环境
- integration-test:如果有必要的话,处理包并发布至集成测试可以运行的环境
- post-integration-test:执行一些在集成测试运行之后需要的动作。如清理集成测试环境。
- verify:执行所有检查,验证包是有效的,符合质量规范
- install :将打好的包安装到本地仓库,供其他项目使用;
- deploy :将打好的包安装到远程仓库,供其他项目使用;
3 site生命周期
- pre-site
- site :生成项目的站点文档;
- post-site
- site-deploy :发布生成的站点文档
使用maven 的site 插件 生成一个 可以在 浏览器中 查看项目的站点,以下的blog 是最基本的操作,可以添加其他的属性生成更加完善的站点文档。
首先在项目的pom.xml 添加一个插件site,就可以在项目target 下看到 有生成的文件
1 <plugin>
2 <groupId>org.apache.maven.plugins</groupId>
3 <artifactId>maven-site-plugin</artifactId>
4 <version>3.3</version>
5 <configuration>
6 <locales>zh_CN</locales>
7 </configuration>
8 </plugin>
进入dos到相应的项目目录下,输入命令:mvn site
生成的网页: