theboyaply
theboyaply
发布于 2020-08-02 / 1030 阅读
0
0

6-maven生命周期和插件详解

原文链接:https://mp.weixin.qq.com/s/D-aVG1dRqQPzmFKfi00KfA

properties元素

在 pom.xml 中,我们也可以将一些重复的值提取出来,然后在需要的地方引用。比如:

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.2.1.RELEASE</version>
    </dependency>
</dependencies>

上面依赖中的 groupIdversion 都一样,因此可以提取出来,像下面这样:

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

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

properties位于pom.xml中的,是project元素的子元素,用户可以在properties中自定义一些用户属性,然后可以在其他地方使用${属性名称}这种方式进行引用。

生命周期介绍

在使用maven的时候,我们经常用到如下命令:

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

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

maven约定好了项目的结构,源码的位置、资源文件的位置、测试代码的位置、测试用到的资源文件的位置、静态资源的位置、打包之后文件的位置等,这些都是maven约定好的,所以清理代码用一个命令mvn clean就可以完成,不需要我们去配置清理的目标目录;用mvn compile命令就可以完成编译的操作;用mvn test就可以自动运行测试用例;用mvn package就可以将项目打包为jar、war格式的包,能够如此简单,主要还是maven中约定大于配置的结果。

maven生命周期

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

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

  1. clean生命周期
  2. default生命周期
  3. site生命周期

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

mvn 阶段名称

通俗点解释:

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

用户执行mvn 阶段名称就相当于调用了具体的某个方法。

下面我们来看看每个生命周期中有哪些阶段(也就是我们说的每个类中有哪些方法,顺序是什么样的)。

clean生命周期

clean生命周期的目的是清理项目,它包含三个阶段:

生命周期阶段描述
pre-clean执行一些需要在clean之前完成的工作
clean移除所有上一次构建生成的文件
post-clean执行一些需要在clean之后立刻完成的工作

用户可以通过mvn pre-clean来调用clean生命周期中的pre-clean阶段需要执行的操作。

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

default生命周期

这个是maven主要的生命周期,主要被用于构建应用,包含了23个阶段:

生命周期阶段描述
validate校验:校验项目是否正确并且所有必要的信息可以完成项目的构建过程。
initialize初始化:初始化构建状态,比如设置属性值。
generate-sources生成源代码:生成包含在编译阶段中的任何源代码。
process-sources处理源代码:处理源代码,比如说,过滤任意值。
generate-resources生成资源文件:生成将会包含在项目包中的资源文件。
process-resources编译:复制和处理资源到目标目录,为打包阶段最好准备。
compile处理类文件:编译项目的源代码。
process-classes处理类文件:处理编译生成的文件,比如说对Java class文件做字节码改善优化。
generate-test-sources生成测试源代码:生成包含在编译阶段中的任何测试源代码。
process-test-sources处理测试源代码:处理测试源代码,比如说,过滤任意值。
generate-test-resources生成测试源文件:为测试创建资源文件。
process-test-resources处理测试源文件:复制和处理测试资源到目标目录。
test-compile编译测试源码:编译测试源代码到测试目标目录.
process-test-classes处理测试类文件:处理测试源码编译生成的文件。
test测试:使用合适的单元测试框架运行测试(Juint是其中之一)。
prepare-package准备打包:在实际打包之前,执行任何的必要的操作为打包做准备。
package打包:将编译后的代码打包成可分发格式的文件,比如JAR、WAR或者EAR文件。
pre-integration-test集成测试前:在执行集成测试前进行必要的动作。比如说,搭建需要的环境。
integration-test集成测试:处理和部署项目到可以运行集成测试环境中。
post-integration-test集成测试后:在执行集成测试完成后进行必要的动作。比如说,清理集成测试环境。
verify验证:运行任意的检查来验证项目包有效且达到质量标准。
install安装:安装项目包到本地仓库,这样项目包可以用作其他本地项目的依赖。
deploy部署:将最终的项目包复制到远程仓库中与其他开发者和项目共享。

site生命周期

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

阶段描述
pre-site执行一些需要在生成站点文档之前完成的工作
site生成项目的站点文档
post-site执行一些需要在生成站点文档之后完成的工作,并且为部署做准备
site-deploy将生成的站点文档部署到特定的服务器上

mvn命令和生命周期

从命令行执行maven任务的最主要方式就是调用maven生命周期的阶段,需要注意的是,每套生命周期是相互独立的,但是每套生命周期中阶段是有前后依赖关系的,执行某个的时候,会按序先执行其前面所有的。

mvn执行阶段的命令格式是:

mvn 阶段1 [阶段2] [阶段n]

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

比如我们常用到的:

mvn clean

该命令是调用clean生命周期的clean阶段,实际执行的阶段为clean生命周期中的pre-clean和clean阶段。

mvn test

该命令调用default生命周期的test阶段,实际上会从default生命周期的第一个阶段(validate)开始执行一直到test阶段结束。这里面包含了代码的编译,运行测试用例。

mvn clean install

这个命令中执行了两个阶段:cleaninstall,从上面3个生命周期的阶段列表中找一下,可以看出clean位于clean生命周期的表格中,install位于default生命周期的表格中,所以这个命令会先从clean生命周期中的pre-clean阶段开始执行一直到clean生命周期的clean阶段;然后会继续从default生命周期的validate阶段开始执行一直到default生命周期的install阶段。

这里面包含了清理上次构建的结果,编译代码,测试,打包,将打好的包安装到本地仓库。

mvn clean deploy

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

案例

创建项目

创建一个maven项目:跳转到-->创建maven项目

创建之后如下图:

maven05-16

配置idea的maven:

maven05-17

然后还原~/.m2/settings.xml的配置到初始状态,即maven默认的setting.xml配置。

maven项目是约定大于配置的,项目结构是按照maven的约定生成好的,关于maven约定项目结构,我们再来回顾一下。

Maven 提倡使用一个共同的标准目录结构,Maven 使用约定优于配置的原则,大家尽可能的遵守这样的目录结构,如下所示:

目录备注
$存放 pom.xml 和所有的子目录,即项目根目录
$/src/main/java项目的 java 源码
$/src/main/resources项目的资源,比如说 property 文件,spring.xml
$/src/test/java项目的测试类,比如说 junit 代码
$/src/test/resources测试用的资源
$/src/main/webapp/WEB-INFweb 应用文件目录,web 项目的信息,比如存放 web.xml、本地图片、jsp 视图页面
$/target打包输出目录
$/target/class编译输出目录
$/target/test-classes测试编译输出目录
Test.javamaven 只会自动运行符合该命名规则的测试类
~/.m2/repositorymaven 默认的本地仓库目录位置

具体文章可以参考这篇:约定配置

结合刚才项目的结构和这个表格领会一下,下面我们来感受一下执行生命周期中的阶段产生的效果。

修改pom.xml,如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.theboyaply</groupId>
    <artifactId>maven4</artifactId>
    <version>1.0</version>

    <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>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

上面properties这个元素中的配置,先略过,后面会详解。

创建一个Demo类,源码是放在src/main/java目录中,如下:

package com.theboyaply.maven;

/**
 * @author theboyaply
 * @description
 */
public class Demo {

    public static void main(String[] args) {
        System.out.println("Hello");
    }

}

mvn clean命令效果

在当前项目pom.xml所在目录中执行下面命令:

mvn clean

效果如下:

D:\myResource\maven\maven4>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.456 s
[INFO] Finished at: 2020-07-29T22:13:01+08:00
[INFO] ------------------------------------------------------------------------

上面有提到编译、打包的内容都放在target目录,看上面输出中有个Deleting target目录,说明mvn clean是对这个目录进行清理,这个目录中目前是空的。

mvn compile命令效果

此时你可以看一下项目结构,是没有 target 这个目录的,下面我们在项目根目录下执行编译命令:

mvn compile

效果如下:

D:\myResource\maven\maven4>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\myResource\maven\maven4\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.604 s
[INFO] Finished at: 2020-07-29T22:17:37+08:00
[INFO] ------------------------------------------------------------------------

可以看到上面有Compiling 1 source ....,这个是编译Demo.java,然后输出到了target中的classes目录,再来看一下项目的结构,如下图:

maven06-01

编译得到的 target 目录出现在了项目的根目录。

mvn clean package

效果如下:

D:\myResource\maven\maven4>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\myResource\maven\maven4\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven4\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven4 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven4 ---
[INFO] Building jar: D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.161 s
[INFO] Finished at: 2020-07-29T22:25:43+08:00
[INFO] ------------------------------------------------------------------------

从输出中看到,有个Building jar ...,生成了一个jar包,这个项目的pom.xml中的packaging元素没有指定值,那就取默认值jar,表示这个构件是一个jar包,mvn clean package先清理编译的代码,然后执行了default生命周期的package阶段,将项目打成了jar放在了target目录,如下图:

maven06-02

上面输出的信息还有很多,可以结合maven生命周期对比看看,理解一下。看完下面的内容后,上面的信息你就能理解了。

mvn clean install

D:\myResource\maven\maven4>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\myResource\maven\maven4\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven4\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven4 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven4 ---
[INFO] Building jar: D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven4 ---
[INFO] Installing D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven4\pom.xml to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.535 s
[INFO] Finished at: 2020-07-29T22:31:55+08:00
[INFO] ------------------------------------------------------------------------

上面的输出信息,与 mvn clean package 相比,主要多出了两行 Installing...。将项目打成jar包以及项目的pom文件放到本地仓库去了,也就是将构件打包安装到本地仓库了。

上面几个mvn命令的案例,都是通过mvn命令去执行了mvn中定义的生命周期中的阶段,然后完成了很多看似内部很复杂的操作。比如打包,内部包含很多复杂的操作,maven都帮我们屏蔽了,通过一个简单的mvn package就完成了。

上面也有说过,每个阶段具体做的事情是由maven插件来完成的。

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

注意以下所有命令都在cmd窗口执行,执行位置位于上面这个项目的pom.xml所在目录。

注意以下所有命令都在cmd窗口执行,执行位置位于上面这个项目的pom.xml所在目录。

注意以下所有命令都在cmd窗口执行,执行位置位于上面这个项目的pom.xml所在目录。

maven 插件

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

maven中的插件就相当于一些工具,比如编译代码的工具,运行测试用例的工具,打包代码的工具,将代码上传到本地仓库的工具,将代码部署到远程仓库的工具等等,这些都是maven中的插件。

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

插件目标

maven中的插件以jar的方式存在于仓库中,和其他构件是一样的,也是通过坐标进行访问,每个插件中可能为了代码可以重用,一个插件可能包含了多个功能,比如编译代码的插件,可以编译源代码、也可以编译测试代码;插件中的每个功能就叫做插件的目标(Plugin Goal),每个插件中可能包含一个或者多个插件目标(Plugin Goal)

插件目标参数

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

列出插件所有目标:

mvn 插件goupId:插件artifactId[:插件version]:help
mvn 插件前缀:help

插件前缀下面再讲。

比如:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-clean-plugin:help
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven4 ---
[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.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.625 s
[INFO] Finished at: 2020-07-30T21:56:02+08:00
[INFO] ------------------------------------------------------------------------

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

查看插件目标参数列表

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

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

如下:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-clean-plugin:help -Ddetail -Dgoal=help
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven4 ---
[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}


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.312 s
[INFO] Finished at: 2020-07-30T22:01:59+08:00
[INFO] ------------------------------------------------------------------------

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

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

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-clean-plugin:help -Ddetail=false -Dgoal=help
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:help (default-cli) @ maven4 ---
[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.


[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.296 s
[INFO] Finished at: 2020-07-30T22:07:31+08:00
[INFO] ------------------------------------------------------------------------

因为我们传的是 -Ddetail=false,所以未展示出参数的详细信息。

命令行运行插件

mvn 插件goupId:插件artifactId[:插件version]:插件目标 [-D目标参数1] [-D目标参数2] [-D目标参数n]
mvn 插件前缀:插件目标  [-D目标参数1] [-D目标参数2] [-D目标参数n]

案例

maven中运行测试用例使用到的插件坐标是:

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.12.4</version>
</dependency>

我们看一下这个插件有哪些目标:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-surefire-plugin:help
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven4 ---
[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] ------------------------------------------------------------------------
[INFO] Total time: 1.437 s
[INFO] Finished at: 2020-07-30T22:13:39+08:00
[INFO] ------------------------------------------------------------------------

从上面输出信息中可以发现有2个目标helptest,描述中可以看出test目标是用来运行测试用例的。

查看test的参数信息,test目标对应的参数太多,这里只列出部分参数,如下:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-surefire-plugin:help -Ddetail -Dgoal=test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:help (default-cli) @ maven4 ---
[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.
      
  skipTests (Default: false)
      Set this to 'true' to skip running tests, but still compile them. Its use
      is NOT RECOMMENDED, but quite convenient on occasion.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.859 s
[INFO] Finished at: 2020-07-30T22:14:12+08:00
[INFO] ------------------------------------------------------------------------
  • skip:这个参数默认是false,如果设置为true的时候,项目将跳过测试代码的编译和测试用例的执行。可以使用maven.test.skip这个属性来进行命令行传参,将其传递给test目标的skip属性,这个通过-D传递的参数名称就和目标参数名称不一样了,所以需要注意-D后面并不一定是参数名称。
  • skipTests:这个参数默认是false,如果设置为true的时候,项目将跳过测试用例的执行,但仍然会编译它们。这里的传参还是使用的参数名,和skip不一样。

我们来运行一下test目标看看效果。

先看一下不加参数的效果:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-surefire-plugin:test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven4 ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.860 s
[INFO] Finished at: 2020-07-30T22:27:59+08:00
[INFO] ------------------------------------------------------------------------

默认是运行了测试用例的,但是我们项目中并没有创建测试用例,所以显示的是No tests to run.

maven.skip.test=true的效果如下(别忘了-D):

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-surefire-plugin:test -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven4 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.578 s
[INFO] Finished at: 2020-07-30T22:30:18+08:00
[INFO] ------------------------------------------------------------------------

对比一下上面2个输出,No tests to run.变成了 Tests are skipped.。说明跳过了测试的执行。

插件的两种传参方式

刚才上面讲了一种通过-D后面跟用户属性的方式给用户传参,还有一种方式,在pom.xml中properties的用户自定义属性中进行配置,如下:

修改项目maven4的pom.xml,properties中加入:

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

在cmd中再次运行test命令,不加参数:

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

如下:

D:\myResource\maven\maven4>mvn org.apache.maven.plugins:maven-surefire-plugin:test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven4 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.656 s
[INFO] Finished at: 2020-07-30T22:36:36+08:00
[INFO] ------------------------------------------------------------------------

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

查看插件/目标详细信息(describe方式)

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

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

看效果:

D:\myResource\maven\maven4>mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin -Dgoal=test -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven4 ---
Downloading from nexus-aliyun: http://maven.aliyun.com/nexus/content/groups/public/org/apache/maven/maven-model/3.6.1/maven-model-3.6.1.pom
......
(由于本地没有describe插件,所以这里需要先下载)
......
http://maven.aliyun.com/nexus/content/groups/public/org/apache/commons/commons-lang3/3.7/commons-lang3-3.7.jar (0 B at 0 B/s)
[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:

    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.

    skipTests (Default: false)
      User property: skipTests
      Set this to 'true' to skip running tests, but still compile them. Its use
      is NOT RECOMMENDED, but quite convenient on occasion.

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.905 s
[INFO] Finished at: 2020-08-02T16:34:02+08:00
[INFO] ------------------------------------------------------------------------

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

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

现在可以大家估计可以知道我们一直用的-Dmaven.test.skip为什么可以跳过测试代码的编译和单元测试的执行了吧。

插件前缀

运行插件的时候,可以通过指定插件坐标的方式运行,但是插件的坐标信息过于复杂,也不方便写和记忆,所以maven中给插件定义了一些简捷的插件前缀,可以通过插件前缀来运行指定的插件。

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

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

示例效果:

D:\myResource\maven\maven4>mvn help:describe -Dplugin=org.apache.maven.plugins:maven-surefire-plugin
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven4 ---
[INFO] org.apache.maven.plugins:maven-surefire-plugin:2.12.4

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

This plugin has 2 goals:

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

surefire:test
  Description: Run tests using Surefire.

For more information, run 'mvn help:describe [...] -Ddetail'

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.971 s
[INFO] Finished at: 2020-08-02T16:41:31+08:00
[INFO] ------------------------------------------------------------------------

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

我们使用前缀来运行一下插件感受一下效果:

D:\myResource\maven\maven4>mvn surefire:test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-cli) @ maven4 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.842 s
[INFO] Finished at: 2020-08-02T16:44:56+08:00
[INFO] ------------------------------------------------------------------------

mvn help命令(插件)

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

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

详细信息如下:

D:\myResource\maven\maven4>mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven4 ---
[INFO] org.apache.maven.plugins:maven-help-plugin:3.2.0

Name: Apache Maven Help Plugin
Description: The Maven Help plugin provides goals aimed at helping to make
  sense out of the build environment. It includes the ability to view the
  effective POM and settings files, after inheritance and active profiles have
  been applied, as well as a describe a particular plugin goal to give usage
  information.
Group Id: org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version: 3.2.0
Goal Prefix: help

This plugin has 8 goals:

help:active-profiles
  Description: Displays a list of the profiles which are currently active for
    this build.

help:all-profiles
  Description: Displays a list of available profiles under the current
    project.
    Note: it will list all profiles for a project. If a profile comes up with a
    status inactive then there might be a need to set profile activation
    switches/property.

help:describe
  Description: Displays a list of the attributes for a Maven Plugin and/or
    goals (aka Mojo - Maven plain Old Java Object).

help:effective-pom
  Description: Displays the effective POM as an XML for this build, with the
    active profiles factored in, or a specified artifact. If verbose, a comment
    is added to each XML element describing the origin of the line.

help:effective-settings
  Description: Displays the calculated settings as XML for this project,
    given any profile enhancement and the inheritance of the global settings
    into the user-level settings.

help:evaluate
  Description: Evaluates Maven expressions given by the user in an
    interactive mode.

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

help:system
  Description: Displays a list of the platform details like system properties
    and environment variables.

For more information, run 'mvn help:describe [...] -Ddetail'

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.003 s
[INFO] Finished at: 2020-08-02T16:48:14+08:00
[INFO] ------------------------------------------------------------------------

插件和生命周期绑定(maven内置)

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

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

maven内置插件以及绑定

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

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

生命周期阶段插件:目标
pre-clean
cleanmaven-clean-plugin:clean
post-clean

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

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

default生命周期中有23个阶段,这里只列出有默认绑定的,其他的没有列出的没有绑定任何插件,因此没有任何实际的行为。

生命周期阶段插件:目标执行任务
process-resourcesmaven-resources-plugin:resources复制主资源文件至主输出目录
compilemaven-compiler-plugin:compile编译主代码至主输出目录
process-test-resourcesmaven-resources-plugin:testResources复制测试资源文件至测试输出目录
test-compilemaven-compiler-plugin:testCompile编译测试代码至测试输出目录
testmaven-surefile-plugin:test执行测试用例
packagemaven-jar-plugin:jar创建项目jar包
installmaven-install-plugin:install将输出构件安装到本地仓库
deploymaven-deploy-plugin:deploy将输出的构件部署到远程仓库

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

生命周期阶段插件:目标
pre-site
sitemaven-site-plugin:site
post-site
site-deploymaven-site-plugin:deploy

案例 mvn clean

该命令是调用clean生命周期的clean阶段,实际执行的阶段为clean生命周期中的pre-clean和clean阶段,从上面内置绑定表格中找一下,可以看到只有clean阶段绑定了maven-clean-plugin插件的clean目标,所以运行mvn clean的时候,实际上会调用maven-clean-plugin插件的clean目标来清理代码。

运行一下看一下效果:

D:\myResource\maven\maven4>mvn clean
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.625 s
[INFO] Finished at: 2020-08-02T17:01:17+08:00
[INFO] ------------------------------------------------------------------------

上面有一行输出如下:

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---

这个表示调用的插件是: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

我们来验证一下,看看是不是和我们分析的一样:

D:\myResource\maven\maven4>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-RELEASE
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven4 ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven4 ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven4 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.088 s
[INFO] Finished at: 2020-08-02T17:08:38+08:00
[INFO] ------------------------------------------------------------------------

从上面输出中可以看到调用了5个插件的目标,和分析的一样。

输出信息中有一条为:[INFO] Tests are skipped.。这是因为我们上面演示properties方式给插件传参的时候,配置了跳过测试。忘记的可以回头再看一下。

插件和生命周期绑定(自定义)

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

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

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

pom.xml加入如下配置:

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

注意上面配置的attach-source,后面输出中会有。

id:任务的id,需唯一,如果不指定,默认为default

每个插件的配置在pom.xml的plugins元素中只能写一次,否则会有警告。

最终pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.theboyaply</groupId>
    <artifactId>maven4</artifactId>
    <version>1.0-RELEASE</version>

    <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>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

我们执行 mvn install,输出如下:

D:\myResource\maven\maven4>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven4\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven4 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven4 ---
[INFO] Building jar: D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ maven4 ---
[INFO] Building jar: D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT-sources.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ maven4 ---
[INFO] Installing D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT.jar to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT.jar
[INFO] Installing D:\myResource\maven\maven4\pom.xml to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT.pom
[INFO] Installing D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT-sources.jar to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT-sources.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.549 s
[INFO] Finished at: 2020-08-02T17:24:16+08:00
[INFO] ------------------------------------------------------------------------

上面有个输出如下:

[INFO] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ maven4 ---
[INFO] Building jar: D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT-sources.jar

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

最后有个输出:

[INFO] Installing D:\myResource\maven\maven4\target\maven4-1.0-SNAPSHOT-sources.jar to D:\apache\.m2\repository\com\theboyaply\maven4\1.0-SNAPSHOT\maven4-1.0-SNAPSHOT-sources.jar

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


有些插件的目标默认会绑定到一些生命周期的阶段中,那么如果刚好插件默认绑定的阶段和上面配置的一致,那么上面phase元素可以不写了。

查看插件的默认绑定阶段:

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

我们看一下插件sourcejar-no-fork目标默认的绑定(source是前缀。):

D:\myResource\maven\maven4>mvn help:describe -Dplugin=source -Dgoal=jar-no-fork -Ddetail
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven4 ---
[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:
......
......

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


我们知道3套生命周期的运行时没有依赖的,但是每套中的阶段是有先后顺序的,运行某个阶段的时候,会先执行他前面所有的阶段。清理代码使用的是clean周期中的clean阶段,编译代码用的是default周期中的compile阶段,当直接运行mvn compile编译代码的时候并不会去清理代码,编译代码的时候若发现文件没有变动,会跳过没有变化的文件进行编译。

如果我们想每次编译之前强制先清理代码,我们经常这么写:

mvn clean compile

效果如下:

D:\myResource\maven\maven4>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\myResource\maven\maven4\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.046 s
[INFO] Finished at: 2020-08-02T17:40:55+08:00
[INFO] ------------------------------------------------------------------------

还有其他方式么?

我们刚才学了自定义绑定,我们可以在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,效果如下:

D:\myResource\maven\maven4>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\myResource\maven\maven4\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.678 s
[INFO] Finished at: 2020-08-02T17:46:29+08:00
[INFO] ------------------------------------------------------------------------

输出中有:

[INFO] --- maven-clean-plugin:2.5:clean (clean-target) @ maven4 ---
[INFO] Deleting D:\myResource\maven\maven4\target

说明我们的绑定成功了,这样的话,后面每次执行编译等操作就不用写clean了。

pom.xml插件配置详解

所有插件目标参数共享

build->plugins->plugin中配置:

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

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

案例:

pom.xml中的build元素修改为如下:(如果还有其它跳过测试的配置,请去掉。比如properties)

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

运行mvn test,效果如下:

D:\myResource\maven\maven4>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven4 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\myResource\maven\maven4\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven4 ---
[INFO] Tests are skipped.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.193 s
[INFO] Finished at: 2020-08-02T17:53:22+08:00
[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>

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

pom.xml中的build元素修改为如下:

<build>
    <plugins>
        <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>
    </plugins>
</build>

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

运行mvn pre-clean,效果如下:

D:\myResource\maven\maven4>mvn pre-clean
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.theboyaply:maven4 >------------------------
[INFO] Building maven4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default) @ maven4 ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:help (default) @ maven4 ---
[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] ------------------------------------------------------------------------
[INFO] Total time: 1.104 s
[INFO] Finished at: 2020-08-02T18:18:25+08:00
[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构件一样,插件构件也是基于坐标存储在maven仓库中,有需要的时候,maven会从本地查找插件,如果不存在,则到远程仓库查找,找到了以后下载到本地仓库,然后使用。

大家回忆一下,上一章讲过的,pom.xml中可以配置依赖的构件的仓库地址,如下:

<repositories>
    <repository>
        <id>maven-nexus</id>
        <url>http://192.168.19.110:8081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

但是插件仓库的配置和这个有一点不一样,插件的是在pluginRepositories->pluginRepository元素中配置的,如下:

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

看一下上面2段配置,repository中的配置和pluginRepository中的子元素是一样的,这个主意下就可以了。

插件的默认groupId

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

修改pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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.theboyaply</groupId>
    <artifactId>maven4</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <compilerVersion>1.8</compilerVersion>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

上面用到了maven-compiler-plugin,这个插件是编译代码的,是maven官方提供的插件,我们省略了groupId

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

大家回头去看一下,文章最开始的时候,在properties中有几个属性值是1.8的配置,这几个值默认会被maven-compiler-plugin这个插件的上面3个参数获取,具体可以去看一下这个插件compile目标的参数说明。

上面pom.xml省略了插件的groupId配置,如下:

<groupId>org.apache.maven.plugins</groupId>

maven在解析该插件的时候,会自动给这个插件补上默认的官方的groupId,所以可以正常运行,但是不建议大家这么使用,容易让新手比较懵逼。

插件的前缀解析

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

插件前缀与插件groupId:artifactId是一一对应的关系,这个关系的配置存储在仓库的元数据中。

中央仓库的元数据位于下面2个xml中:

~/.m2/repository/org/apache/maven/plugins/maven-metadata-central.xml
~/.m2/repository/org/codehaus/mojo/maven-metadata-central.xml

阿里云的元数据位于下面2个xml中:

~/.m2/repository/org/apache/maven/plugins/maven-metadata-alimaven.xml
~/.m2/repository/org/codehaus/mojo/maven-metadata-alimaven.xml

部分内容如下,大家感受一下:

<plugin>
    <prefix>clean</prefix>
    <artifactId>maven-clean-plugin</artifactId>
    <name>Apache Maven Clean Plugin</name>
</plugin>
<plugin>
    <prefix>clover</prefix>
    <artifactId>maven-clover-plugin</artifactId>
    <name>Maven Clover Plugin</name>
</plugin>
<plugin>
    <prefix>compiler</prefix>
    <artifactId>maven-compiler-plugin</artifactId>
    <name>Apache Maven Compiler Plugin</name>
</plugin>
<plugin>
    <prefix>continuum</prefix>
    <artifactId>maven-continuum-plugin</artifactId>
    <name>Maven Continuum Plugin</name>
</plugin>

也可以通过在settings.xml中配置,让maven检查其他grouId上的插件元数据中前缀和插件关系的配置,如下:

<settings>
  <pluginGroups>
    <pluginGroup>com.your.plugins</pluginGroup>
  </pluginGroups>
</settings>

pluginGroups中有多个pluginGroup,可以配置你自己插件的元数据所在的groupId,然后可以通过前缀来访问你自己的插件元数据目录,此处先不细说,这个后面文章中讲自定义插件的时候会再次说明。

查看最终pom.xml文件

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

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

mvn help:effective-pom

效果:

D:\myResource\maven\maven4>mvn help:effective-pom > final.xml

上面我们将命令产生的结果输出到项目的final.xml文件中了,我们看一下项目的final.xml的内容:

(开头和结尾的[INFO]信息抛开不看)

<?xml version="1.0" encoding="GBK"?>
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Generated by Maven Help Plugin on 2020-08-02T18:54:54+08:00            -->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/                -->
<!--                                                                        -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                                                        -->
<!-- Effective POM for project 'com.theboyaply:maven4:jar:1.0-SNAPSHOT'     -->
<!--                                                                        -->
<!-- ====================================================================== -->
<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.theboyaply</groupId>
  <artifactId>maven4</artifactId>
  <version>1.0-SNAPSHOT</version>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <repositories>
    <repository>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </repository>
  </repositories>
  <pluginRepositories>
    <pluginRepository>
      <releases>
        <updatePolicy>never</updatePolicy>
      </releases>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
      <id>central</id>
      <name>Central Repository</name>
      <url>https://repo.maven.apache.org/maven2</url>
    </pluginRepository>
  </pluginRepositories>
  <build>
    <sourceDirectory>D:\myResource\maven\maven4\src\main\java</sourceDirectory>
    <scriptSourceDirectory>D:\myResource\maven\maven4\src\main\scripts</scriptSourceDirectory>
    <testSourceDirectory>D:\myResource\maven\maven4\src\test\java</testSourceDirectory>
    <outputDirectory>D:\myResource\maven\maven4\target\classes</outputDirectory>
    <testOutputDirectory>D:\myResource\maven\maven4\target\test-classes</testOutputDirectory>
    <resources>
      <resource>
        <directory>D:\myResource\maven\maven4\src\main\resources</directory>
      </resource>
    </resources>
    <testResources>
      <testResource>
        <directory>D:\myResource\maven\maven4\src\test\resources</directory>
      </testResource>
    </testResources>
    <directory>D:\myResource\maven\maven4\target</directory>
    <finalName>maven4-1.0-SNAPSHOT</finalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-antrun-plugin</artifactId>
          <version>1.3</version>
        </plugin>
        <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.2-beta-5</version>
        </plugin>
        <plugin>
          <artifactId>maven-dependency-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <artifactId>maven-release-plugin</artifactId>
          <version>2.5.3</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <executions>
          <execution>
            <id>default-compile</id>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
            <configuration>
              <compilerVersion>1.8</compilerVersion>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
            <configuration>
              <compilerVersion>1.8</compilerVersion>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <compilerVersion>1.8</compilerVersion>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-clean-plugin</artifactId>
        <version>2.5</version>
        <executions>
          <execution>
            <id>default-clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <executions>
          <execution>
            <id>default-testResources</id>
            <phase>process-test-resources</phase>
            <goals>
              <goal>testResources</goal>
            </goals>
          </execution>
          <execution>
            <id>default-resources</id>
            <phase>process-resources</phase>
            <goals>
              <goal>resources</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
              <goal>jar</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.4</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
              <goal>test</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-install-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-install</id>
            <phase>install</phase>
            <goals>
              <goal>install</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-deploy-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.3</version>
        <executions>
          <execution>
            <id>default-site</id>
            <phase>site</phase>
            <goals>
              <goal>site</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\myResource\maven\maven4\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
          <execution>
            <id>default-deploy</id>
            <phase>site-deploy</phase>
            <goals>
              <goal>deploy</goal>
            </goals>
            <configuration>
              <outputDirectory>D:\myResource\maven\maven4\target\site</outputDirectory>
              <reportPlugins>
                <reportPlugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-project-info-reports-plugin</artifactId>
                </reportPlugin>
              </reportPlugins>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <outputDirectory>D:\myResource\maven\maven4\target\site</outputDirectory>
          <reportPlugins>
            <reportPlugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-project-info-reports-plugin</artifactId>
            </reportPlugin>
          </reportPlugins>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <outputDirectory>D:\myResource\maven\maven4\target\site</outputDirectory>
  </reporting>
</project>

-- end --


评论