首页 > 其他 > 详细

Maven 生命周期

时间:2015-07-11 14:51:49      阅读:158      评论:0      收藏:0      [点我收藏+]

生命周期是指项目的构建过程,它包含了一系列的有序的阶段(phase),而一个阶段就是构建过程中的一个步骤。

Maven有一下三种标准的生命周期,最常用的是默认的Maven生命周期(default Maven lifecycly):

  • clean
  • default(or build)
  • site

目标代表一个特定的任务,这有助于项目的建设和管理。目标可以被绑定到零个或多个生成阶段。一个没有绑定到任何构建阶段的目标的构建生命周期可以执行直接调用。执行的顺序取决于目标和构建阶段被调用的顺序。

一 clean 生命周期

执行mvn clean之后,maven的清洁生命周期由一下三个阶段组成:

  • pre-clean
  • clean
  • post-clean

下面我们将使用 maven-antrun-plugin:run进行调用clean生命周期。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   
  5.   <groupId>com.maven.demo</groupId>  
  6.   <artifactId>MavenDemo</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.   
  10.   <name>MavenDemo</name>  
  11.   <url>http://maven.apache.org</url>  
  12.   
  13.   <properties>  
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.   </properties>  
  16.   
  17.   <build>  
  18.   <plugins>  
  19.    <plugin>  
  20.    <groupId>org.apache.maven.plugins</groupId>  
  21.    <artifactId>maven-antrun-plugin</artifactId>  
  22.    <version>1.1</version>  
  23.    <executions>  
  24.       <execution>  
  25.          <id>id.pre-clean</id>  
  26.          <phase>pre-clean</phase>  
  27.          <goals>  
  28.             <goal>run</goal>  
  29.          </goals>  
  30.          <configuration>  
  31.             <tasks>  
  32.                <echo>pre-clean phase</echo>  
  33.             </tasks>  
  34.          </configuration>  
  35.       </execution>  
  36.       <execution>  
  37.          <id>id.clean</id>  
  38.          <phase>clean</phase>  
  39.          <goals>  
  40.           <goal>run</goal>  
  41.          </goals>  
  42.          <configuration>  
  43.             <tasks>  
  44.                <echo>clean phase</echo>  
  45.             </tasks>  
  46.          </configuration>  
  47.       </execution>  
  48.       <execution>  
  49.          <id>id.post-clean</id>  
  50.          <phase>post-clean</phase>  
  51.          <goals>  
  52.             <goal>run</goal>  
  53.          </goals>  
  54.          <configuration>  
  55.             <tasks>  
  56.                <echo>post-clean phase</echo>  
  57.             </tasks>  
  58.          </configuration>  
  59.       </execution>  
  60.    </executions>  
  61.    </plugin>  
  62.    </plugins>  
  63.    </build>  
  64. </project>  

执行 "mvn post-clean“之后,显示如下:

  1. [INFO] Scanning for projects...  
  2. [INFO]                                                                           
  3. [INFO] ------------------------------------------------------------------------  
  4. [INFO] Building MavenDemo 0.0.1-SNAPSHOT  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO]   
  7. [INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ MavenDemo ---  
  8. [INFO] Executing tasks  
  9.      [echo] pre-clean phase  
  10. [INFO] Executed tasks  
  11. [INFO]   
  12. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---  
  13. [INFO] Deleting D:\workspace_luna\MavenDemo\target  
  14. [INFO]   
  15. [INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ MavenDemo ---  
  16. [INFO] Executing tasks  
  17.      [echo] clean phase  
  18. [INFO] Executed tasks  
  19. [INFO]   
  20. [INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ MavenDemo ---  
  21. [INFO] Executing tasks  
  22.      [echo] post-clean phase  
  23. [INFO] Executed tasks  
  24. [INFO] ------------------------------------------------------------------------  
  25. [INFO] BUILD SUCCESS  
  26. [INFO] ------------------------------------------------------------------------  
  27. [INFO] Total time: 20.888 s  
  28. [INFO] Finished at: 2015-02-11T15:57:21+08:00  
  29. [INFO] Final Memory: 5M/16M  
  30. [INFO] ------------------------------------------------------------------------  

可以看到依次执行了 pre-clean、clean以及 post-clean。并且执行结束之后项目中target路径中的编译好的文件被删除。说明清洁成功。

二 default 生命周期

默认生命周期是maven使用最多的生命周期,用于构建程序。它由一下23个阶段组成:

描述
validate Validates whether project is correct and all necessary information is available to complete the build process.
initialize Initializes build state, for example set properties
generate-sources Generate any source code to be included in compilation phase.
process-sources Process the source code, for example, filter any value.
generate-resources Generate resources to be included in the package.
process-resources Copy and process the resources into the destination directory, ready for packaging phase.
compile Compile the source code of the project.
process-classes Post-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.
generate-test-sources Generate any test source code to be included in compilation phase.
process-test-sources Process the test source code, for example, filter any values.
test-compile Compile the test source code into the test destination directory.
process-test-classes Process the generated files from test code file compilation.
test Run tests using a suitable unit testing framework(Junit is one).
prepare-package Perform any operations necessary to prepare a package before the actual packaging.
package Take the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.
pre-integration-test Perform actions required before integration tests are executed. For example, setting up the required environment.
integration-test Process and deploy the package if necessary into an environment where integration tests can be run.
pre-integration-test Perform actions required after integration tests have been executed. For example, cleaning up the environment.
verify Run any check-ups to verify the package is valid and meets quality criterias.
install Install the package into the local repository, which can be used as a dependency in other projects locally.
deploy Copies the final package to the remote repository for sharing with other developers and projects.

当我们通过命令调用一个阶段的时候,比如说 mvn compile,这个阶段以及这个阶段之前的所有阶段都将被执行。根据打包的种类(JAR/WAR/EAR),每个阶段会绑定不同的goal。

执行 mvn compile,target路径下会多一个maven-status的文件夹,里面存放了编译的结果。控制台输出结果如下:

  1. [INFO] Scanning for projects...  
  2. [INFO]                                                                           
  3. [INFO] ------------------------------------------------------------------------  
  4. [INFO] Building MavenDemo 0.0.1-SNAPSHOT  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO]   
  7. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ MavenDemo ---  
  8. [INFO] Using ‘UTF-8‘ encoding to copy filtered resources.  
  9. [INFO] skip non existing resourceDirectory D:\workspace_luna\MavenDemo\src\main\resources  
  10. [INFO]   
  11. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ MavenDemo ---  
  12. [INFO] Nothing to compile - all classes are up to date  
  13. [INFO] ------------------------------------------------------------------------  
  14. [INFO] BUILD SUCCESS  
  15. [INFO] ------------------------------------------------------------------------  
  16. [INFO] Total time: 0.999 s  
  17. [INFO] Finished at: 2015-02-11T16:19:31+08:00  
  18. [INFO] Final Memory: 7M/16M  
  19. [INFO] ------------------------------------------------------------------------ 

三 网站的生命周期

该阶段分为四个阶段:

  • pre-site
  • site
  • post-site
  • site-deploy

运行 mvn site之后,log如下:

  1. [INFO] Scanning for projects...  
  2. [INFO]                                                                           
  3. [INFO] ------------------------------------------------------------------------  
  4. [INFO] Building MavenDemo 0.0.1-SNAPSHOT  
  5. [INFO] ------------------------------------------------------------------------  
  6. [INFO]   
  7. [INFO] --- maven-site-plugin:3.3:site (default-site) @ MavenDemo ---  
  8. [INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin.  
  9. [INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.8  
  10. [WARNING] The repository url ‘https://oss.sonatype.org/content/repositories/snapshots‘ is invalid - Repository ‘sonatype-nexus-snapshots‘ will be blacklisted.  
  11. [INFO] Generating "Dependency Convergence" report    --- maven-project-info-reports-plugin:2.8  
  12. [INFO] Generating "Dependency Information" report    --- maven-project-info-reports-plugin:2.8  
  13. [INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.8  
  14. [INFO] Generating "Plugin Management" report    --- maven-project-info-reports-plugin:2.8  
  15. [INFO] Generating "Project Plugins" report    --- maven-project-info-reports-plugin:2.8  
  16. [INFO] Generating "Project Summary" report    --- maven-project-info-reports-plugin:2.8  
  17. [INFO] ------------------------------------------------------------------------  
  18. [INFO] BUILD SUCCESS  
  19. [INFO] ------------------------------------------------------------------------  
  20. [INFO] Total time: 04:34 min  
  21. [INFO] Finished at: 2015-02-11T16:27:07+08:00  
  22. [INFO] Final Memory: 14M/42M  
  23. [INFO] ------------------------------------------------------------------------  

运行之后,会在target路径下生成一个site文件夹,里面包括很多文件,包括css,images以及一些项目描述文件。

Maven 生命周期

原文:http://www.cnblogs.com/miniren/p/4638619.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!