当前位置:首页 > 开发 > 开源软件 > 正文

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

发表于: 2014-08-03   作者:bit1129   来源:转载   浏览:
摘要: 从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/devs

从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操作

 

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

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
何为生命周期: Maven的生命周期就是为了对所有的构建过程进行抽象和统一。Maven从大量项目和构建工
例子包括的子模块有:一个java工程,一个android 库工程(打包为aar),一个Android 项目 一、首先
    坐标:       坐标最熟悉的定义应该来源于平面几何,在任何一个平面坐标系中,任何坐
Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你就会发现曾经大红大紫
Maven面临的挑战 软件行业新旧交替的速度之快往往令人咂舌,不用多少时间,你就会发现曾经大红大紫
以前使用maven时没有细了解过maven的生命周期,这段时间需要自己做架构设计,为了能更好的使用maven
一个完整的项目构建通常包括清理,编译,测试,打包,继承部署等步骤,而maven是生命周期的抽象,具
1.安装和配置   去maven官网下载,这里用的是3.1的版本。然后解压。我这里是D:\Java\IDE\apache-m
转自: http://blog.csdn.net/lhzx_zjg/article/details/6096060 1、 什么是Maven Maven是一个项目管
Apache Maven是一个软件项目管理和综合工具。基于项目对象模型(POM)的概念,Maven可以从一个中心
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号