服务器之家

服务器之家 > 正文

Maven插件的安装及使用

时间:2020-09-21 14:21     来源/作者:易兮科技

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

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

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

插件目标

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

目标参数

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

列出插件所有目标

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

上面插件前缀的先略过,我们先看第一种效果。

如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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,分号后面的部分是目标名称,分号前面的部分是插件的前缀,每个目标的后面包含对这个目标的详细解释说明,关于前缀的后面会有详细介绍。

查看插件目标参数列表

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

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

如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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有时候和目标参数的名称不一致,所以这点需要注意,运行带参数的目标,看一下效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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,上面未输出目标的详细参数信息。

命令行运行插件

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

案例:

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

?
1
2
3
4
5
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12.4</version>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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目标对应的参数太多,我们只列出了部分参数,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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目标看看效果。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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的效果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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个输出,下面的多了一行如下:

?
1
[INFO] Tests are skipped.

说明跳过了测试的执行。

插件传参的2种方式

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

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

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

cmd中运行:

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

效果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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效果一样。

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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中给插件定义了一些简捷的插件前缀,可以通过插件前缀来运行指定的插件。

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

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

示例效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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是插件的前缀,它的坐标是:

?
1
2
3
4
5
<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目标来清理代码。

运行一下看一下效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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] ------------------------------------------------------------------------

上面有一行输出如下:

?
1
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-chat06 ---

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

该命令调用default生命周期的test阶段,实际上会从default生命周期的第一个阶段(validate)开始执行一直到test阶段结束。这里面包含了代码的编译,运行测试用例。还是和上面的分析过程一样,对照上面表格中的绑定关系,可以得到mvn test会调用下面一些插件的目标:

?
1
2
3
4
5
maven-resources-plugin:resources
maven-compiler-plugin:compile
maven-resources-plugin:testResources
maven-compiler-plugin:testCompile
maven-surefile-plugin:test

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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个插件的目标,和分析的一样。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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加入如下配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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>

运行下面命令:

?
1
mvn install

效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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] ------------------------------------------------------------------------

上面有个输出如下:

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

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

最后有个输出:

?
1
[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元素可以不写了,那么怎么查看插件的默认绑定呢?

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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编译代码的时候并不会去清理代码,编译代码的时候若发现文件没有变动,会跳过没有变化的文件进行编译。如果我们想每次编译之前强制先清理代码,我们经常这么写:

?
1
mvn clean compile

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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元素中加入下面配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<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>

运行下面命令看效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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] ------------------------------------------------------------------------

输出中有:

?
1
2
[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中配置:

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
<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>

运行下面命令,看效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
2
3
1. mvn -Dmaven.test.skip=tue
2. properties中配置<maven.test.skip>true</maven.test.skip>
3. build中配置插件参数的方式

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

插件目标参数配置

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

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

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

感受一下效果,将pom.xml中的build元素改为下面内容:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<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。

运行下面命令,见效果:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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插件信息

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

?
1
2
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构件一样,插件构件也是基于坐标存储在maven仓库中,有需要的时候,maven会从本地查找插件,如果不存在,则到远程仓库查找,找到了以后下载到本地仓库,然后使用。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
<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元素中配置的,如下:

?
1
2
3
4
5
6
7
8
9
<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,如下:

?
1
2
3
4
5
6
7
8
9
<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目标的参数说明。

运行下面命令:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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配置,如下:

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

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

插件前缀的解析

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

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

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

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

Maven插件的安装及使用

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

?
1
2
3
4
5
<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写的比较复杂的时候,最终合并之后是什么效果呢,我们可以通过下面这个命令查看:

?
1
mvn help:effective-pom

效果:

?
1
D:\code\IdeaProjects\maven-chat06>mvn help:effective-pom > 1.xml

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?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插件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_47157676/article/details/108506546

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
最新idea2020注册码永久激活(激活到2100年)
最新idea2020注册码永久激活(激活到2100年) 2020-07-29
返回顶部