maven生命周期和插件详解

常用命令

mvn clean
mvn compile
mvn test
mvn package
mvn install
mvn install -Dmaven.test.skip=true
mvn deploy
mvn help:system

用户属性Properties的使用

用户可以在properties中自定义一些用户属性,然后可以在其他地方使用${属性名称}这种方式进行引用。

<properties>
    <spring.group>org.springframework</spring.group>
    <spring.version>5.2.1.RELEASE</spring.version>
</properties>

 <dependency>
        <groupId>${spring.group}</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring.version}</version>
</dependency>

生命周期

我们开发一个项目的时候,通常有这些环节:创建项目、编写代码、清理已编译的代码、编译代码、执行单元测试、打包、集成测试、验证、部署、生成站点等,这些环节组成了项目的生命周期,这些过程也叫做项目的构建过程

maven中生命周期详解

maven将项目的生命周期抽象成了3套生命周期,每套生命周期又包含多个阶段,每套中具体包含哪些阶段是maven已经约定好的,但是每个阶段具体需要做什么,是用户可以自己指定的

maven中定义的3套生命周期:

  • clean生命周期
  • default生命周期
  • site生命周期

每套生命周期中有多个阶段,每套中的多个阶段是有先后顺序的,并且后面的阶段依赖于前面的阶段,而用户可以直接使用mvn命令来调用这些阶段去完成项目生命周期中具体的操作

mvn 生命周期阶段

maven中的3套生命周期相当于maven定义了3个类来解决项目生命周期中需要的各种操作,每个类中有多个方法,这些方法就是指具体的阶段,方法名称就是阶段的名称,每个类的方法是有顺序的,当执行某个方法的时候,这个方法前面的方法也会执行。具体每个方法中需要执行什么,这个是通过插件的方式让用户去配置的,所以非常灵活。
用户执行mvn 阶段名称就相当于调用了具体的某个方法.

clean生命周期

在这里插入图片描述
用户可以通过mvn pre-clean来调用clean生命周期中的pre-clean阶段需要执行的操作。

调用mvn post-clean会执行上面3个阶段所有的操作,上文中有说过,每个生命周期中的后面的阶段会依赖于前面的阶段,当执行某个阶段的时候,会先执行其前面的阶段.

default生命周期

这个是maven主要的生命周期,主要被用于构建应用,包含了23个阶段
在这里插入图片描述

site生命周期

site生命周期的目的是建立和发布项目站点,Maven能够基于pom.xml所包含的信息,自动生成一个友好的站点,方便团队交流和发布项目信息。主要包含以下4个阶段:
在这里插入图片描述

mvn命令和生命周期
mvn 阶段1 [阶段2] [阶段n]

多个阶段的名称之间用空格隔开。

mvn clean deploy

这个命令也比较常用,会先按顺序执行clean生命周期的[pre-clean,clean]这个闭区间内所有的阶段,然后按序执行default生命周期[validate,deploy]这个闭区间内的所有阶段(也就是default生命周期中的所有阶段)。这个命令内部包含了清理上次构建的结果、编译代码、运行单元测试、打包、将打好的包安装到本地仓库、将打好的包发布到私服仓库。

项目pom.xml中常用配置

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 配置maven编译的时候采用的编译器版本 -->
        <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        <!-- 指定源代码是什么版本的,如果源码和这个版本不符将报错,maven中执行编译的时候会用到这个配置,默认是1.5,这个相当于javac命令后面的-source参数 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <!-- 该命令用于指定生成的class文件将保证和哪个版本的虚拟机进行兼容,maven中执行编译的时候会用到这个配置,默认是1.5,这个相当于javac命令后面的-target参数 -->
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

maven 的每个阶段具体做的事情是由maven插件来完成的。

有很多类似于maven-xxxx-plugin:版本:xxx这样的内容,这个就是表示当前在运行这个插件来完成对应阶段的操作,mvn 阶段明明执行的是阶段,但是实际输出中确实插件在干活,那么阶段是如何和插件关联起来的呢?插件又是什么呢?

Maven插件

maven插件主要是为maven中生命周期中的阶段服务的,maven中只是定义了3套生命周期,以及每套生命周期中有哪些阶段,具体每个阶段中执行什么操作,完全是交给插件去干的

插件可以通过mvn命令的方式调用直接运行,或者将插件和maven生命周期的阶段进行绑定,然后通过mvn 阶段的方式执行阶段的时候,会自动执行和这些阶段绑定的插件。

插件目标

每个插件中可能为了代码可以重用,一个插件可能包含了多个功能,比如编译代码的插件,可以编译源代码、也可以编译测试代码;插件中的每个功能就叫做插件的目标(Plugin Goal),每个插件中可能包含一个或者多个插件目标(Plugin Goal)。

目标参数

插件目标是用来执行任务的,那么执行任务肯定是有参数配的,这些就是目标的参数,每个插件目标对应于java中的一个类,参数就对应于这个类中的属性

列出插件所有目标:
mvn 插件goupId:插件artifactId[:插件version]:help
mvn 插件前缀:help

例如:

mvn org.apache.maven.plugins:maven-clean-plugin:help

输出内容如下:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  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.

上面列出了maven-clean-plugin这个插件所有的目标,有2个,分别是clean:clean、clean:help,分号后面的部分是目标名称,分号前面的部分是插件的前缀,每个目标的后面包含对这个目标的详细解释说明

查看插件目标参数列表
mvn 插件goupId:插件artifactId[:插件version]:help -Dgoal=目标名称 -Ddetail
mvn 插件前缀:help -Dgoal=目标名称 -Ddetail

上面命令中的-Ddetail用户输出目标详细的参数列表信息,如果没有这个,目标的参数列表不会输出出来

例如:

mvn org.apache.maven.plugins:maven-clean-plugin:help -Dgoal=help -Ddetail

输出内容:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  The Maven Clean Plugin is a plugin that removes files generated at build-time
  in a project's directory.

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

  Available parameters:

    detail (Default: false)
      If true, display all settable properties for each goal.
      Expression: ${detail}

    goal
      The name of the goal for which to show help. If unspecified, all goals
      will be displayed.
      Expression: ${goal}

    indentSize (Default: 2)
      The number of spaces per indentation level, should be positive.
      Expression: ${indentSize}

    lineLength (Default: 80)
      The maximum length of a display line, should be positive.
      Expression: ${lineLength}

上面列出了clean插件的help目标的详细参数信息

注意上面参数详细参数说明中有Expression: ${xxx}这样的部分,这种表示给这个运行的目标传参,可以通过mvn -Dxxx这种方式传参,xxx为${xxx}中的xxx部分,这个xxx有时候和目标参数的名称不一致,所以这点需要注意,运行带参数的目标

例如:执行 目标任务 -Dgoal=help 传入参数 -Ddetail=false

mvn org.apache.maven.plugins:maven-clean-plugin:help -Dgoal=help -Ddetail=false

输出内容:

[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven-chat06 ---
[INFO] org.apache.maven.plugins:maven-clean-plugin:2.5

Maven Clean Plugin
  The Maven Clean Plugin is a plugin that removes files generated at build-time
  in a project's directory.

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

上面传了一个detail=false,上面未输出目标的详细参数信息

命令行运行插件

mvn 插件goupId:插件artifactId[:插件version]:插件目标 [-D目标参数1] [-D目标参数2] [-D目标参数n]
mvn 插件前缀:插件目标  [-D目标参数1] [-D目标参数2] [-D目标参数n]
案例
插件有哪些目标
mvn org.apache.maven.plugins:maven-surefire-plugin:help

输出内容:

[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven-chat06 ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

This plugin has 2 goals:

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

surefire:test
  Run tests using Surefire.

maven-surefire-plugin插件有2个目标help和test,描述中可以看出test目标是用来运行测试用例的。

test目标对应的参数列表
mvn org.apache.maven.plugins:maven-surefire-plugin:help -Dgoal=test -Ddetail=true

输出内容:

[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven-chat06 ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

surefire:test
  Run tests using Surefire.

  Available parameters:

    skip (Default: false)
      Set this to 'true' to bypass unit tests entirely. Its use is NOT
      RECOMMENDED, especially if you enable it using the 'maven.test.skip'
      property, because maven.test.skip disables both running the tests and
      compiling the tests. Consider using the skipTests parameter instead.

看一下skip这个参数说明,这个参数默认是false,如果设置为true的时候,项目将跳过测试代码的编译和测试用例的执行,可以maven.test.skip这个属性来进行命令行传参,将其传递给test目标的skip参数,这个通过-D传递的参数名称就和目标参数名称不一样了,所以需要注意-D后面并不一定是参数名称。

先看一下不加参数的效果

mvn org.apache.maven.plugins:maven-surefire-plugin:test

输出内容:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

maven.skip.test=true 时,将其传递给test目标的skip参数,来设置 skip 的值,效果如下:

mvn org.apache.maven.plugins:maven-surefire-plugin:test -Dmaven.test.skip=true

输出内容:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

对比一下上面2个输出,下面的多了一行如下

[INFO] Tests are skipped.

说明跳过了测试的执行。

插件传参的2种方式

一种通过-D后面跟用户属性的方式给用户传参,还有一种方式,在pom.xml中properties的用户自定义属性中进行配置,properties中加入

<maven.test.skip>true</maven.test.skip>

执行命令

mvn org.apache.maven.plugins:maven-surefire-plugin:test

输出内容

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.javacode2018:maven-chat06 >--------------------
[INFO] Building maven-chat06 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven-chat06 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS

输出中也有Tests are skipped.,说明也跳过了测试,和-Dmaven.test.skip=true效果一样。

插件目标是如何和生命周期关联起来的呢?

获取插件目标详细描述信息的另外一种方式

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目标名称 -Ddetail
mvn help:describe -Dplugin=插件前缀 -Dgoal=目标名称 -Ddetail

上面这个命令调用的是help插件的describe这个目标,这个目标可以列出其他指定插件目标的详细信息

执行命令

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin -Dgoal=test -Ddetail

输出内容

[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-chat06 ---
[INFO] Mojo: 'surefire:test'
surefire:test
  Description: Run tests using Surefire.
  Implementation: org.apache.maven.plugin.surefire.SurefirePlugin
  Language: java
  Bound to phase: test

  Available parameters:

    additionalClasspathElements
      Additional elements to be appended to the classpath.

    argLine
      User property: argLine
      Arbitrary JVM options to set on the command line.

    skip (Default: false)
      User property: maven.test.skip
      Set this to 'true' to bypass unit tests entirely. Its use is NOT
      RECOMMENDED, especially if you enable it using the 'maven.test.skip'
      property, because maven.test.skip disables both running the tests and
      compiling the tests. Consider using the skipTests parameter instead.

和上面获取插件目标参数详情列表对比一下,上面这个更详细一些,参数说明中多了一行User property: 属性名称,这个属性名称可以通过两种方式传递

  • mvn命令-D属性名称的方式传递
  • pom.xml中properties中定义的方式指定。

现在可以知道我们一直用的-Dmaven.test.skip为什么可以跳过测试代码的编译和单元测试的执行了吧。(通过-D 传入参数,来控制目标的参数的属性值)

插件前缀

可以通过下面命令查看到插件的前缀:

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version]

输出内容如下:

Name: Maven Surefire Plugin
Description: Surefire is a test framework project.
Group Id: org.apache.maven.plugins
Artifact Id: maven-surefire-plugin
Version: 2.18.1
Goal Prefix: surefire

输出中的Goal Prefix:部分对应的就是插件的前缀,上面这个插件的前缀是surefire

使用前缀来运行插件
mvn surefire:test

上面通过别名来运行插件maven-surefire-plugin的test目标

上面用了很多mvn help:这个命令,这个调用的是maven-help-plugin插件的功能,help是插件的前缀,它的座标是:

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-help-plugin</artifactId>
    <version>3.2.0</version>
</dependency>

插件和生命周期阶段绑定

maven只是定义了生命周期中的阶段,而没有定义每个阶段中具体的实现,这些实现是由插件的目标来完成的,所以需要将阶段和插件目标进行绑定,来让插件目标帮助生命周期的阶段做具体的工作,生命周期中的每个阶段支持绑定多个插件的多个目标。

当我们将生命周期中的阶段和插件的目标进行绑定的时候,执行mvn 阶段就可以执行和这些阶段绑定的插件目标

maven内置插件以及绑定

maven内部已经提供了很多默认的插件,而将一些阶段默认和这些插件阶段绑定好了,所以我们不用做任何配置就可以执行清理代码、编译代码、测试、打包、安装到本地仓库、上传到远程仓库等阶段的操作,是因为maven已经默认给这些阶段绑定好了插件目标,所以不需要我们再去配置,就直接可以运行,这些都是maven内置绑定帮我们做的事情

maven内置绑定

clean生命周期阶段与插件绑定关系
在这里插入图片描述

clean周期中只有clean阶段默认绑定了maven-clean-plugin插件的clean目标maven-clean-plugin插件的clean目标作用就是删除项目的输出目录。

default生命周期阶段与插件绑定关系

在这里插入图片描述

site生命周期阶段与插件绑定关系
在这里插入图片描述

mvn clean

[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ maven-application ---

这个表示调用的插件是:maven-clean-plugin,版本是:2.5,插件的目标是:clean

mvn test
该命令调用default生命周期的test阶段,实际上会从default生命周期的第一个阶段(validate)开始执行一直到test阶段结束
mvn test会调用下面一些插件的目标:

maven-resources-plugin:resources
maven-compiler-plugin:compile
maven-resources-plugin:testResources
maven-compiler-plugin:testCompile
maven-surefile-plugin:test
自定义绑定

除了默认绑定的一些操作,我们自己也可以将一些阶段绑定到指定的插件目标上来完成一些操作,这种自定义绑定让maven项目在构件的过程中可以执行更多更丰富的操作。

常见的一个案例是:创建项目的源码jar包,将其安装到本地仓库中,内置插件绑定关系中没有涉及到这一步的任务,所以需要用户自己配置。

插件maven-source-plugin的jar-no-fork可以帮助我们完成该任务,我们将这个目标绑定在default生命周期的verify阶段上面,这个阶段没有任何默认绑定,verify是在测试完成之后并将构件安装到本地仓库之前执行的阶段,在这个阶段我们生成源码,配置如下:

 <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <!-- 使用插件需要执行的任务 -->
                    <execution>
                        <!-- 任务id -->
                        <id>attach-source</id>
                        <!-- 任务中插件的目标,可以指定多个 -->
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                        <!-- 绑定的阶段 -->
                        <phase>verify</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

执行命令

mvn install

输出内容如下:

[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[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:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-rpc ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\gitproject\maven-demo\maven-rpc\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-rpc ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-rpc ---
[INFO] Building jar: E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-source) @ maven-rpc ---
[INFO] Building jar: E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar
[INFO] 
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-rpc ---
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT.jar
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\pom.xml to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT.pom
[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看出调用了我们配置的插件生成源码jar,上面的括号中的attach-source就是pom.xml中配置的任务id。

[INFO] Installing E:\gitproject\maven-demo\maven-rpc\target\maven-rpc-1.0-SNAPSHOT-sources.jar to F:\JDMavenRepository\com\myke\maven-rpc\1.0-SNAPSHOT\maven-rpc-1.0-SNAPSHOT-sources.jar

可以看到将源码安装到本地仓库了。

怎么查看插件的默认绑定呢?
mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目标名称 -Ddetail
mvn help:describe -Dplugin=插件前缀 -Dgoal=目标名称 -Ddetail

我们看一下插件source的jar-no-fork目标默认的绑定:

$ mvn help:describe -Dplugin=source -Dgoal=jar-no-fork -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-rpc ---
[INFO] Mojo: 'source:jar-no-fork'
source:jar-no-fork
  Description: This goal bundles all the sources into a jar archive. This
    goal functions the same as the jar goal but does not fork the build and is
    suitable for attaching to the build lifecycle.
  Implementation: org.apache.maven.plugins.source.SourceJarNoForkMojo
  Language: java
  Bound to phase: package

  Available parameters:

    archive
      The archive configuration to use. See Maven Archiver Reference.
      Note: Since 3.0.0 the resulting archives contain a maven descriptor. If
      you need to suppress the generation of the maven descriptor you can
      simply achieve this by using the archiver configuration..

    attach (Default: true)
      User property: maven.source.attach
      Specifies whether or not to attach the artifact to the project

    classifier (Default: sources)
      User property: maven.source.classifier
      (no description available)

    defaultManifestFile (Default:
    ${project.build.outputDirectory}/META-INF/MANIFEST.MF)
      Required: true
      Path to the default MANIFEST file to use. It will be used if
      useDefaultManifestFile is set to true.

    excludeResources (Default: false)
      User property: maven.source.excludeResources
      Specifies whether or not to exclude resources from the sources-jar. This
      can be convenient if your project includes large resources, such as
      images, and you don't want to include them in the sources-jar.

    excludes
      List of files to exclude. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    finalName (Default: ${project.build.finalName})
      The filename to be used for the generated archive file. For the
      source:jar goal, '-sources' is appended to this filename. For the
      source:test-jar goal, '-test-sources' is appended.

    forceCreation (Default: false)
      User property: maven.source.forceCreation
      Whether creating the archive should be forced. If set to true, the jar
      will always be created. If set to false, the jar will only be created
      when the sources are newer than the jar.

    includePom (Default: false)
      User property: maven.source.includePom
      Specifies whether or not to include the POM file in the sources-jar.

    includes
      List of files to include. Specified as fileset patterns which are
      relative to the input directory whose contents is being packaged into the
      JAR.

    outputDirectory (Default: ${project.build.directory})
      The directory where the generated archive file will be put.

    outputTimestamp (Default: ${project.build.outputTimestamp})
      Timestamp for reproducible output archive entries, either formatted as
      ISO 8601 yyyy-MM-dd'T'HH:mm:ssXXX or as an int representing seconds since
      the epoch (like SOURCE_DATE_EPOCH).

    skipSource (Default: false)
      User property: maven.source.skip
      A flag used to disable the source procedure. This is primarily intended
      for usage from the command line to occasionally adjust the build.

    useDefaultExcludes (Default: true)
      User property: maven.source.useDefaultExcludes
      Exclude commonly excluded files such as SCM configuration. These are
      defined in the plexus FileUtils.getDefaultExcludes()

    useDefaultManifestFile (Default: false)
      User property: maven.source.useDefaultManifestFile
      Set this to true to enable the use of the defaultManifestFile.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

上面输出中有个Bound to phase: package,表示默认绑定在了package阶段上。

我们可以在default生命周期的第一个阶段validate绑定清理代码的插件,那我们来通过自定义绑定来实现一下,project->build->plugins元素中加入下面配置:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <!-- 使用插件需要执行的任务 -->
        <execution>
            <!-- 任务中插件的目标,可以指定多个 -->
            <id>clean-target</id>
            <goals>
                <goal>clean</goal>
            </goals>
            <!-- 绑定的阶段 -->
            <phase>validate</phase>
        </execution>
    </executions>
</plugin>

执行命令

$ mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

输出中有:

[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target

这个表示运行了代码清理的功能,进行了代码清理.相当于 mvn clean compile

POM.xml插件配置详解
插件目标共享参数配置

build->plugins->plugin中配置:

<!-- 插件参数配置,对插件中所有的目标起效 -->
<configuration>
    <目标参数名>参数值</目标参数名>
</configuration>

configuration节点下配置目标参数的值,节点名称目标的参数名称,上面这种配置对当前插件的所有目标起效,也就是说这个插件中所有的目标共享此参数配置。

案例
          <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <!-- 插件参数配置,对插件中所有的目标起效 -->
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>

执行命令

$ mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-rpc ---
[INFO] Deleting E:\gitproject\maven-demo\maven-rpc\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-rpc ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\gitproject\maven-demo\maven-rpc\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-rpc ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory E:\gitproject\maven-demo\maven-rpc\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven-rpc ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看到Test are skipped,说明跳过了测试,到此为止,跳过测试已经讲了3种了:

1. mvn -Dmaven.test.skip=tue
2. properties中配置<maven.test.skip>true</maven.test.skip>
3. build中配置插件参数的方式

上面这个配置参数方式对当前插件的所有目标有效,如果想对指定的目标进行配置呢,用下面的方式。

对指定的目标参数配置

project->build->plugins->plugin->executions->execution元素中进行配置,如下:

<!-- 这个地方配置只对当前任务有效 -->
<configuration>
    <目标参数名>参数值</目标参数名>
</configuration>

上面这种配置常用于自定义插件绑定,只对当前任务有效。

案例
<!--跳过测试用例:对指定的目标参数配置-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                            <goal>help</goal>
                        </goals>
                        <phase>pre-clean</phase>
                        <!-- 这个地方配置只对当前任务有效 -->
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

上面自定义了一个绑定,在clean周期的pre-clean阶段绑定了插件maven-surefire-plugin的两个目标test和helpexecution元素没有指定id,所以默认id是default

执行命令

$ mvn pre-clean
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maven-rpc 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default) @ maven-rpc ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:help (default) @ maven-rpc ---
[INFO] Maven Surefire Plugin 2.12.4
  Surefire is a test framework project.

This plugin has 2 goals:

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

surefire:test
  Run tests using Surefire.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

可以看到上面输出中运行了插件的两个目标

获取maven插件信息

可以通过下面命令获取插件详细介绍信息

mvn help:describe -Dplugin=插件goupId:插件artifactId[:插件version] -Dgoal=目标名称 -Ddetail
mvn help:describe -Dplugin=插件前缀 -Dgoal=目标名称 -Ddetail

更多maven插件的帮助文档可以参考maven的官方网站,上面有详细的介绍,地址:

http://maven.apache.org/plugins/
插件解析机制

为了方便用户使用和配置插件,maven不需要用户提供完整的插件座标信息,就可以解析到正确的插件,不过我建议使用插件配置的时候最好还是配置完整的座标信息.

插件仓库

插件的是在pluginRepositories->pluginRepository元素中配置的,如下:

<pluginRepositories>
    <pluginRepository>
        <id>myplugin-repository</id>
        <url>http://repo1.maven.org/maven2/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

插件的默认groupId
pom.xml中配置插件的时候,如果是官方的插件,可以省略groupId

编译代码插件

 <!--编译代码-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

上面这个插件用于编译代码的,编译代码的时候需要指定编译器的版本,源码的版本,目标代码的版本,都是用的是1.8。

插件前缀的解析

使用mvn命令调用插件的时候,可以使用插件的前缀来代替繁琐的插件座标的方式,那么maven是如何根据插件的前缀找到对应的插件的呢?

插件前缀与插件groupId:artifactId是一一对应的关系,这个关系的配置存储在仓库的元数据中。目录:repository/org/apache/maven/plugins/查看 maven-metadata xml文件,
在这里插入图片描述

查看项目最终pom.xml文件

我们的pom.xml默认会继承maven顶级的一个父类pom.xml,顶级的pom.xml中指定了很多默认的配置,如生命周期中的阶段和很多插件的绑定,这些如果我们想看到,到哪里看呢?

mvn命令在项目中执行的时候,我们的pom.xml和父类的pom.xml最终会进行合并,当我们的pom.xml写的比较复杂的时候,最终合并之后是什么效果呢,我们可以通过下面这个命令查看:

mvn help:effective-pom

输出部分内容如下:

<build>
    <sourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\main\java</sourceDirectory>
    <scriptSourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>E:\gitproject\maven-demo\maven-rpc\src\test\java</testSourceDirectory>
    <outputDirectory>E:\gitproject\maven-demo\maven-rpc\target\classes</outputDirectory>
    <testOutputDirectory>E:\gitproject\maven-demo\maven-rpc\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>E:\gitproject\maven-demo\maven-rpc\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>E:\gitproject\maven-demo\maven-rpc\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>E:\gitproject\maven-demo\maven-rpc\target</directory>
    <finalName>maven-rpc-1.0-SNAPSHOT</finalName>

参考

maven生命周期和插件详解
maven插件的帮助文档

发布了19 篇原创文章 · 获赞 2 · 访问量 1420
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章