欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven 生命周期与插件

程序员文章站 2022-04-23 16:55:14
...

Maven 生命周期与插件

 

一、生命周期

Maven有三套独立的生命周期,分别为clean,default和site。clean生命周期的目的是清理之前的构建,default的目的是构建项目,site的目的是建立项目站点。

 

每个生命周期都包含一些阶段(build phase),这些阶段是有顺序的,并且后面的阶段依赖于前面的阶段。三套生命周期是完全独立的,我们可以仅调用clean生命周期的某个阶段,或者仅调用default生命周期的某个阶段。

 

执行生命周期:

 

mvn [options] [<goal(s)>] [<phase(s)>]

 

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

二、插件与目标

插件:Maven的生命周期是抽象的,也就是说,执行一个生命周期实际上什么也没干,而实际的构建的活是由插件(Plugin)完成的,插件以独立的构件存在,Maven会在必要时下载这些构件以支持构建。

目标:对于插件本身,为了能够复用代码,它一般能够完成多项功能,例如maven-dependency-pugin插件,它能够基于项目依赖做很多的事情,比如分析项目依赖,列出项目的依赖树,列出项目已解析的依赖等等。为每个这样的功能都编写一个独立的插件是不合理的,为了代码的复用,Maven将这些功能聚集在一个插件里,每个功能就是一个插件目标(Plugin Goal)

 

Maven生命周期与插件相互绑定,更具体的说是与插件的目标绑定,以完成某个具体的构建任务。A build phase is made up of plugin goals

 

内置绑定

为了能让用户几乎不用任何配置就能进行构建,Maven为一些主要的生命周期阶段绑定了一些插件目标,当通过命令执行生命周期时,相应的插件目标就会执行。

 
clean生命周期的内置绑定
clean maven-clean-plugin:clean
default生命周期的内置绑定

default生命周期的绑定关系与具体的打包类型(pom中的packaging元素)相关

 

打包类型为 jar、ejb、ejb3、par、rar、war时,其绑定关系如下:

process-resources maven-resources-plugin:resources
compile maven-compiler-plugin:compile
process-test-resources maven-resources-plugin:testResources
test-compile maven-compiler-plugin:testCompile
test maven-surefire-plugin:test
package 以jar为例 maven-jar-plugin:jar
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

 打包类型为ear时,其绑定关系如下:

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

打包类型为maven-plugin 时,其绑定关系如下:

generate-resources maven-plugin-plugin:descriptor
process-resources maven-resources-plugin:resources
compile maven-compiler-plugin:compile
process-test-resources maven-resources-plugin:testResources
test-compile maven-compiler-plugin:testCompile
test maven-surefire-plugin:test
package maven-jar-plugin:jar and maven-plugin-plugin:addPluginArtifactMetadata
install maven-install-plugin:install
deploy maven-deploy-plugin:deploy

打包类型为pom时, 其绑定关系如下:

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

 

site生命周期的内置绑定
site maven-site-plugin:site
site-deploy maven-site-plugin:deploy

三、自定义绑定

大部分情况下内置绑定都不能完全满足我们的需求,因此我们需要通过配置pom.xml来手动绑定一些插件目标。

 

举个例子,我想把多个模块生成的jar包以及外部依赖都复制到一个lib目录下,那么maven-dependency-plugin的copy和copy-dependencies目标可以帮我们实现这个需求:

 

...
<build>
<plugin>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.2</version>
	<configuration>
		<excludeTransitive>false</excludeTransitive>
		<stripVersion>false</stripVersion>
                <outputDirectory>${libPath}</outputDirectory>  <!-- 复制到哪个目录 -->
	</configuration>
	<executions>
		<execution>
		        <id>copy-dependencies</id>     
		        <phase>package</phase>    <!-- 绑定到default生命周期的package阶段 -->
		        <goals>
			        <goal>copy-dependencies</goal>      <!-- 指定目标:复制外部依赖 -->
		        </goals>
		        <configuration>
		                <includeScope>runtime</includeScope>
		                <overWriteIfNewer>true</overWriteIfNewer>
		        </configuration>
		</execution>
		<execution>
			<id>copy</id>    
			<phase>package</phase>     <!-- 在package阶段执行该任务 -->
			<goals>
				<goal>copy</goal>      <!-- 指定目标:复制模块生成的jar -->
			</goals>
			<configuration>
			        <artifactItems>
				        <artifactItem>       <!-- -->
					        <groupId>${project.groupId}</groupId>
					        <artifactId>${project.artifactId}</artifactId>
					        <version>${project.version}</version>
					        <overWrite>true</overWrite>
				        </artifactItem>
			        </artifactItems>
			</configuration>
		</execution>
	</executions>
</plugin>
</build>
...

 

更详细的插件信息 http://maven.apache.org/plugins/index.html