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-clean | executes processes needed prior to the actual project cleaning |
clean | remove all files generated by the previous build |
post-clean | executes processes needed to finalize the project cleaning |
Default Lifecycle
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 Lifecycle
pre-site | executes processes needed prior to the actual project site generation |
site | generates the project's site documentation |
post-site | executes processes needed to finalize the site generation, and to prepare for site deployment |
site-deploy | deploys the generated site documentation to the specified web server |
默认的绑定目标:
clean | clean:clean |
Default Lifecycle Bindings - Packaging 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 Lifecycle Bindings - Packaging ear
generate-resources | ear:generate-application-xml |
process-resources | resources:resources |
package | ear:ear |
install | install:install |
deploy | deploy:deploy |
Default Lifecycle Bindings - Packaging 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 Lifecycle Bindings - Packaging pom
package | site:attach-descriptor |
install | install:install |
deploy | deploy:deploy |
site | site:site |
site-deploy | site:deploy |