[Maven学习笔记六]Maven生命周期

本文详细解析了Maven的生命周期,包括clean、default和site三个主要阶段,并解释了mvntest命令执行的具体步骤及其背后的插件目标。此外还介绍了Default生命周期中的各个目标及执行顺序。

从mvn test的输出开始说起

 

当我们在user-core中执行mvn test时,执行的输出如下:

 

/software/devsoftware/jdk1.7.0_55/bin/java -Dmaven.home=/software/devsoftware/apache-maven-3.2.1 -Dclassworlds.conf=/software/devsoftware/apache-maven-3.2.1/bin/m2.conf -Didea.launcher.port=7532 -Didea.launcher.bin.path=/software/devsoftware/idea-IU-135.690/bin -Dfile.encoding=UTF-8 -classpath /software/devsoftware/apache-maven-3.2.1/boot/plexus-classworlds-2.5.1.jar:/software/devsoftware/idea-IU-135.690/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain org.codehaus.classworlds.Launcher -Didea.version=13.1.2 test
[INFO] Scanning for projects...
[INFO] 
[INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building user.core 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ user.core ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ user.core ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ user.core ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ~/development/learnmaven/user.manager/user.core/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ user.core ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ user.core ---
[INFO] Surefire report directory: ~/development/learnmaven/user.manager/user.core/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running HelloTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.08 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.501 s
[INFO] Finished at: 2014-08-03T17:51:33+08:00
[INFO] Final Memory: 9M/104M
[INFO] ----------------------------------------------------------------------

 

从执行结果中,我们可以看到,mvn test实际执行了如下几步包括src/main/resources下资源文件的编译,src/main/java下Java文件的编译,src/test/resources下资源文件的编译,src/test/java下Java文件的编译,以及最后运行测试。这五个步骤实际上是执行了三个插件的五个目标,mvn test的五个目标以及它们的次序是由Maven的生命周期控制的

maven-resources-plugin:2.6:resources

maven-compiler-plugin:2.5.1:compile

maven-resources-plugin:2.6:testResources

maven-compiler-plugin:2.5.1:testCompile

maven-surefire-plugin:2.12.4:test

 

Maven的三组生命周期

 

Maven定义了3组生命周期,

1. clean生命周期

2. default生命周期(也称为compile生命周期)

3. site生命周期

 

这些生命周期管理着我们在执行Maven的某个生命周期类的操作时,Maven到底做了什么事情,比如执行mvn clean,执行mvn compile,mvn test,mvn package,mvn install时,Maven执行build的过程。

参考:http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

clean生命周期

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生命周期

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.

上面的表格定义了当执行validate,compile,test,package,verify,install,deploy等目标时,这个目标包含哪些要执行的目标以及这些目标的执行顺序。这些目标由Maven的插件执行的,因此可以说,Maven的生命周期是可以通过添加插件机制来进行扩展和定制

 

 执行compile目标时,是否会执行clean目标

 因为compile目标属于Default生命周期,clean目标属于Clean生命周期,Default和Clean两个生命周期独立,因此,compile目标执行时,不会执行clean,因此,我们在Inteillj Idea中在Maven模块中执行compile时,不会触发clean操作

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值