欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Maven插件的安装及使用

程序员文章站 2022-03-21 13:03:30
maven插件主要是为maven中生命周期中的阶段服务的,maven中只是定义了3套生命周期,以及每套生命周期中有哪些阶段,具体每个阶段中执行什么操作,完全是交给插件去干的。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:\code\ideaprojects\maven-chat06>mvn org.apache.maven.plugins:maven-clean-plugin:help
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[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用户输出目标详细的参数列表信息,如果没有这个,目标的参数列表不会输出出来,看效果。

如:

d:\code\ideaprojects\maven-chat06>mvn org.apache.maven.plugins:maven-clean-plugin:help -dgoal=help -ddetail
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[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: x x x 这 样 的 部 分 , 这 种 表 示 给 这 个 运 行 的 目 标 传 参 , 可 以 通 过 m v n − d x x x 这 种 方 式 传 参 , x x x 为 {xxx}这样的部分,这个xxx有时候和目标参数的名称不一致,所以这点需要注意,运行带参数的目标,看一下效果:

d:\code\ideaprojects\maven-chat06>mvn org.apache.maven.plugins:maven-clean-plugin:help -dgoal=help -ddetail=false
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[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.


[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 0.332 s
[info] finished at: 2019-11-18t15:14:56+08:00
[info] ------------------------------------------------------------------------

上面传了一个detail=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:\code\ideaprojects\maven-chat06>mvn org.apache.maven.plugins:maven-surefire-plugin:help
[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: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.


[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 0.662 s
[info] finished at: 2019-11-18t15:26:26+08:00
[info] ------------------------------------------------------------------------

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

我们看一下test目标对应的参数列表:

test目标对应的参数太多,我们只列出了部分参数,如下:

d:\code\ideaprojects\maven-chat06>mvn org.apache.maven.plugins:maven-surefire-plugin:help
[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: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.


[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 0.662 s
[info] finished at: 2019-11-18t15:26:26+08:00
[info] ------------------------------------------------------------------------

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

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

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

d:\code\ideaprojects\maven-chat06>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
[info] ------------------------------------------------------------------------
[info] total time: 0.640 s
[info] finished at: 2019-11-18t15:33:48+08:00
[info] ------------------------------------------------------------------------

加maven.skip.test=true的效果如下:

d:\code\ideaprojects\maven-chat06>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
[info] ------------------------------------------------------------------------
[info] total time: 0.652 s
[info] finished at: 2019-11-18t15:34:45+08:00
[info] ------------------------------------------------------------------------

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

[info] tests are skipped.

说明跳过了测试的执行。

插件传参的2种方式

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

修改项目maven-chat06的pom.xml,properties中加入:

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

cmd中运行:

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

效果如下:

d:\code\ideaprojects\maven-chat06>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
[info] ------------------------------------------------------------------------
[info] total time: 0.638 s
[info] finished at: 2019-11-18t15:46:04+08:00
[info] ------------------------------------------------------------------------

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

上面说的都是插件目标的东西,那么插件目标是如何和生命周期关联起来的呢?继续向下看。

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

mvn help:describe -dplugin=插件goupid:插件artifactid[:插件version] -dgoal=目标名称 -ddetail
mvn help:describe -dplugin=插件前缀 -dgoal=目标名称 -ddetail

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

d:\code\ideaprojects\maven-chat06>mvn help:describe -dplugin=org.apache.maven.plugins:maven-surefire-plugin -dgoal=test -ddetail
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[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为什么可以跳过测试代码的编译和单元测试的执行了吧。

插件前缀

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

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

mvn help:describe -dplugin=插件goupid:插件artifactid[:插件version]

示例效果:

d:\code\ideaprojects\maven-chat06>mvn help:describe -dplugin=org.apache.maven.plugins:maven-surefire-plugin
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-chat06 ---
[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

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

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

d:\code\ideaprojects\maven-chat06>mvn surefire: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
[info] ------------------------------------------------------------------------
[info] total time: 0.934 s
[info] finished at: 2019-11-18t16:18:42+08:00
[info] ------------------------------------------------------------------------

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

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

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

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

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

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

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

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

来几个案例解说一下。

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

运行一下看一下效果:

d:\code\ideaprojects\maven-chat06>mvn clean
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-chat06 ---
[info] deleting d:\code\ideaprojects\maven-chat06\target
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 0.408 s
[info] finished at: 2019-11-18t16:34:14+08:00
[info] ------------------------------------------------------------------------

上面有一行输出如下:

[info] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-chat06 ---

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

该命令调用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:\code\ideaprojects\maven-chat06>mvn test
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to d:\code\ideaprojects\maven-chat06\target\classes
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ maven-chat06 ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to d:\code\ideaprojects\maven-chat06\target\test-classes
[info]
[info] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-chat06 ---
[info] tests are skipped.
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.880 s
[info] finished at: 2019-11-18t16:36:55+08:00
[info] ------------------------------------------------------------------------

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

再来看一个跳过测试的例子,如下:

d:\code\ideaprojects\maven-chat06>mvn test -dmaven.skip.test=true
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-chat06 ---
[info] tests are skipped.
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.384 s
[info] finished at: 2019-11-18t16:38:52+08:00
[info] ------------------------------------------------------------------------

上面这个是不是很熟悉,经常用到的跳过测试,为什么这么写,我想大家都知道了吧。

其他几个mvn compile、mvn install、mvn deploy建议大家也自己去玩玩,加深理解。

自定义绑定

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

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

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

在maven-chat06中的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如下:

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

运行下面命令:

mvn install

效果:

d:\code\ideaprojects\maven-chat06>mvn install
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-chat06 ---
[info]
[info] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-chat06 ---
[info]
[info] --- maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ maven-chat06 ---
[info]
[info] --- maven-install-plugin:2.4:install (default-install) @ maven-chat06 ---
[info] installing d:\code\ideaprojects\maven-chat06\target\maven-chat06-1.0-snapshot.jar to c:\users\think\.m2\repository\com\javacode2018\maven-chat06\1.0-snapshot\maven-chat06-1.0-snapshot.jar
[info] installing d:\code\ideaprojects\maven-chat06\pom.xml to c:\users\think\.m2\repository\com\javacode2018\maven-chat06\1.0-snapshot\maven-chat06-1.0-snapshot.pom
[info] installing d:\code\ideaprojects\maven-chat06\target\maven-chat06-1.0-snapshot-sources.jar to c:\users\think\.m2\repository\com\javacode2018\maven-chat06\1.0-snapshot\maven-chat06-1.0-snapshot-sources.jar
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 2.268 s
[info] finished at: 2019-11-18t16:59:12+08:00
[info] ------------------------------------------------------------------------

上面有个输出如下:

maven-source-plugin:3.2.0:jar-no-fork (attach-source) @ maven-chat06 ---

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

最后有个输出:

[info] installing d:\code\ideaprojects\maven-chat06\target\maven-chat06-1.0-snapshot-sources.jar to c:\users\think\.m2\repository\com\javacode2018\maven-chat06\1.0-snapshot\maven-chat06-1.0-snapshot-sources.jar

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

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

mvn help:describe -dplugin=插件goupid:插件artifactid[:插件version] -dgoal=目标名称 -ddetail
mvn help:describe -dplugin=插件前缀 -dgoal=目标名称 -ddetail

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

d:\code\ideaprojects\maven-chat06>mvn help:describe -dplugin=source -dgoal=jar-no-fork -ddetail
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-help-plugin:3.2.0:describe (default-cli) @ maven-chat06 ---
[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

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

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

mvn clean compile

上面的写法是不是很熟悉,运行一下看看效果:

d:\code\ideaprojects\maven-chat06>mvn clean compile
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-chat06 ---
[info] deleting d:\code\ideaprojects\maven-chat06\target
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to d:\code\ideaprojects\maven-chat06\target\classes
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.666 s
[info] finished at: 2019-11-18t17:16:53+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>

运行下面命令看效果:

d:\code\ideaprojects\maven-chat06>mvn compile
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-chat06 ---
[info] deleting d:\code\ideaprojects\maven-chat06\target
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to d:\code\ideaprojects\maven-chat06\target\classes
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.630 s
[info] finished at: 2019-11-18t17:22:37+08:00
[info] ------------------------------------------------------------------------

输出中有:

[info] --- maven-clean-plugin:2.5:clean (clean-target) @ maven-chat06 ---
[info] deleting d:\code\ideaprojects\maven-chat06\target

这个表示运行了代码清理的功能,进行了代码清理,是不是感觉很爽,不用每次都写clean了。

pom.xml插件配置详解

插件目标共享参数配置

build->plugins->plugin中配置:

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

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

案例:
将案例中的pom.xml中的build元素修改成下面这样。

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

运行下面命令,看效果:

d:\code\ideaprojects\maven-chat06>mvn test
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-resources-plugin:2.6:testresources (default-testresources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:testcompile (default-testcompile) @ maven-chat06 ---
[info] nothing to compile - all classes are up to date
[info]
[info] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-chat06 ---
[info] tests are skipped.
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.328 s
[info] finished at: 2019-11-18t17:30:58+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和help,execution元素没有指定id,所以默认id是default。

运行下面命令,见效果:

d:\code\ideaprojects\maven-chat06>mvn pre-clean
[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) @ maven-chat06 ---
[info] tests are skipped.
[info]
[info] --- maven-surefire-plugin:2.12.4:help (default) @ 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.


[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 0.738 s
[info] finished at: 2019-11-18t17:41:08+08:00
[info] ------------------------------------------------------------------------

可以看到上面输出中运行了插件的两个目标,和预期结果一致。

获取maven插件信息

上面我们介绍了,可以通过下面命令获取插件详细介绍信息

mvn help:describe -dplugin=插件goupid:插件artifactid[:插件version] -dgoal=目标名称 -ddetail
mvn help:describe -dplugin=插件前缀 -dgoal=目标名称 -ddetail

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

Maven插件的安装及使用

插件解析机制

为了方便用户使用和配置插件,maven不需要用户提供完整的插件坐标信息,就可以解析到正确的插件,不过我建议使用插件配置的时候最好还是配置完整的坐标信息,不然不利于新人的理解和问题的排查。

插件仓库

与其他maven构件一样,插件构件也是基于坐标存储在maven仓库中,有需要的时候,maven会从本地查找插件,如果不存在,则到远程仓库查找,找到了以后下载到本地仓库,然后使用。

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

<repositories>
  <repository>
    <id>maven-nexus</id>
    <url>http://localhost: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,如下:

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

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

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

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

运行下面命令:

d:\code\ideaprojects\maven-chat06>mvn clean compile
[info] scanning for projects...
[info]
[info] -------------------< com.javacode2018:maven-chat06 >--------------------
[info] building maven-chat06 1.0-snapshot
[info] --------------------------------[ jar ]---------------------------------
[info]
[info] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-chat06 ---
[info] deleting d:\code\ideaprojects\maven-chat06\target
[info]
[info] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-chat06 ---
[info] using 'utf-8' encoding to copy filtered resources.
[info] copying 0 resource
[info]
[info] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven-chat06 ---
[info] changes detected - recompiling the module!
[info] compiling 1 source file to d:\code\ideaprojects\maven-chat06\target\classes
[info] ------------------------------------------------------------------------
[info] build success
[info] ------------------------------------------------------------------------
[info] total time: 1.692 s
[info] finished at: 2019-11-18t18:11:34+08:00
[info] ------------------------------------------------------------------------

可以看到可以正常运行。

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

接几个图,大家感受一下:

Maven插件的安装及使用

也可以通过在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:\code\ideaprojects\maven-chat06>mvn help:effective-pom > 1.xml

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

<?xml version="1.0" encoding="gbk"?>
<!-- ====================================================================== -->
<!--                                    -->
<!-- generated by maven help plugin on 2019-11-18t18:41:40+08:00      -->
<!-- see: http://maven.apache.org/plugins/maven-help-plugin/        -->
<!--                                    -->
<!-- ====================================================================== -->
<!-- ====================================================================== -->
<!--                                    -->
<!-- effective pom for project                       -->
<!-- 'com.javacode2018:maven-chat06: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.javacode2018</groupid>
  <artifactid>maven-chat06</artifactid>
  <version>1.0-snapshot</version>
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
  </properties>
  <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:\code\ideaprojects\maven-chat06\src\main\java</sourcedirectory>
    <scriptsourcedirectory>d:\code\ideaprojects\maven-chat06\src\main\scripts</scriptsourcedirectory>
    <testsourcedirectory>d:\code\ideaprojects\maven-chat06\src\test\java</testsourcedirectory>
    <outputdirectory>d:\code\ideaprojects\maven-chat06\target\classes</outputdirectory>
    <testoutputdirectory>d:\code\ideaprojects\maven-chat06\target\test-classes</testoutputdirectory>
    <resources>
      <resource>
        <directory>d:\code\ideaprojects\maven-chat06\src\main\resources</directory>
      </resource>
    </resources>
    <testresources>
      <testresource>
        <directory>d:\code\ideaprojects\maven-chat06\src\test\resources</directory>
      </testresource>
    </testresources>
    <directory>d:\code\ideaprojects\maven-chat06\target</directory>
    <finalname>maven-chat06-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:\code\ideaprojects\maven-chat06\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:\code\ideaprojects\maven-chat06\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:\code\ideaprojects\maven-chat06\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:\code\ideaprojects\maven-chat06\target\site</outputdirectory>
  </reporting>
</project>

上面这个文件,大家一定要认真多看几遍,理解一下,里面包含太多东西,再重复一下,上面的文件多看几遍!多看几遍!多看几遍!要理解!

到此这篇关于maven插件的安装及使用的文章就介绍到这了,更多相关maven插件内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关标签: Maven 插件