从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-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生命周期
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. |
上面的表格定义了当执行validate,compile,test,package,verify,install,deploy等目标时,这个目标包含哪些要执行的目标以及这些目标的执行顺序。这些目标由Maven的插件执行的,因此可以说,Maven的生命周期是可以通过添加插件机制来进行扩展和定制
执行compile目标时,是否会执行clean目标
因为compile目标属于Default生命周期,clean目标属于Clean生命周期,Default和Clean两个生命周期独立,因此,compile目标执行时,不会执行clean,因此,我们在Inteillj Idea中在Maven模块中执行compile时,不会触发clean操作