天天看點

Maven 生命周期與插件Maven 生命周期與插件

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

繼續閱讀