Maven 生命周期

生命周期是指项目的构建过程,它包含了一系列的有序的阶段(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生命周期。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.maven.demo</groupId>
  <artifactId>MavenDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MavenDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
  <plugins>
   <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-antrun-plugin</artifactId>
   <version>1.1</version>
   <executions>
      <execution>
         <id>id.pre-clean</id>
         <phase>pre-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>pre-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.clean</id>
         <phase>clean</phase>
         <goals>
          <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>clean phase</echo>
            </tasks>
         </configuration>
      </execution>
      <execution>
         <id>id.post-clean</id>
         <phase>post-clean</phase>
         <goals>
            <goal>run</goal>
         </goals>
         <configuration>
            <tasks>
               <echo>post-clean phase</echo>
            </tasks>
         </configuration>
      </execution>
   </executions>
   </plugin>
   </plugins>
   </build>
</project>

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

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (id.pre-clean) @ MavenDemo ---
[INFO] Executing tasks
     [echo] pre-clean phase
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ MavenDemo ---
[INFO] Deleting D:\workspace_luna\MavenDemo\target
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (id.clean) @ MavenDemo ---
[INFO] Executing tasks
     [echo] clean phase
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (id.post-clean) @ MavenDemo ---
[INFO] Executing tasks
     [echo] post-clean phase
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 20.888 s
[INFO] Finished at: 2015-02-11T15:57:21+08:00
[INFO] Final Memory: 5M/16M
[INFO] ------------------------------------------------------------------------

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

二 default 生命周期

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

Lifecycle Phase描述
validateValidates whether project is correct and all necessary information is available to complete the build process.
initializeInitializes build state, for example set properties
generate-sourcesGenerate any source code to be included in compilation phase.
process-sourcesProcess the source code, for example, filter any value.
generate-resourcesGenerate resources to be included in the package.
process-resourcesCopy and process the resources into the destination directory, ready for packaging phase.
compileCompile the source code of the project.
process-classesPost-process the generated files from compilation, for example to do bytecode enhancement/optimization on Java classes.
generate-test-sourcesGenerate any test source code to be included in compilation phase.
process-test-sourcesProcess the test source code, for example, filter any values.
test-compileCompile the test source code into the test destination directory.
process-test-classesProcess the generated files from test code file compilation.
testRun tests using a suitable unit testing framework(Junit is one).
prepare-packagePerform any operations necessary to prepare a package before the actual packaging.
packageTake the compiled code and package it in its distributable format, such as a JAR, WAR, or EAR file.
pre-integration-testPerform actions required before integration tests are executed. For example, setting up the required environment.
integration-testProcess and deploy the package if necessary into an environment where integration tests can be run.
pre-integration-testPerform actions required after integration tests have been executed. For example, cleaning up the environment.
verifyRun any check-ups to verify the package is valid and meets quality criterias.
installInstall the package into the local repository, which can be used as a dependency in other projects locally.
deployCopies 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的文件夹,里面存放了编译的结果。控制台输出结果如下:

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

三 网站的生命周期

该阶段分为四个阶段:

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

运行 mvn site之后,log如下:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenDemo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-site-plugin:3.3:site (default-site) @ MavenDemo ---
[INFO] Rendering site with org.apache.maven.skins:maven-default-skin:jar:1.0 skin.
[INFO] Generating "Dependencies" report    --- maven-project-info-reports-plugin:2.8
[WARNING] The repository url 'https://oss.sonatype.org/content/repositories/snapshots' is invalid - Repository 'sonatype-nexus-snapshots' will be blacklisted.
[INFO] Generating "Dependency Convergence" report    --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Dependency Information" report    --- maven-project-info-reports-plugin:2.8
[INFO] Generating "About" report    --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Plugin Management" report    --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Project Plugins" report    --- maven-project-info-reports-plugin:2.8
[INFO] Generating "Project Summary" report    --- maven-project-info-reports-plugin:2.8
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:34 min
[INFO] Finished at: 2015-02-11T16:27:07+08:00
[INFO] Final Memory: 14M/42M
[INFO] ------------------------------------------------------------------------

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值