Maven生命周期


Maven的三个生命周期:clean、default和site。


1. 简介

Maven共有三个相互独立的生命周期(Build Lifecycle):clean、default和site。

每个生命周期又可以细分成不同的阶段(Phase)。

2. clean生命周期

clean生命周期用于清理工程。

阶段功能
pre-clean executes processes needed prior to the actual project cleaning
执行在真正清理前需要完成的工作。缺省为空。
clean remove all files generated by the previous build
清理上一次构建时生成的文件。缺省为删除 target 目录。
post-clean executes processes needed to finalize the project cleaning
执行在清理后需要完成的工作。缺省为空。

3. default生命周期

default生命周期用于构建。

阶段功能
validate validate the project is correct and all necessary information is available.
initialize initialize build state, e.g. set properties or create directories.
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
process-test-classes post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
test run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
使用单元测试框架执行测试用例
prepare-package perform any operations necessary to prepare a package before the actual packaging. This often results in an unpacked, processed version of the package. (Maven 2.1 and above)
package take the compiled code and package it in its distributable format, such as a JAR.
将编译好的代码打包。如:打成 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.
将包部署到远端的仓库,以便其他开发人员可以使用。

4. site生命周期

site生命周期用于建立和发布项目站点。

阶段功能
pre-site executes processes needed prior to the actual project site generation
site generates the project's site documentation
post-site executes processes needed to finalize the site generation, and to prepare for site deployment
site-deploy deploys the generated site documentation to the specified web server

5. 执行顺序

执行 Maven 的主要方式就是调用 Maven 生命周期阶段。

注意:生命周期是相互独立的,但一个生命周期内部的阶段则是顺序的,要想执行后一个阶段,必须先执行前一个阶段。

  • mvn clean : 调用 clean 生命周期的 clean 阶段。
  • mvn test : 调用 default 生命周期的 test 阶段(及 test 阶段前的所有阶段)。
  • mvn clean deploy : 先调用 clean 生命周期的 clean 阶段, 再调用 default 生命周期的 deploy 阶段。由于 deploy 是 default 生命周期中的最后一个阶段,所以执行了整个 default 生命周期。

通过执行下面的代码,可以更好的理解 phase 与 lifeCycle 之间的关系。 MvnPhase.java

参考


声明: 本文采用 CC BY-NC-SA 3.0 协议进行授权,转载请注明出处。