0%

maven插件

maven插件

maven本身就是一个依赖插件执行的框架,每个任务都是由插件完成的,一般有 创建jar文件、创建war文件、编译代码文件、单元测试等

插件语法

1
mvn [pluginName]:[goalName]

有两种类型的插件,一个是构建插件,一个是报告插件

常用的插件

maven-compiler-plugin插件

进行项目的编译

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-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>8</source>
<target>8</target>
<fork>true</fork>
<verbose>true</verbose>
<encoding>UTF-8</encoding>
<compilerArguments>
<sourcepath>
${project.basedir}/src/main/java
</sourcepath>
</compilerArguments>
</configuration>
</plugin>

maven-archetype-plugin插件

生成项目骨架的插件,mvn archetype:generate就是让maven-archetype-plugin生成一个很简单的项目骨架,帮助开发者快速上手

maven-shade-plugin插件

默认生成的jar是不能直接运行的,带有main方法的类信息不会添加到manifest中(jar文件中的META-INF/MANIFEST.MF文件中Main-Class就是记录的主类),可以借助maven-shade-plugin插件来生成可执行的jar文件,且打包的jar会包含依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<!--<minimizeJar>true</minimizeJar> -->
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.zhanghe.study.HelloWorld</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

maven-assembly-plugin插件

maven-assembly-plugin的用途是制作项目分发包,该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。maven-assembly-plugin支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的,例如用户可以 按文件级别的粒度、文件集级别的粒度、模块级别的粒度、以及依赖级别的粒度控制打包,此外,包含和排除配置也是支持的。maven-assembly- plugin要求用户使用一个名为assembly.xml的元数据文件来表述打包,它的single目标可以直接在命令行调用,也可以被绑定至生命周期

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

assembly.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
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<!-- 可自定义,这里指定的是项目环境 -->
<!-- spring-boot-assembly-local-1.0.RELEASE.tar.gz -->
<id>${profileActive}-${project.version}</id>
<!-- 打包的类型,如果有N个,将会打N个类型的包 -->
<formats>
<format>tar.gz</format>
<!--<format>zip</format>-->
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<fileSets>
<!--
0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限;
0644->即用户具有读写权限,组用户和其它用户具有只读权限;
-->
<!-- 将src/bin目录下的所有文件输出到打包后的bin目录中 -->
<fileSet>
<directory>${basedir}/src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>**.sh</include>
</includes>
</fileSet>
<!-- 指定输出target/classes中的配置文件到config目录中 -->
<fileSet>
<directory>${basedir}/target/classes</directory>
<outputDirectory>config</outputDirectory>
<fileMode>0644</fileMode>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</fileSet>
<!-- 将第三方依赖打包到lib目录中 -->
<fileSet>
<directory>${basedir}/target/lib</directory>
<outputDirectory>lib</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<!-- 将项目启动jar打包到boot目录中 -->
<fileSet>
<directory>${basedir}/target</directory>
<outputDirectory>boot</outputDirectory>
<fileMode>0755</fileMode>
<includes>
<include>${project.build.finalName}.jar</include>
</includes>
</fileSet>
<!-- 包含根目录下的文件 -->
<fileSet>
<directory>${basedir}</directory>
<includes>
<include>*.md</include>
</includes>
</fileSet>
</fileSets>
</assembly>

maven-dependency-plugin插件

maven-dependency-plugin最大的用途是帮助分析项目依赖,dependency:list能够列出项目最终解析到的依赖列表;dependency:tree能进一步的描绘项目依赖树;dependency:analyze可以告诉你项目依赖潜在的问题,如果有直接使用到的却未声明的依赖,该目标就会发出警告;dependency:copy-dependencies能将项目依赖从本地Maven仓库复制到某个特定的文件夹下面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>target/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<!-- 包含的范围 -->
<includeScope>system</includeScope>
<!-- 排除的groupId依赖 -->
<excludeGroupIds>test</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>

maven-jar-plugin插件

用于自定义maven打包jar内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<!-- 项目启动类 -->
<mainClass>Application</mainClass>
<!-- 依赖的jar的目录前缀 -->
<classpathPrefix>../lib</classpathPrefix>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<includes>
<!-- 只打包指定目录的文件 -->
<include>com/zhanghe/study/**</include>
</includes>
</configuration>
</plugin>

maven-war-plugin插件

自定义maven打包war内容

maven-resources-plugin插件

为了使项目结构更为清晰,Maven区别对待Java代码文件和资源文件,maven-compiler-plugin用来编译Java代码,maven-resources-plugin则用来处理资源文件。默认的主资源文件目录是src/main/resources,很多用户会需要添加额外的资源文件目录,这个时候就可以通过配置maven-resources-plugin来实现。此外,资源文件过滤也是Maven的一大特性,你可以在资源文件中使用${propertyName}形式的Maven属性,然后配置maven-resources-plugin开启对资源文件的过滤,之后就可以针对不同环境通过命令行或者Profile传入属性的值,以实现更为灵活的构建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profileActive}.yml</include>
<include>mapper/**/*.xml</include>
<include>static/**</include>
<include>templates/**</include>
<include>*.xml</include>
<include>*.properties</include>
</includes>
</resource>

maven-release-plugin插件

maven-release-plugin的是帮助自动化项目版本发布,它依赖于POM中的SCM信息。版本发布是非常琐碎的工作,它涉及了各种检查,而且由于该工作仅仅是偶尔需要,因此手动操作很容易遗漏一 些细节,maven-release-plugin让该工作变得非常快速简便,不易出错。maven-release-plugin的各种目标通常直接在命令行调用

  • release:prepare用来准备版本发布,具体的工作包括检查是否有未提交代码、检查是否有SNAPSHOT依赖、升级项目的SNAPSHOT版本至RELEASE版本、为项目打标签等等。
  • release:perform是签出标签中的RELEASE源码,构建并发布。

maven-enforcer-plugin插件

maven-enforcer- plugin允许创建一系列规则,包括设定Java版本、设定Maven版本、禁止某些依赖、禁止 SNAPSHOT依赖。只要在一个父POM配置规则,然后让子项目继承,当规则遭到破坏的时候,Maven就会报错。除了标准的规则之外,还可以扩展该插 件,编写自己的规则。maven-enforcer-plugin的enforce目标负责检查规则,它默认绑定到生命周期的validate阶段

maven-help-plugin插件

maven-help-plugin是一个小巧的辅助工具

  • help:system可以打印所有可用的环境变量和Java系统属性
  • help:effective-pom 用来打印项目的有效POM,有效POM是指合并了所有父POM(包括Super POM)后的XML,当你不确定POM的某些信息从何而来时,就可以查看有效POM
  • help:effective-settings用来打印项目的有效settings
  • maven-help-plugin的describe目标可以帮助你描述任何一个Maven插件的信息
  • all-profiles目标和active-profiles目标帮助查看项目的Profile

maven-antrun-plugin插件

maven-antrun-plugin能让用户在Maven项目中运行Ant任务。用户可以直接在该插件的配置以Ant的方式编写Target, 然后交给该插件的run目标去执行,在老项目由Ant向maven迁移的项目中,可能会使用该插件

插件的调用

用户可以通过两种方式调用Maven插件目标。

第一种方式

第一种方式是将插件目标与生命周期阶段(lifecycle phase)绑定,这样用户在命令行只是输入生命周期阶段而已,例如Maven默认将maven-compiler-plugin的compile目标与 compile生命周期阶段绑定,因此命令mvn compile实际上是先定位到compile这一生命周期阶段,然后再根据绑定关系调用maven-compiler-plugin的compile目标;

绑定又分为内置绑定和自定义绑定

内置绑定

内置绑定是为了不需要用户进行配置就可以直接进行执行,如compile就是执行的maven-compiler-plugin:compile

自定义绑定

除了内置绑定之外,用户还可以自己选择将某个插件目标绑定到生命周期的某一个阶段上

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<plugin>
<groupId>org.apache.thrift.tools</groupId>
<artifactId>maven-thrift-plugin</artifactId>
<version>0.1.10</version>
<configuration>
</configuration>
<executions>
<execution>
<id>thrift-sources</id>
<!-- 将compile目标绑定到generate-sources阶段上,也就是在执行generate-sources生命周期的时候,maven-thrift-plugin:compile会执行 -->
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

其实插件的目标本身可能在编写时就已经定义了默认绑定阶段,使用maven-help-plugin来查看插件的详细信息

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
 mvn help:describe -Dplugin=org.apache.thrift.tools:maven-thrift-plugin:0.1.10 -Ddetail



[INFO] org.apache.thrift.tools:maven-thrift-plugin:0.1.10

Name: Maven Thrift Plugin
Description: (no description available)
Group Id: org.apache.thrift.tools
Artifact Id: maven-thrift-plugin
Version: 0.1.10
Goal Prefix: thrift

This plugin has 2 goals:

thrift:compile
Description: This mojo executes the thrift compiler for generating java
sources from thrift definitions. It also searches dependency artifacts for
thrift files and includes them in the thriftPath so that they can be
referenced. Finally, it adds the thrift files to the project as resources
so that they are included in the final artifact.
Implementation: org.apache.thrift.maven.ThriftCompileMojo
Language: java
# 这个就是默认绑定的生命周期阶段,所以上述配置中可以不写<phase>generate-sources</phase>
Bound to phase: generate-sources

Available parameters:

additionalThriftPathElements
(no description available)

excludes
(no description available)

generator (Default: java:hashcode)
This string is passed to the --gen option of the thrift parameter. By
default it will generate Java output. The main reason for this option is
to be able to add options to the Java generator - if you generate
something else, you're on your own.

hashDependentPaths (Default: true)
Required: true
Set this to false to disable hashing of dependent jar paths. This plugin
expands jars on the classpath looking for embedded .thrift files.
Normally these paths are hashed (MD5) to avoid issues with long file
names on windows. However if this property is set to false longer paths
will be used.

includes
(no description available)

localRepository (Default: ${localRepository})
Required: true
This is the path to the local maven repository.

outputDirectory (Default:
${project.build.directory}/generated-sources/thrift)
Required: true
This is the directory into which the .java will be created.

temporaryThriftFileDirectory
Required: true
Expression: ${project.build.directory}/thrift-dependencies
Since thrift cannot access jars, thrift files in dependencies are
extracted to this location and deleted on exit. This directory is always
cleaned during execution.

thriftExecutable (Default: thrift)
Required: true
This is the path to the thrift executable. By default it will search the
$PATH.

thriftSourceRoot (Default: ${basedir}/src/main/thrift)
Required: true
The source directories containing the sources to be compiled.

thrift:testCompile
Description: (no description available)
Implementation: org.apache.thrift.maven.ThriftTestCompileMojo
Language: java
Bound to phase: generate-test-sources

Available parameters:

additionalThriftPathElements
(no description available)

excludes
(no description available)

generator (Default: java:hashcode)
This string is passed to the --gen option of the thrift parameter. By
default it will generate Java output. The main reason for this option is
to be able to add options to the Java generator - if you generate
something else, you're on your own.

hashDependentPaths (Default: true)
Required: true
Set this to false to disable hashing of dependent jar paths. This plugin
expands jars on the classpath looking for embedded .thrift files.
Normally these paths are hashed (MD5) to avoid issues with long file
names on windows. However if this property is set to false longer paths
will be used.

includes
(no description available)

localRepository (Default: ${localRepository})
Required: true
This is the path to the local maven repository.

outputDirectory (Default:
${project.build.directory}/generated-test-sources/thrift)
Required: true
This is the directory into which the .java will be created.

temporaryThriftFileDirectory
Required: true
Expression: ${project.build.directory}/thrift-dependencies
Since thrift cannot access jars, thrift files in dependencies are
extracted to this location and deleted on exit. This directory is always
cleaned during execution.

thriftExecutable (Default: thrift)
Required: true
This is the path to the thrift executable. By default it will search the
$PATH.

thriftTestSourceRoot (Default: ${basedir}/src/test/thrift)
Required: true
The source directories containing the sources to be compiled.

第二种方式

第二种方式是直接在命令行指定要执行的插件目标,例如mvn archetype:generate 就表示调用maven-archetype-plugin的generate目标,这种带冒号的调用方式与生命周期无关,冒号前面是插件前缀,冒号后面是该插件的目标

欢迎关注我的其它发布渠道