Apache Maven --- [构建生命周期]

构建生命周期基础html

Maven基于构建生命周期的核心概念.这意味着一个特定工件(项目)的构建和分布过程是明肯定义的.咱们只须要学习一些必要的命令集合就能够构建项目,而POM将会保证咱们获得咱们想要的结果.java

这里有三个内置的构建生命周期:default,clean,site.default支持项目部署,clean支持项目清理,而site支持你的项目站点文档的建立.web

一个构建生命周期由阶段构成apache

每个构建生命周期都被定义成不一样的构建阶段列表,每个构建阶段都表明生命周期中的一个阶段.安全

例如,default生命周期由如下阶段组成bash

validate-验证项目是正确的而且全部的必要信息是可用的服务器

compile-编译项目源码app

test-用合适的单元测试框架测试编译后的代码,这些测试不须要代码被打包或部署框架

package-拿取编译后的代码并将它打包成它的可分配的格式,如JARmaven

verify-在集成测试结果上运行检查以确保知足质量标准

install-安装包到本地仓库,做为依赖提供给其余本地项目

deploy-在构建环境下完成,拷贝最终的包到远程仓库分享给其余的开发者和项目

这些生命周期阶段(这里并无显示完整的生命周期阶段)被连续执行以完成default生命周期.上述给出的生命周期阶段意味着当default生命周期被使用时,Maven首先会验证项目,而后尝试编译项目,执行单元测试,打包二进制文件(如:JAR),运行集成测试,检查集成测试,安装检查后的包到本地仓库,最后部署安装包到远程仓库.

经常使用的命令行调用

在开发环境中,使用以下命令来构建和安装工件到本地仓库:

mvn install

这个命令会执行在install以前(包含install)default生命周期中的各个阶段.你只须要调用执行最后的构建阶段,在这里就是install.

在构建环境中,使用如下命令来纯净的构建和部署工件到共享仓库:

mvn clean deploy

这个命令同样能够被用在多模块方案(一个项目拥有一个或多个子项目).Maven遍历每一个子模块并执行clean,deploy(包括全部在此以前的构建阶段步骤).

一个构建阶段由插件目标构成

然而,即便一个构建阶段负责构建生命周期中的一个指定步骤,可是它执行这些责任的方式多是多种多样的.这经过声明插件目标绑定到那些构建阶段来完成.

一个插件目标表明了一个指定的任务(比构建阶段更精细),用来为构建和管理项目作出贡献.它额可能被绑定到零个或多个构建阶段.一个没有绑定到任何构建阶段的插件目标能够被从构建生命周期的外部直接调用执行.执行顺序依赖插件目标和构建阶段的调用顺序.例如:考虑以下命令.clean和package是构建阶段,而dependency:copy-dependencies是插件目标:

mvn clean dependency:copy-dependencies package

若是它被执行,clean构建阶段首先被执行(意味着它将执行全部clean生命周期的前置阶段,加上clean阶段自己),而后执行dependency:copy-dependencies目标,最后执行package构建阶段(全部default生命周期的前置阶段加上package构建阶段自己).

此外,若是目标被绑定到一个或多个构建阶段,那么它会被全部这些构建阶段所调用.

另外,一个构建阶段一样能够有零个或者多个插件目标绑定在上面.若是构建阶段没有目标绑定在上面,那么这个构建阶段将不会被执行.反之,则会执行绑定了的目标.

(请注意:在Maven2.0.5以上版本,多个目标绑定到一个阶段按照它们在POM中被声明的顺序执行,可是不支持相同插件的多个实例.相同插件的多个实例分组依次执行在Maven2.0.11以上版本)

一些阶段一般不从命令行调用

这些用连字符链接命名的阶段(pre-*,post-*,或process-*)一般不从命令行直接调用.这些阶段按顺序排列构建,产生构建外部不可用的中间结果.在执行集成测试的状况下,环境可能被挂机.

代码覆盖工具(如Jacoco)以及执行容器插件(如Tomcat,Cargo,Docker)绑定目标到pre-integration-test阶段来准备集成测试容器环境.这些插件也绑定目标到post-integration-test阶段来收集覆盖统计或解除集成测试容器.

故障安全和代码覆盖插件绑定目标到integration-test和verify阶段.获得的结果是在verify以后测试和覆盖报告是可用的.若是integration-test是从命令行被调用的,不会有报告生成.更坏的结果是测试容器环境将被挂起;Tomcat Web服务器和Docker实例会持续运行,并且Maven自身也不会终止.

使用构建生命周期设置你的项目

构建生命周期的使用很简单,可是当你为一个项目构造Maven构建时,你该如何分派任务到每个构建阶段?

打包

首先,最经常使用的方式,是经过同名的POM元素来为你的项目设置打包方案.一些可用的打包方案值是jar,war,ear和pom.若是没有指定值,默认是jar.

每一个打包方案包含了一个目标列表绑定到特定方案.例如,jar打包方案将会绑定以下目标到default生命周期中的对应构建阶段.

process-resources resources:resources
compile compiler:compile
process-test-resources resources:testResources
test-compile compiler:testCompile
test surefire:test
package jar:jar
install install:install
deploy deploy:deploy

这基本上是绑定的标准集合.然而,一些打包方案以不一样的方式处理它们.例如,一个纯粹是元数据的项目(打包方案是pom)仅仅绑定目标到install阶段和deploy阶段.

注意为了使某些打包类型可用,你也许须要包含一个指定的插件到你POM文件中的部分并为该插件指定true.一个须要这么作的插件案例是Plexus插件,它用来提供plexus-application和plexus-service打包类型.

插件

第二种添加目标到阶段的方式是在你的项目中配置插件.插件是提供目标给Maven的工件.另外,一个插件也许有一个或者多个目标,其中每一个目标都表明了插件的一个功能.例如,编译插件有两个功能:compile和testCompile.前者编译你的主要代码的源码,后者编译你测试代码的源码.

正如你再接下来的章节将会看到的,插件包含能代表一个目标将会绑定到哪一个生命周期阶段的信息.注意,添加插件的信息是不够的,你还必须指定你想运行的目标做为构建的一部分.

已经配置的目标将会被添加到根据打包类型所选择的被绑定到生命周期的目标中.若是超过一个目标被绑定到一个特定的阶段,那么执行顺序是自带的优先于配置进POM的.注意你可使用元素来得到更多的顺序控制.

例如,Modello插件默认绑定它的目标modello:java到generate-sources阶段.(注意,这个目标生成java代码).因此使用Modello插件,用它的模版来生成代码并整合到构建中去.你应该添加如下内容到你POM的元素中的中去:

...
 <plugin>
   <groupId>org.codehaus.modello</groupId>
   <artifactId>modello-maven-plugin</artifactId>
   <version>1.8.1</version>
   <executions>
     <execution>
       <configuration>
         <models>
           <model>src/main/mdo/maven.mdo</model>
         </models>
         <version>4.0.0</version>
       </configuration>
       <goals>
         <goal>java</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
...

你也许奇怪为何有元素在这.这是便于你在须要的时候能够屡次运行相同的目标经过不一样的配置.单独的executions能够给出一个ID以便于在继承或者应用配置文件期间可让你控制是否要合并目标配置或转变为一个额外的execution.

当给出许多executions匹配到一个特定的阶段时,它们按在POM中指定的顺序执行.继承的execution优先执行.

如今,对于modello.java来讲,它仅仅在generate-sources阶段有意义.可是一些目标能够被使用在超过一个阶段中,并且默认状况下它们也许是不能被察觉的.对于这些,你能够本身指定阶段.例如,让咱们假设咱们有个目标display:time用来显示当前时间到命令行,并且你但愿它运行在process-test-resources阶段来代表测试的开始时间.能够像这样配置:

...
 <plugin>
   <groupId>com.mycompany.example</groupId>
   <artifactId>display-maven-plugin</artifactId>
   <version>1.0</version>
   <executions>
     <execution>
       <phase>process-test-resources</phase>
       <goals>
         <goal>time</goal>
       </goals>
     </execution>
   </executions>
 </plugin>
...

生命周期参考

下面罗列了default,clean和site生命周期的全部构建阶段,它们是按序执行的:

Clean生命周期

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

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.
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.

Site生命周期

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

内置的生命周期绑定

有些阶段默认有一些目标绑定在上面.对于default生命周期来讲,这些绑定依赖于打包类型值.这里有一些绑定到阶段的目标

Clean生命周期绑定

clean clean:clean

Default生命周期绑定-打包类型(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生命周期绑定-打包类型(ear)

generate-resources ear:generate-application-xml
process-resources resources:resources
package ear:ear
install install:install
deploy deploy:deploy

Default生命周期绑定-打包类型(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生命周期绑定-打包类型(pom)

package site:attach-descriptor
install install:install
deploy deploy:deploy

Site生命周期绑定

site site:site
site-deploy site:deploy

参考

完整的Maven生命周期被定义在maven-core模块的components.xml文件中,详细介绍:https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html

在Maven2.x中,default生命周期绑定被包含在components.xml中,可是在Maven3.x,它们被定义在一个独立的default-bindings.xml描述中.

相关文章
相关标签/搜索