Maven Lifecycle
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven Lifecycle相关的知识,希望对你有一定的参考价值。
通常在使用Maven时,只需要把Maven的相关命令组合起来使用,就可以完成日常的工作了。Maven使用起来如此简单方便,到底是怎么做到的呢?在使用maven过程中,有时也会出现一些错误,怎么解决呢?要想知道这些东西,就需要对Maven的生命周期有所了解才好。
Maven Lifecycle 基本知识
Maven中,有三种内置的生命周期:default, clean, site 。其中default的生命周期用于处理项目部署的相关工作,clean生命周期用于处理项目的清理工作,site生命周期用于处理项目的站点文档创建的工作。
Phase
生命周期分为多个阶段,例如default生命周期,包含的阶段有(只列出了部分阶段):
- 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.
- integration-test - process and deploy the package if necessary into an environment where integration tests can be run
- 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.
生命周期的阶段是有序进行的。
上述列表中的阶段,就是按照:
validate –> compile -> test -> package -> integration-test -> verify -> install -> deploy 来进行的。
如果使用了mvn compile,那么就会执行:validate->compile两个阶段。
如果使用了mvn package,就会执行:validate –> compile -> test -> package。
如果使用了mvn install,那么就会执行:validate –> compile -> test -> package -> integration-test -> verify -> install
如果一个项目,有多个子项目,那么这个命令也会在多个子项目中执行。
例如mvn clean install命令,那么每一个子项目也都会先执行clean命令,再执行install命令。
Goal
每个生命周期的阶段,又可以细分为多个Goal(目标)。一个目标可以与一个阶段进行绑定,也可以不与一个阶段进行绑定。
例如:clean 生命周期包括了三个阶段(pre-clean,clean,post-clean)。在clean阶段,是与maven-clean-plugin插件的clean goal绑定的。所以在执行clean阶段时,是由maven-clean-plugin的clean goal来执行的。
在maven-clean-plugin 插件中,有两个goal:clean,help。Help这个goal没有与任何的阶段进行绑定。那么要想执行这个goal。就需要在命令行中明确的指定出这个goal。
例如:
内置生命周期的阶段定义与绑定
maven-core.jar的META-INF/plexus/components.xml中为三个内置的生命周期分配了phase,配置的内容是:
<lifecycles> <lifecycle> <id>default</id> <!-- START SNIPPET: lifecycle --> <phases> <phase>validate</phase> <phase>initialize</phase> <phase>generate-sources</phase> <phase>process-sources</phase> <phase>generate-resources</phase> <phase>process-resources</phase> <phase>compile</phase> <phase>process-classes</phase> <phase>generate-test-sources</phase> <phase>process-test-sources</phase> <phase>generate-test-resources</phase> <phase>process-test-resources</phase> <phase>test-compile</phase> <phase>process-test-classes</phase> <phase>test</phase> <phase>package</phase> <phase>pre-integration-test</phase> <phase>integration-test</phase> <phase>post-integration-test</phase> <phase>verify</phase> <phase>install</phase> <phase>deploy</phase> </phases> <!-- END SNIPPET: lifecycle --> </lifecycle> <lifecycle> <id>clean</id> <phases> <phase>pre-clean</phase> <phase>clean</phase> <phase>post-clean</phase> </phases> <default-phases> <clean>org.apache.maven.plugins:maven-clean-plugin:clean</clean> </default-phases> </lifecycle> <lifecycle> <id>site</id> <phases> <phase>pre-site</phase> <phase>site</phase> <phase>post-site</phase> <phase>site-deploy</phase> </phases> <default-phases> <site>org.apache.maven.plugins:maven-site-plugin:site</site> <site-deploy>org.apache.maven.plugins:maven-site-plugin:deploy</site-deploy> </default-phases> </lifecycle> </lifecycles>
<lifecycle><phases /></lifecycle>定义了生命周期拥有的阶段。
<lifecycle><default-phases /></lifecycle>定义了该生命周期默认绑定的阶段,即默认要使用的阶段。
从上述配置中,可以看出有三个内置的生命周期:default、clean、site。其中clean、site都指定了默认要使用的阶段。Default生命周期并没有指定默认要使用的阶段,因为它是由packaging方式决定的。
在项目中使用生命周期
上面是对Maven中的内置生命周期的说明。下面就来说说,在项目中使用packaging\plugin将goal绑定到生命周期中。
<packaging>
每个Maven项目的pom文件中,必定要配置一个<packaging>(如果不配置,默认值是jar),每一种packaging方式,其实都会将某些goal绑定到default 生命周期多个不同的阶段中。
在【内置生命周期阶段定义】的配置中,其中clean生命周期中,默认只使用了clean阶段,没有使用pre-clean,post-clean阶段。同样的,在site生命周期中,默认使用了site阶段和site-deploy阶段。上面的配置中,并没有为default生命周期指定默认的阶段。因为这个要根据packaging方式来动态绑定的。
下面就看看Packaging方式与内置的生命周期的绑定:
pom方式
<phases> <package>org.apache.maven.plugins:maven-site-plugin:attach-descriptor</package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
jar 方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:testResources </process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package> org.apache.maven.plugins:maven-jar-plugin:jar </package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
maven-plugin方式
<phases> <generate-resources>org.apache.maven.plugins:maven-plugin-plugin:descriptor</generate-resources> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:testResources </process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package> org.apache.maven.plugins:maven-jar-plugin:jar, org.apache.maven.plugins:maven-plugin-plugin:addPluginArtifactMetadata </package> <install> org.apache.maven.plugins:maven-install-plugin:install </install> <deploy> org.apache.maven.plugins:maven-deploy-plugin:deploy </deploy> </phases>
ejb方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:testResources </process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package> org.apache.maven.plugins:maven-ejb-plugin:ejb </package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
war方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:testResources </process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package>org.apache.maven.plugins:maven-war-plugin:war</package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
ear方式
<phases> <generate-resources> org.apache.maven.plugins:maven-ear-plugin:generate-application-xml </generate-resources> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <package>org.apache.maven.plugins:maven-ear-plugin:ear</package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
rar方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources> org.apache.maven.plugins:maven-resources-plugin:testResources </process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package>org.apache.maven.plugins:maven-rar-plugin:rar</package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
par方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package> org.apache.maven.plugins:maven-par-plugin:par </package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
ejb3方式
<phases> <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources> <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile> <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources> <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile> <test>org.apache.maven.plugins:maven-surefire-plugin:test</test> <package> org.apache.maven.plugins:maven-ejb3-plugin:ejb3 </package> <install>org.apache.maven.plugins:maven-install-plugin:install</install> <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy> </phases>
Plugins
上面是关于生命周期与pharse绑定关系的,其实在每一个阶段,要做哪些事情,完全是由plugin来完成的,一个plugin中会拥有多个goal,其实最终执行的都是plugin中的一个或者多个goal。
如果你想要改变插件的默认的行为,或者你想添加自己的插件的goal到指定的阶段。可以在相应Plugin中进行配置的。
clean插件的有两个goal: clean, help。 默认情况下,clean插件的clean goal是在clean阶段执行的,help goal是不会执行的。现在想让clean goal执行之前,先执行help goal。那么就需要在pre-clean阶段就执行help goal,就需要如下配置了: 在pluginManagement中把clean插件的help goal 添加到pre-clean阶段。 <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> <configuration> <indentSize>4</indentSize> <retryOnError>true</retryOnError> <verbose>true</verbose> </configuration> <executions> <execution> <id>auto-clean</id> <phase>pre-clean</phase> <goals> <goal>help</goal> </goals> </execution> </executions> </plugin> </plugins> </pluginManagement> 引用指定的插件: <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> </plugin> </plugins>
如此一来,使用mvn clean执行时,就会先执行help(pre-clean阶段执行),再执行clean(clean 阶段执行),下面就是测试结果:
[INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building helloworld 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:3.0.0:help (auto-clean) @ helloworld --- [INFO] Apache Maven Clean Plugin 3.0.0 The Maven Clean Plugin is a plugin that removes files generated at build-time in a project‘s directory. This plugin has 2 goals: clean:clean Goal which cleans the build. This attempts to clean a project‘s working directory of the files that were generated at build-time. By default, it discovers and deletes the directories configured in project.build.directory, project.build.outputDirectory, project.build.testOutputDirectory, and project.reporting.outputDirectory. Files outside the default may also be included in the deletion by configuring the filesets tag. clean:help Display help information on maven-clean-plugin. Call mvn clean:help -Ddetail=true -Dgoal=<goal-name> to display parameter details. [INFO] [INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ helloworld --- [INFO] Deleting E:\workspaces\workspace_maven3\helloworld\target [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven\helloworld\HelloTest.class [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven\helloworld [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\surefire-reports\TEST-com.fjn.maven.helloworld.HelloTest.xml [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\surefire-reports\com.fjn.maven.helloworld.HelloTest.txt [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\surefire-reports [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile\inputFiles.lst [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile\createdFiles.lst [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile\inputFiles.lst [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile\createdFiles.lst [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-archiver\pom.properties [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-archiver [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\jdbc.properties [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld\HelloImpl.class [INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld\Hello.class [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes [INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.828 s [INFO] Finished at: 2016-01-26T09:43:33+08:00 [INFO] Final Memory: 5M/15M [INFO] ------------------------------------------------------------------------
以上是关于Maven Lifecycle的主要内容,如果未能解决你的问题,请参考以下文章
Cannot read lifecycle mapping metadata for artifact org.apache.maven.plugins问题的解决