Maven学习笔记四 生命周期

Maven的项目管理和构建是基于生命周期这个概念的。

这意味着构建和发布工程构件的过程是十分清晰的。

在构建过程中有三个生命周期:default,clean,site。

default主要用来处理项目部署和构建

clean是项目的清理

site是产生项目文档

组成生命周期的阶段

每个生命周期都有自己不同的阶段列表。以default为例:

validate:验证

complie:编译

test:测试

package:打包

integration-test:集成测试

verify:检验包的有效性

install:安装

deploy:发布

阶段都按顺序执行,每一个阶段的执行都包含以上的阶段。

多个生命周期的阶段可以在同一个命令执行

mvn clean install

先清理后安装

插件目标组成阶段

一个阶段往往绑定了0个或多个插件目标。

阶段的任务都靠执行这些绑定的插件目标来完成相应的任务。

一个插件目标就是一个指定的任务。

有些插件目标并不需要绑定阶段可以直接调用。插件目标可以和阶段在同一个指令中。例如:

mvn clean dependency:copy-dependencies package

阶段将执行所有绑定的插件目标,一个插件目标可以绑定多个阶段。

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

简单的构建生命周期已经足够使用,那么怎么在构建阶段指定任务

第一个方法,就是通过设置pom中的packaging的值

这个在pom中的<packaging>中可以去绑定一些插件目标给构建阶段。

第二个方法,就是在<plugin>单元中指定plugin

 <plugin>
   <groupId>org.codehaus.modello</groupId>
   <artifactId>modello-maven-plugin</artifactId>
   <version>1.4</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>
 Modello插件默认绑定目标 modello:java 给 generate-sources 阶段

可以指定绑定阶段

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

附录:生命周期的阶段

Clean Lifecycle

pre-cleanexecutes processes needed prior to the actual project cleaning
cleanremove all files generated by the previous build
post-cleanexecutes processes needed to finalize the project cleaning

Default Lifecycle

validatevalidate the project is correct and all necessary information is available.
initializeinitialize build state, e.g. set properties or create directories.
generate-sourcesgenerate any source code for inclusion in compilation.
process-sourcesprocess the source code, for example to filter any values.
generate-resourcesgenerate resources for inclusion in the package.
process-resourcescopy and process the resources into the destination directory, ready for packaging.
compilecompile the source code of the project.
process-classespost-process the generated files from compilation, for example to do bytecode enhancement on Java classes.
generate-test-sourcesgenerate any test source code for inclusion in compilation.
process-test-sourcesprocess the test source code, for example to filter any values.
generate-test-resourcescreate resources for testing.
process-test-resourcescopy and process the resources into the test destination directory.
test-compilecompile the test source code into the test destination directory
process-test-classespost-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
testrun tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.
prepare-packageperform 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)
packagetake the compiled code and package it in its distributable format, such as a JAR.
pre-integration-testperform actions required before integration tests are executed. This may involve things such as setting up the required environment.
integration-testprocess and deploy the package if necessary into an environment where integration tests can be run.
post-integration-testperform actions required after integration tests have been executed. This may including cleaning up the environment.
verifyrun any checks to verify the package is valid and meets quality criteria.
installinstall the package into the local repository, for use as a dependency in other projects locally.
deploydone in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Site Lifecycle

pre-siteexecutes processes needed prior to the actual project site generation
sitegenerates the project's site documentation
post-siteexecutes processes needed to finalize the site generation, and to prepare for site deployment
site-deploydeploys the generated site documentation to the specified web server

默认的绑定目标:

Clean Lifecycle Bindings

cleanclean:clean

Default Lifecycle Bindings - Packaging ejb / ejb3 / jar / par / rar / war

process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packageejb:ejb or ejb3:ejb3 or jar:jar or par:par or rar:rar or war:war
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging ear

generate-resourcesear:generate-application-xml
process-resourcesresources:resources
packageear:ear
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging maven-plugin

generate-resourcesplugin:descriptor
process-resourcesresources:resources
compilecompiler:compile
process-test-resourcesresources:testResources
test-compilecompiler:testCompile
testsurefire:test
packagejar:jar and plugin:addPluginArtifactMetadata
installinstall:install
deploydeploy:deploy

Default Lifecycle Bindings - Packaging pom

packagesite:attach-descriptor
installinstall:install
deploydeploy:deploy

Site Lifecycle Bindings

sitesite:site
site-deploysite:deploy
  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值