1.文章内容概述:
任何一个工程从创建开始,到编译,到测试,到部署..,都是有完整的生命周期的,使用maven管理开发出的project也不例外,只不过所有使用maven管理的project都遵循一套标准的生命周期,即maven project LifeCycle.本文就针对maven管理的project的标准生命周期作详细解说。
2.maven管理的project的生命周期:
2.1基础知识点:
-
- maven的完整生命周期lifeCycle是由若干阶段phase组合而成的,所以有必要先介绍一下组成maven project的lifeCycle的各个阶段(phase):
-
- validate - validate the project is correct and all necessary information is available
- compile - compile the source code of the project
- test - test the compiled source code 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.
- verify - run any checks on results of integration tests to ensure quality criteria are met
- install - install the package into the local repository, for use as a dependency in other projects locally
- deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
- ... 其他phase,这里没有一一列举
- ...
- ...
- ...
-
- maven的完整生命周期又分成多种,不同种类的lifeCycle实际上包含不同的phase。换句话说,phase的不同组合可以组合成maven project的多个不同的maven lifeCycle,如“the default lifecycle”,“the clean lifecycle”,“the site lifecycle”,maven的相关文件定义了这些lifecycle具体是由哪些phase组成的,The full Maven lifecycle is defined by the components.xml file in the maven-core module, with associated documentation for reference.下面的表格列出了maven中各个lifecycle的组成结构(phase lists)
-
- 其中the defaule lifecycle是由下面的phase 顺序组合 而成的,
-
-
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. 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.
-
-
- 其中the defaule lifecycle是由下面的phase 顺序组合 而成的,
-
- maven的完整生命周期lifeCycle是由若干阶段phase组合而成的,所以有必要先介绍一下组成maven project的lifeCycle的各个阶段(phase):
-
-
- 其中the clean lifecycle是由下面的phase 顺序组合 而成的,
-
pre-clean | execute processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | execute processes needed to finalize the project cleaning |
-
-
- 其中the site lifecycle是由下面的phase 顺序组合 而成的,
-
pre-site execute processes needed prior to the actual project site generation site generate the project's site documentation post-site execute processes needed to finalize the site generation, and to prepare for site deployment site-deploy deploy the generated site documentation to the specified web server
-
3.从上面一部分内容我们已经知道,maven project中其实是有三种lifeCycle(即the default/clean/site lifecycle),另外,还需要知道,有一些maven相关的命令来控制你的执行过程,包括控制你执行的是哪一个生命周期(是default,clean还是site 生命周期),还包括控制执行到具体某个生命周期的哪个phase(阶段)
-
-
-
-
- 执行相应的maven命令时,系统会按照上述表格, 顺序执行 具体某个lifeCycle 的 若干phase
- 如命令mvn install,则顺序执行the default lifecycle中的 validate、initialize、generate-sources.......verify、install这23个阶段(phase)
- 再如 mvn clean,则顺序执行the clean lifecycle中的pre-clean、clean这2个阶段(phase)
- 再如 mvn clean install,则先执行the clean lifecycle的相应(pre-clean、clean)这两个phase之后,又执行the default lifecycle中的validate、initialize、generate-sources.......verify、install这23个阶段(phase))
-
-
-
4.phase可以绑定plugin goal,同一个phase绑定不同的plugin goal的时候,实际运行时会产生不同的行为
-
- if a goal is bound to one or more build phases, that goal will be called in all those phases.
- Furthermore, a build phase can also have zero or more goals bound to it. If a build phase has no goals bound to it, that build phase will not execute.
- But if it has one or more goals bound to it, it will execute all those goals
- maven 工程本身已经为不同package模式下的the default lifecycle的若干phase默认配置了一些 plugin,参见maven官网documentation
- 例一,如下命令的运行机制
mvn clean dependency:copy-dependencies package
首先可以看到该命令涉及了两个phase,分别是the clean lifecycle的clean和the default lifecycle的package这两个phase;其次可以看到该命令还绑定了一个plugin goal(dependency:copy-dependencies)。所以这个命令的整体运行过程是这样的:If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).也就是说,先运行the clean lifecycle的pre-clean、clean这两个phase,然后运行 dependency:copy-dependencies goal,再然后运行the default lifecycle的validate、initialize、generate-sources.......verify、install这23个阶段(phase)
- 除了上述例子中的 dependency:copy-dependencies goal,还有哪些plugin goal是可以被绑定在phase上的呢,下表中列举了可以绑定在the default lifecycle的各个phase的plugin goal,
-
- Some phases have goals bound to them by default.And for the default lifecycle, these bindings depend on the packaging value. (下面的表格列举出maven内部默认绑定在各个phase上的plugin,这些都是默认情况下就已经绑定好的)
-
-
-
-
Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war
process-resources resources:resources compile compiler:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package ejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war install install:install deploy deploy:deploy -
Default Lifecycle Bindings - Packaging ear
generate-resources ear:generate-application-xml process-resources resources:resources package ear:ear install install:install deploy deploy:deploy -
Default Lifecycle Bindings - Packaging maven-plugin
generate-resources plugin:descriptor process-resources resources:resources compile compiler:compile process-test-resources resources:testResources test-compile compiler:testCompile test surefire:test package jar:jar and plugin:addPluginArtifactMetadata install install:install deploy deploy:deploy -
Default Lifecycle Bindings - Packaging pom
package site:attach-descriptor install install:install deploy deploy:deploy -
Clean Lifecycle Bindings
clean clean:clean -
Site Lifecycle Bindings
site site:site site-deploy site:deploy - In Maven 2.x, default lifecycle bindings were included in components.xml, but in Maven 3.x, they are defined in a separate default-bindings.xml descriptor。 pom、jar、ejb、ejb3、maven-plugin、war、ear、rar、par packaging可以绑定的plugin goal,参见maven官网documentation
-
-
-
2.2相关命令:
1)若干常用命令:
- mvn install 在开发环境中执行此命令。 这个命令是执行default lifeCycle的相应phase,执行此命令时,会顺序执行the default lifecycle 的validate、initialize、generate-sources.......verify、install这23个阶段(phase)。命令执行的最终结果是 build and install artifacts into the local repository,也即将自己的项目打包好放到maven的本地仓库。
- mvn clean deploy In a build environment中执行此命令, 这个命令是执行the clean lifecycle的相应phase(pre-clean、clean)之后又执行the default lifecycle中的相应phase(即validate、initialize、generate-sources.......verify、install这23个阶段(phase)),命令执行的最终结果是 cleanly build and deploy artifacts into the shared repository,也即将自己的项目部署到maven的中央仓库,从而使得其他人可以使用你的项目。
- mvn clean dependency:copy-dependencies package这个命令的整体运行过程是这样的:先运行the clean lifecycle的pre-clean、clean这两个phase,然后运行 dependency:copy-dependencies goal,再然后运行the default lifecycle的validate、initialize、generate-sources.......verify、install这23个阶段(phase)。
2)这些mvn命令的运行机制
-
-
- 首先,他会根据mvn具体命令查找该命令所涉及的phase,
- 其次,他会根据phase查找对应的lifeCycle(判断涉及default、clean、site 生命周期中的哪一个或者哪几个lifeCycle)
- 再次,顺序执行相应lifecycle 的一系列phase
- 最后要注意的是,在真正执行相应phase的时候,要注意phase具体绑定的plugin goals,因为plugin goals决定了相应phase的具体行为(即使是相同的phase,绑定了一个plugin goal时会有一种行为,绑定另一个plugin时又会有另外一种行为)
- 例一,如下命令的运行机制
mvn clean dependency:copy-dependencies package
首先可以看到该命令涉及了两个phase,分别是the clean lifecycle的clean和the default lifecycle的package这两个phase;其次可以看到该命令还绑定了一个plugin goal(dependency:copy-dependencies)。所以这个命令的整体运行过程是这样的:If this were to be executed, the clean phase will be executed first (meaning it will run all preceding phases of the clean lifecycle, plus the clean phase itself), and then the dependency:copy-dependencies goal, before finally executing the package phase (and all its preceding build phases of the default lifecycle).也就是说,先运行the clean lifecycle的pre-clean、clean这两个phase,然后运行 dependency:copy-dependencies goal,再然后运行the default lifecycle的validate、initialize、generate-sources.......verify、install这23个阶段(phase)。
-