Maven Lifecycle

  • Maven Lifecycle 基本知识

    • Phase
    • Goal
    • 内置生命周期定义及绑定
  • 在项目中使用生命周期
    • packaging

      • pom
      • jar
      • maven-plugin
      • ejb
      • war
      • ear
      • rar
      • par
      • ejb3
    • plugin

  通常在使用Maven时,只需要把Maven的相关命令组合起来使用,就可以完成日常的工作了。Maven使用起来如此简单方便,到底是怎么做到的呢?在使用maven过程中,有时也会出现一些错误,怎么解决呢?要想知道这些东西,就需要对Maven的生命周期有所了解才好。

Maven Lifecycle 基本知识

Maven中,有三种内置的生命周期:default, clean, site 。其中default的生命周期用于处理项目部署的相关工作,clean生命周期用于处理项目的清理工作,site生命周期用于处理项目的站点文档创建的工作。

Phase

生命周期分为多个阶段,例如default生命周期,包含的阶段有(只列出了部分阶段):

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run
  • 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 -> integration-test -> verify -> install -> deploy 来进行的。

如果使用了mvn compile,那么就会执行:validate->compile两个阶段。

如果使用了mvn package,就会执行:validate –> compile -> test -> package。

如果使用了mvn install,那么就会执行:validate –> compile -> test -> package -> integration-test -> verify -> install

如果一个项目,有多个子项目,那么这个命令也会在多个子项目中执行。

例如mvn clean install命令,那么每一个子项目也都会先执行clean命令,再执行install命令。

Goal

每个生命周期的阶段,又可以细分为多个Goal(目标)。一个目标可以与一个阶段进行绑定,也可以不与一个阶段进行绑定。

例如:clean 生命周期包括了三个阶段(pre-clean,clean,post-clean)。在clean阶段,是与maven-clean-plugin插件的clean goal绑定的。所以在执行clean阶段时,是由maven-clean-plugin的clean goal来执行的。

在maven-clean-plugin 插件中,有两个goal:clean,help。Help这个goal没有与任何的阶段进行绑定。那么要想执行这个goal。就需要在命令行中明确的指定出这个goal。

例如:

内置生命周期的阶段定义与绑定

maven-core.jar的META-INF/plexus/components.xml中为三个内置的生命周期分配了phase,配置的内容是:

<lifecycles>
          <lifecycle>
            <id>default</id>
            <!-- START SNIPPET: lifecycle -->
            <phases>
              <phase>validate</phase>
              <phase>initialize</phase>
              <phase>generate-sources</phase>
              <phase>process-sources</phase>
              <phase>generate-resources</phase>
              <phase>process-resources</phase>
              <phase>compile</phase>
              <phase>process-classes</phase>
              <phase>generate-test-sources</phase>
              <phase>process-test-sources</phase>
              <phase>generate-test-resources</phase>
              <phase>process-test-resources</phase>
              <phase>test-compile</phase>
              <phase>process-test-classes</phase>
              <phase>test</phase>
              <phase>package</phase>
              <phase>pre-integration-test</phase>
              <phase>integration-test</phase>
              <phase>post-integration-test</phase>
              <phase>verify</phase>
              <phase>install</phase>
              <phase>deploy</phase>
            </phases>
            <!-- END SNIPPET: lifecycle -->
          </lifecycle>
          <lifecycle>
            <id>clean</id>
            <phases>
              <phase>pre-clean</phase>
              <phase>clean</phase>
              <phase>post-clean</phase>
            </phases>
            <default-phases>
              <clean>org.apache.maven.plugins:maven-clean-plugin:clean</clean>
            </default-phases>
          </lifecycle>
          <lifecycle>
            <id>site</id>
            <phases>
              <phase>pre-site</phase>
              <phase>site</phase>
              <phase>post-site</phase>
              <phase>site-deploy</phase>
            </phases>
            <default-phases>
              <site>org.apache.maven.plugins:maven-site-plugin:site</site>
              <site-deploy>org.apache.maven.plugins:maven-site-plugin:deploy</site-deploy>
            </default-phases>
          </lifecycle>
        </lifecycles>

<lifecycle><phases /></lifecycle>定义了生命周期拥有的阶段。

<lifecycle><default-phases /></lifecycle>定义了该生命周期默认绑定的阶段,即默认要使用的阶段。

从上述配置中,可以看出有三个内置的生命周期:default、clean、site。其中clean、site都指定了默认要使用的阶段。Default生命周期并没有指定默认要使用的阶段,因为它是由packaging方式决定的。

在项目中使用生命周期

上面是对Maven中的内置生命周期的说明。下面就来说说,在项目中使用packaging\plugin将goal绑定到生命周期中。

<packaging>

每个Maven项目的pom文件中,必定要配置一个<packaging>(如果不配置,默认值是jar),每一种packaging方式,其实都会将某些goal绑定到default 生命周期多个不同的阶段中。

在【内置生命周期阶段定义】的配置中,其中clean生命周期中,默认只使用了clean阶段,没有使用pre-clean,post-clean阶段。同样的,在site生命周期中,默认使用了site阶段和site-deploy阶段。上面的配置中,并没有为default生命周期指定默认的阶段。因为这个要根据packaging方式来动态绑定的。

下面就看看Packaging方式与内置的生命周期的绑定:

pom方式

<phases>
              <package>org.apache.maven.plugins:maven-site-plugin:attach-descriptor</package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

jar 方式

<phases>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
              <process-test-resources>
                org.apache.maven.plugins:maven-resources-plugin:testResources
              </process-test-resources>
              <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
              <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
              <package>
                org.apache.maven.plugins:maven-jar-plugin:jar
              </package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

maven-plugin方式

<phases>
              <generate-resources>org.apache.maven.plugins:maven-plugin-plugin:descriptor</generate-resources>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
              <process-test-resources>
                org.apache.maven.plugins:maven-resources-plugin:testResources
              </process-test-resources>
              <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
              <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
              <package>
                org.apache.maven.plugins:maven-jar-plugin:jar,
                org.apache.maven.plugins:maven-plugin-plugin:addPluginArtifactMetadata
              </package>
              <install>
                org.apache.maven.plugins:maven-install-plugin:install
              </install>
              <deploy>
                org.apache.maven.plugins:maven-deploy-plugin:deploy
              </deploy>
</phases>

ejb方式

<phases>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
              <process-test-resources>
                org.apache.maven.plugins:maven-resources-plugin:testResources
              </process-test-resources>
              <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
              <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
              <package>
                org.apache.maven.plugins:maven-ejb-plugin:ejb
              </package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

war方式

<phases>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
              <process-test-resources>
                org.apache.maven.plugins:maven-resources-plugin:testResources
              </process-test-resources>
              <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
              <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
              <package>org.apache.maven.plugins:maven-war-plugin:war</package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

ear方式

<phases>
              <generate-resources>
                org.apache.maven.plugins:maven-ear-plugin:generate-application-xml
              </generate-resources>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <package>org.apache.maven.plugins:maven-ear-plugin:ear</package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

rar方式

<phases>
              <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
              <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
              <process-test-resources>
                org.apache.maven.plugins:maven-resources-plugin:testResources
              </process-test-resources>
              <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
              <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
              <package>org.apache.maven.plugins:maven-rar-plugin:rar</package>
              <install>org.apache.maven.plugins:maven-install-plugin:install</install>
              <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

par方式

<phases>
          <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
          <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
          <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources>
          <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
          <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
          <package>
            org.apache.maven.plugins:maven-par-plugin:par
          </package>
          <install>org.apache.maven.plugins:maven-install-plugin:install</install>
          <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
</phases>

ejb3方式

<phases>
          <process-resources>org.apache.maven.plugins:maven-resources-plugin:resources</process-resources>
          <compile>org.apache.maven.plugins:maven-compiler-plugin:compile</compile>
          <process-test-resources>org.apache.maven.plugins:maven-resources-plugin:testResources</process-test-resources>
          <test-compile>org.apache.maven.plugins:maven-compiler-plugin:testCompile</test-compile>
          <test>org.apache.maven.plugins:maven-surefire-plugin:test</test>
          <package>
            org.apache.maven.plugins:maven-ejb3-plugin:ejb3
          </package>
          <install>org.apache.maven.plugins:maven-install-plugin:install</install>
          <deploy>org.apache.maven.plugins:maven-deploy-plugin:deploy</deploy>
        </phases>

Plugins

上面是关于生命周期与pharse绑定关系的,其实在每一个阶段,要做哪些事情,完全是由plugin来完成的,一个plugin中会拥有多个goal,其实最终执行的都是plugin中的一个或者多个goal。

如果你想要改变插件的默认的行为,或者你想添加自己的插件的goal到指定的阶段。可以在相应Plugin中进行配置的。

clean插件的有两个goal: clean, help。
默认情况下,clean插件的clean goal是在clean阶段执行的,help goal是不会执行的。现在想让clean goal执行之前,先执行help goal。那么就需要在pre-clean阶段就执行help goal,就需要如下配置了:
在pluginManagement中把clean插件的help goal 添加到pre-clean阶段。
<pluginManagement>
<plugins>
<plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                    <configuration>
                        <indentSize>4</indentSize>
                        <retryOnError>true</retryOnError>
                        <verbose>true</verbose>
                    </configuration>
                    <executions>
                        <execution>
                            <id>auto-clean</id>
                            <phase>pre-clean</phase>
                            <goals>
                                <goal>help</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

引用指定的插件:
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
            </plugin>
        </plugins>

如此一来,使用mvn clean执行时,就会先执行help(pre-clean阶段执行),再执行clean(clean 阶段执行),下面就是测试结果:

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.0.0:help (auto-clean) @ helloworld ---
[INFO] Apache Maven Clean Plugin 3.0.0
    The Maven Clean Plugin is a plugin that removes files generated at
    build-time in a project‘s directory.

This plugin has 2 goals:

clean:clean
    Goal which cleans the build.
    This attempts to clean a project‘s working directory of the files that were
    generated at build-time. By default, it discovers and deletes the
    directories configured in project.build.directory,
    project.build.outputDirectory, project.build.testOutputDirectory, and
    project.reporting.outputDirectory.

    Files outside the default may also be included in the deletion by
    configuring the filesets tag.

clean:help
    Display help information on maven-clean-plugin.
    Call mvn clean:help -Ddetail=true -Dgoal=<goal-name> to display parameter
    details.

[INFO]
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ helloworld ---
[INFO] Deleting E:\workspaces\workspace_maven3\helloworld\target
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven\helloworld\HelloTest.class
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven\helloworld
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn\maven
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com\fjn
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes\com
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\test-classes
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\surefire-reports\TEST-com.fjn.maven.helloworld.HelloTest.xml
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\surefire-reports\com.fjn.maven.helloworld.HelloTest.txt
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\surefire-reports
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile\inputFiles.lst
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile\createdFiles.lst
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile\default-testCompile
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\testCompile
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile\inputFiles.lst
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile\createdFiles.lst
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile\default-compile
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin\compile
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status\maven-compiler-plugin
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-status
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\maven-archiver\pom.properties
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\maven-archiver
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\helloworld-0.0.1-SNAPSHOT.jar
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\jdbc.properties
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld\HelloImpl.class
[INFO] Deleting file E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld\Hello.class
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven\helloworld
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn\maven
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com\fjn
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes\com
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target\classes
[INFO] Deleting directory E:\workspaces\workspace_maven3\helloworld\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.828 s
[INFO] Finished at: 2016-01-26T09:43:33+08:00
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
时间: 2024-10-19 06:58:52

Maven Lifecycle的相关文章

maven:log4j:WARN No appenders could be found for logger (loggerInfo).或者maven build error:org.apache.maven.lifecycle.LifecycleExecutionExceptio

maven在build构建时,加载资源文件时需要配置资源文件插件: 1,在pom.xml文件中加入 <build> <finalName>${project.build.target.file.name}</finalName> <directory>${basedir}/target</directory>  <sourceDirectory>${basedir}/src/main/java</sourceDirectory&

16)maven lifecycle

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

dbdeploy的maven插件使用

Introduction 简介 The maven plugin is designed for people who use Apache Maven as a build tool. maven插件的设计是被人们当初maven构建工具使用. As well as this maven plugin, dbdeploy supports an ant task and a command line interface. 以及这个Maven插件,dbdeploy支持ant任务 和 命令行接口 U

Maven的几个核心概念

POM (Project Object Model) 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java 1.5 来编译. 示例的 POM: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

Apache Maven 入门篇(下)

第一篇文章大概的介绍了一下Apache Maven以及它的下载和安装,并且运行了一个简单的示例.那么在对maven有了一点接触后,接下去的一步是要了解maven的核心概念,这样才能在使用maven的时候游刃有余. 接下来我们介绍下面这几个核心概念: POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依赖管理 Maven 库 POM (Project Object Model) 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字

Maven的POM.xml详解(一)

原文:http://maven.apache.org/pom.html POM是什么? POM是"Project ObjectModel"的首字母缩写,即工程对象模型.它在一个名叫pom.xml的文件中使用XML来表示一个Maven工程.在跟使用Maven的人们一起谈论工程时,工程的概念有些哲学的意义,它不仅仅是一个包含代码的文件的集合.一个工程包含了配置文件.相关的开发者和他们的角色.缺陷跟踪系统.组织机构和授权许可.工程的URL.工程依赖.以及所有的其他的对代码发挥作用的微笑片段.

Maven内部运行原理

原作者zlwen 原文链接 内部运行原理(二) maven至今还是Java编程语言构建的事实标准,大部分项目还在使用maven来进行构建,因此了解maven内部运行的原理对定位和分析问题还是很有裨益的.本篇文章主要介绍一些maven内部运行过程中的一些基本概念,相信看完后,对那么些刚刚接触maven的读者来说maven将不再陌生.??在具体分析项目构建的过程前,需要了解maven的一些基本概念,这些概念十分重要,请务必理解清楚后再看下文.基本概念主要有:POM,Lifecycle.这两个概念又会

maven是什么?(转自oracle官网)

Maven 是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能.所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要.Maven 使用惯例优于配置的原则 .它要求在没有定制之前,所有的项目都有如下的结构: 目录 目的 ${basedir} 存放 pom.xml和所有的子目录 ${basedir}/src/main/java 项目的 java源代码 ${basedir}/src/main/resources 项目的资源,比如说 property文件

maven 的几个重要的配置文件:super pom &amp; 生命周期默认绑定的配置文件

这几个配置文件都在 maven 的 安装 目录的 lib 目录中 1. super pom. 在 maven-model-builder-{version}.jar 包的 org/apache/maven/model 中的 pom-4.0.0.xml 这个文件 所有的pom 文件都继承该 pom 文件.摘抄如下: 1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!-- 4 Licensed to the