Maven使用

Maven插件简述

Maven中有两种类型的插件

  • Build plugins 是在项目创建时执行的插件,位于<build/>标签内
  • Reporting plugins是在网站发布时执行的插件,位于<reporting/>标签内

插件原理

所有的Maven插件的配置都来自于<configuration>标签,每个<configuration>都会映射到Mojo类(Mojo类有插件具体执行工作的类)中你所指定的变量或set方法,具体案例如下

创建一个插件用于访问特定的url,并需要配置url和超时时间等配置,创建步骤如下

  1. 创建Maven工程,可使用quickstar快速创建

  2. 修改pom文件

    1
    2
    3
    4
    5
    6
    7
    8
    <!-- 以往我们这里都是控制打包类型如,jar或者war,这里我们要生成插件因此需要修改为如下配置 -->
    <packaging>maven-plugin</packaging>
    <!-- 添加插件依赖以便我们实现AbstractMojo抽象类来开发我们自己的插件 -->
    <dependency>
    <groupId>org.apache.maven</groupId>
    <artifactId>maven-plugin-api</artifactId>
    <version>3.5.0</version>
    </dependency>
  3. 实现AbstractMojo接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /**
    * @goal query
    */
    public class MyQueryMojo
    extends AbstractMojo
    {
    //配置获取
    @Parameter(property = "query.url", required = true)
    private String url;

    @Parameter(property = "timeout", required = false, defaultValue = "50")
    private int timeout;

    @Parameter(property = "options")
    private String[] options;
    //具体插件执行的方法
    public void execute()
    throws MojoExecutionException
    {
    ...
    }
    }
  4. 执行mvn clean install将会在本地.m2库中生成你的插件

  5. 具体如何使用插件如下,可对应步骤3了解插件配置如何映射

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    oject>
    ...
    <build>
    <plugins>
    <plugin>
    <artifactId>maven-myquery-plugin</artifactId>
    <version>1.0</version>
    <configuration>
    <url>http://www.foobar.com/query</url>
    <timeout>10</timeout>
    <options>
    <option>one</option>
    <option>two</option>
    <option>three</option>
    </options>
    </configuration>
    </plugin>
    </plugins>
    </build>
    ...
    </project>

    有时,我们想通过命令行界面来控制url参数以便我们不去修改pom.xml的configuration配置

    • pom配置中的url修改为${query.url}
    • 执行命令mvn myquery:query -Dquery.url=http://maven.apache.org

    6.当有复杂参数时,需要定义实体类封装参数

    1
    2
    3
    4
    5
    6
    <configuration>
    <person implementation="com.mycompany.mojo.query.SuperPerson">
    <firstName>Jason</firstName>
    <lastName>van Zyl</lastName>
    </person>
    </configuration>

常用插件

关于Maven插件的原理参考

https://blog.csdn.net/jerry010101/article/details/93601563

插件文档:

  1. http://maven.apache.org/plugins/index.html
  2. http://mojo.codehaus.org/plugins.html

maven内置配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
${basedir}表示项目根目录,即包含pom.xml文件的目录;

${version}表示项目版本;

${project.basedir}同${basedir};

${project.baseUri}表示项目文件地址;

${maven.build.timestamp}表示项目构件开始时间;

${maven.build.timestamp.format}表示属性${maven.build.timestamp}的展示格式,默认值为yyyyMMdd-HHmm,可自定义其格式,其类型可参考java.text.SimpleDateFormat。
 ${project.build.directory}表示主源码路径;

 ${project.build.sourceEncoding}表示主源码的编码格式;

 ${project.build.sourceDirectory}表示主源码路径;

 ${project.build.finalName}表示输出文件名称;

${project.version}表示项目版本,与${version}相同;

${project.xxx} 当前pom文件的任意节点的内容
${env.xxx} 获取系统环境变量。
${settings.xxx} 指代了settings.xml中对应元素的值。

编译

maven-compiler-plugin

设置maven编译的jdk版本,maven3默认用jdk1.5,maven2默认用jdk1.3

1
2
3
4
5
6
7
8
9
10
11
<plugin>     
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source> <!-- 源代码使用的JDK版本 -->
<target>1.8</target> <!-- 需要生成的目标class文件的编译版本 -->
<encoding>UTF-8</encoding><!-- 字符集编码 -->
<skipTests>true</skipTests><!-- 跳过测试 -->
</configuration>
</plugin>

打包

Maven Archiver

Maven Archiver是主要的控制打包功能的配置

  • 打包配置
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
 <archive>
<!-- 是否包含pom.xml和pom.properties文件:默认是true -->
<addMavenDescriptor>false</addMavenDescriptor>
<!-- 是否激活压缩:默认是true -->
<compress>true</compress>
<!-- 是否强制创建存档(会对比新旧时间戳,若新建存档时间较晚则更新jar包,反之亦然):默认为true -->
<forced/>
<!-- 是否包含INDEX.LIST文件:默认为true -->
<index/>
<!-- 将定义的pom.properties文件用于覆盖自动生成的pom.properties文件(只有在addMavenDescriptor 设置为true时有效) -->
<!--
自动生成的pom。properties默认包含以下属性
artifactId=${project.artifactId}
groupId=${project.groupId}
version=${project.version}
-->
<pomPropertiesFile/>
<!-- 设置MANIFEST.MF文件路径 -->
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifest>
<!-- 是否要把第三方jar放到manifest的classpath中:默认false -->
<addClasspath>true</addClasspath>
<!--
用于为mainfest添加如下信息
Created-By: Maven Archiver ${maven-archiver.version}
Build-Jdk-Spec: ${java.specification.version}
-->
<addDefaultEntries/>
<!--
用于为mainfest添加如下信息
Implementation-Title: ${project.name}
Implementation-Version: ${project.version}
Implementation-Vendor: ${project.organization.name}
-->
<addDefaultImplementationEntries/>
<!--
Specification-Title: ${project.name}
Specification-Version: ${project.artifact.selectedVersion.majorVersion}.${project.artifact.selectedVersion.minorVersion}
Specification-Vendor: ${project.organization.name}
-->
<addDefaultSpecificationEntries/>
<!--
Build-Tool: ${maven.build.version}
Build-Jdk: ${java.version} (${java.vendor})
Build-Os: ${os.name} (${os.version}; (${os.arch})
-->
<addBuildEnvironmentEntries/>
<!-- 是否开启扩展清单-->
<addExtensions/>
<!--
设置classPath的布局类型
simple:(默认)
repository:与maven的classPath一致
custom:与customClasspathLayout配置有关
-->
<classpathLayoutType/>
<!-- 生成的manifest中classpath的前缀,
因为要把第三方jar放到lib目录下,
所以classpath的前缀是lib/,默认为空 -->
<classpathPrefix>lib/.</classpathPrefix>
<!-- 所传的表达式会被classPath关联的对象清单所识别 -->
<customClasspathLayout/>
<!-- 设置主类 -->
<mainClass/>
<!-- 包的入口 -->
<packageName/>
<!--是否使用唯一时间戳快照版本,而不是-快照版本:默认为true-->
<useUniqueVersions/>
</manifest>
<manifestEntries>
<key>value</key>
</manifestEntries>
<manifestSections>
<manifestSection>
<name/>
<manifestEntries>
<key>value</key>
</manifestEntries>
</manifestSection>
</manifestSections>
</archive>

maven-jar-plugin

  • 普通jar包
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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- 是否包含pom.xml和pom.properties文件:默认是true -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<!-- 是否要把第三方jar放到manifest的classpath中:默认false -->
<addClasspath>true</addClasspath>
<!-- 生成的manifest中classpath的前缀,
因为要把第三方jar放到lib目录下,
所以classpath的前缀是lib/,默认为空 -->
<classpathPrefix>lib/.</classpathPrefix>
</manifest>
</archive>
<excludes>
<exclude>**/bookFlight/</exclude>
<exclude>**/database/</exclude>
<exclude>**/debug/</exclude>
<exclude>**/IBE/</exclude>
<exclude>*.xml</exclude>
</excludes>
</configuration>
</plugin>

maven-assembly-plugin

  • maven-assembly-plugin的用途是制作项目分发包,该分发包可能包含了项目的可执行文件、源代码、readme、平台脚本等等。 maven-assembly-plugin支持各种主流的格式如zip、tar.gz、jar和war等,具体打包哪些文件是高度可控的,例如用户可以 按文件级别的粒度、文件集级别的粒度、模块级别的粒度、以及依赖级别的粒度控制打包,此外,包含和排除配置也是支持的。maven-assembly- plugin要求用户使用一个名为assembly.xml的元数据文件来表述打包,它的single目标可以直接在命令行调用,也可以被绑定至生命周期。
  • assembly插件打压缩包(jar)
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
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--使用默认assembly组件-->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--使用自定义assembly组件-->
<!--
<descriptors>
<descriptor>src/main/resources/assembly/*.xml</descriptor>
</descriptors>
-->
<!--assembly插件可支持archive配置用于指定可执行jar包的主类-->
<archive>
<manifest>
<mainClass>org.sample.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<!-- 将组装绑定到maven生命周期的哪一阶段 -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<!-- 指定assembly插件的打包方式-->
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
  • 当有个性化配置时可参考Assembly,,改方案需要该插件支持<descriptor/>

Assembly配置

  • assembly文件是插件的配置,通过配置的变更来实现不同的自定义打包方式和类型等
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
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0
http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!--用于表示打包后的包名,且还可以在发布时座位组件的标识-->
<id/>
<!--打包类型
"zip" - Creates a ZIP file format
"tar" - Creates a TAR format
"tar.gz" or "tgz" - Creates a gzip'd TAR format
"tar.bz2" or "tbz2" - Creates a bzip'd TAR format
"tar.snappy" - Creates a snappy'd TAR format
"tar.xz" or "txz" - Creates a xz'd TAR format
"jar" - Creates a JAR format
"dir" - Creates an exploded directory format
"war" - Creates a WAR format
-->
<formats/>
<!--是否将包含根目录,若设为false则压缩包中只包含目录中的内容,而不是从根目录开始:默认true-->
<includeBaseDirectory/>
<!--当includeBaseDirectory标签设为true时,需要为根目录命名,若未命名则默认用项目名称为之命名${project.build.finalName} -->
<baseDirectory/>
<!---->
<includeSiteDirectory/>
<!--用于将多个文件内容定义在同一个标识下,例如将几个文件的内容合并到一个文件中去或为特殊的文件自定义内容-->
<!--有以下几种配置可参考:http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/using-container-descriptor-handlers.html-->
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName/>
<configuration/>
</containerDescriptorHandler>
</containerDescriptorHandlers>
<!--允许将多个Module和项目加入到一个pom.xml中-->
<moduleSets>
<moduleSet>
<!--如果设置为true插件将会集中处理包含在内的所有项目工程-->
<useAllReactorProjects/>
<!--是否包含子工程-->
<includeSubModules/>
<!--项目坐标-->
<includes/>
<!--除外的项目坐标-->
<excludes/>
<!--包含的source文件-->
<sources>
<useDefaultExcludes/>
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<fileSets>
<fileSet>
<useDefaultExcludes/>
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<directory/>
<lineEnding/>
<filtered/>
<nonFilteredFileExtensions/>
</fileSet>
</fileSets>
<includeModuleDirectory/>
<excludeSubModuleDirectories/>
<outputDirectoryMapping/>
</sources>
<!--包含的二进制文件-->
<binaries>
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<attachmentClassifier/>
<includeDependencies/>
<dependencySets>
<dependencySet>
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<useStrictFiltering/>
<outputFileNameMapping/>
<unpack/>
<unpackOptions>
<includes/>
<excludes/>
<filtered/>
<nonFilteredFileExtensions/>
<lineEnding/>
<useDefaultExcludes/>
<encoding/>
</unpackOptions>
<scope/>
<useProjectArtifact/>
<useProjectAttachments/>
<useTransitiveDependencies/>
<useTransitiveFiltering/>
</dependencySet>
</dependencySets>
<unpack/>
<unpackOptions>
<includes/>
<excludes/>
<filtered/>
<nonFilteredFileExtensions/>
<lineEnding/>
<useDefaultExcludes/>
<encoding/>
</unpackOptions>
<outputFileNameMapping/>
</binaries>
</moduleSet>
</moduleSets>
<!--文件设置,将制定的文件或集合写入压缩包内-->
<fileSets>
<fileSet>
<!--是否使用默认排查设置,如匹配CVS或Subversion元数据文件时排除:默认为true-->
<useDefaultExcludes/>
<!--输出路径,这个路径从压缩包的根目录开始-->
<outputDirectory/>
<!--设置匹配的包含文件-->
<includes/>
<!--设置排除文件-->
<excludes/>
<!--类似linux系统的权限设置,如:Read = 4, Write = 2, and Execute = 1;默认为0644-->
<!--更多权限可查看http://www.onlamp.com/pub/a/bsd/2000/09/06/FreeBSD_Basics.html-->
<fileMode/>
<!--和fileMode类似-->
<directoryMode/>
<!--限定要选取的目录,可填绝对路径或相对路径-->
<directory/>
<!--设置换行符类型-->
<!--
"keep" - Preserve all line endings
"unix" - Use Unix-style line endings (i.e. "\n")
"lf" - Use a single line-feed line endings (i.e. "\n")
"dos" - Use DOS-/Windows-style line endings (i.e. "\r\n")
"windows" - Use DOS-/Windows-style line endings (i.e. "\r\n")
"crlf" - Use carriage-return, line-feed line endings (i.e. "\r\n")
-->
<lineEnding/>
<!--是否用配置中的属性来过滤选中的文件,默认为:false-->
<filtered/>
<!--附加文件扩展名不应用过滤-->
<nonFilteredFileExtensions/>
</fileSet>
</fileSets>
<!--允许操作fileset除外的一些单个文件-->
<files>
<file>
<source/>
<sources/>
<outputDirectory/>
<destName/>
<fileMode/>
<lineEnding/>
<filtered/>
</file>
</files>
<!--可将项目之外的依赖文件加入到assembly插件压缩包中-->
<dependencySets>
<dependencySet>
<!--输出保存路径-->
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<useStrictFiltering/>
<!--为所有依赖设置映射符号,默认为:${artifact.artifactId}-${artifact.version}${dashClassifier?}.${artifact.extension}-->
<outputFileNameMapping/>
<!--若设为true则可以解压压缩包中的内容到新的压缩包中,目前支持解压jar, zip, tar.gz, 和 tar.bz 默认:false-->
<unpack/>
<unpackOptions>
<includes/>
<excludes/>
<filtered/>
<nonFilteredFileExtensions/>
<lineEnding/>
<useDefaultExcludes/>
<encoding/>
</unpackOptions>
<!--依赖的范围 默认:runtime-->
<scope/>
<!--定义这个依赖项目构建过程中生产的组件是否要包含在此依赖集中 默认:true-->
<useProjectArtifact/>
<!--同上,定义一些附加组件的设置 默认:false-->
<useProjectAttachments/>
<!--当有依赖传递时,是否将传递的依赖也接入到压缩包中;-->
<useTransitiveDependencies/>
<!--依赖传递过滤设置 默认:false-->
<useTransitiveFiltering/>
</dependencySet>
</dependencySets>
<!--为assembly插件配置maven仓库组件-->
<repositories>
<repository>
<outputDirectory/>
<includes/>
<excludes/>
<fileMode/>
<directoryMode/>
<includeMetadata/>
<groupVersionAlignments>
<groupVersionAlignment>
<id/>
<version/>
<excludes/>
</groupVersionAlignment>
</groupVersionAlignments>
<scope/>
</repository>
</repositories>
<componentDescriptors/>
</assembly>

container–file-aggregator

  • 该组件负责将读取<filePattern/>中的表达式对应的文件,将其内容合并,最终输出到<outputPath/>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
....
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>file-aggregator</handlerName>
<configuration>
<filePattern>.*/file.txt</filePattern>
<outputPath>file.txt</outputPath>
</configuration>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>

container–metaInf-services

  • 这个组件会匹配所有的META-INF/services目录中的文件并将它们合并到一个META-INF/services目录下的文件中
1
2
3
4
5
6
7
8
9
10
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
....
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-services</handlerName>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>

container–metaInf-spring

  • 该组件类似metaInf-services会将文件名为META-INF/spring.开头的文件合并
1
2
3
4
5
6
7
8
9
10
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
....
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>metaInf-spring</handlerName>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>

container–plexus

  • 该组件会同上,将所有META-INF/plexus/components.xml文件内容合并,并输出到一个有效的META-INF/plexus/components.xml文件中
1
2
3
4
5
6
7
8
9
10
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
....
<containerDescriptorHandlers>
<containerDescriptorHandler>
<handlerName>plexus</handlerName>
</containerDescriptorHandler>
</containerDescriptorHandlers>
</assembly>

Assembly样例

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
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<id>full</id>
<!-- 最终打包成一个用于发布的zip文件 -->
<formats>
<format>zip</format>
</formats>

<!-- 把依赖jar包打包进Zip压缩文件的lib目录下 -->
<dependencySets>
<dependencySet>
<!--不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录-->
<useProjectArtifact>false</useProjectArtifact>

<!-- 第三方jar打包进Zip文件的lib目录下, -->
<!-- 注意此目录要与maven-jar-plugin中classpathPrefix指定的目录相同, -->
<!-- 不然这些依赖的jar包加载到ClassPath的时候会找不到-->
<outputDirectory>lib</outputDirectory>

<!-- 第三方jar不要解压-->
<!--<unpack>false</unpack>-->
</dependencySet>
</dependencySets>

<!-- 文件设置,你想把哪些文件包含进去,或者把某些文件排除掉,都是在这里配置-->
<fileSets>
<!-- 把项目自己编译出来的可执行jar,打包进zip文件的根目录 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>

<!--
把项目readme说明文档,打包进zip文件根目录下
(这里针对目录document/readme.txt文件)
${projet.document.directory}是pom.xml中自己配置的
-->
<fileSet>
<directoryl>${projet.document.directory}</directoryl>
<outputDirectory></outputDirectory>
<includes>
<include>readme.*</include>
</includes>
</fileSet>

<!--
把项目相关的说明文档(除了readme文档),
打包进zip文件根目录下的document目录
(这里针对document/exclode.txt文件)
${project.document.directory}是在pom.xml中自己配置的
-->
<fileSet>
<directory>${project.document.directory}</directory>
<outputDirectory>document</outputDirectory>
<excludes>
<exclude>readme.*</exclude>
</excludes>
</fileSet>

<!--
把项目的脚本文件目录(src/main/scripts )中的启动脚本文件,
打包进zip文件的根目录
(这里针对的是src/scripts/execute/include-file.sh文件)
${project.script.execute.directory}
-->
<fileSet>
<directory>${project.script.execute.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*</include>
</includes>
</fileSet>

</fileSets>
</assembly>

maven-shade-plugin

使用maven-assembly-plugin容易产生一个错误,就是在多个依赖冲突的时候无法解决。不过使用 maven-shade-plugin 生成一个 uber-jar,可以解决依赖间的冲突,适用于Spring Framework。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
  • 该插件有以下几个默认的配置功能可供选择,命名空间都为org.apache.maven.plugins.shade.resource

ApacheLicenseResourceTransformer

一些服务提供者或jar包产商在提供的jar包中都会提供他们的权限在META-IN文件夹中,它们都会被习惯性的被命名为LICENSELICENSE.txt,当合并这些依赖时,添加这些资源时就会产生冲突而报错;ApacheLicenseResourceTransformer配置则可以忽视掉这些名字的证书

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
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ApacheLicenseResourceTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

AppendingTransformer

一些jar包包含一些同名资源文件例如(propertes文件),你可以合并输出它以避免冲突

  • 如下案例将spring-contextplexus-spring 这两个jar包的META-INF/spring.handlers文件,该文件是用于处理XML schema的命名空间的
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
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>

其它插件

maven-archetype-plugin

http://maven.apache.org/archetype/maven-archetype-plugin/

Archtype指项目的骨架,Maven初学者最开始执行的Maven命令可能就是mvn archetype:generate,这实际上就是让maven-archetype-plugin生成一个很简单的项目骨架,帮助开发者快速上手。可能也有人看到一些文档写了mvn archetype:create, 但实际上create目标已经被弃用了,取而代之的是generate目标,该目标使用交互式的方式提示用户输入必要的信息以创建项目,体验更好。 maven-archetype-plugin还有一些其他目标帮助用户自己定义项目原型,例如你由一个产品需要交付给很多客户进行二次开发,你就可以为 他们提供一个Archtype,帮助他们快速上手。

1
2
3
4
5
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</version>
</plugin>

maven-resource-plugin

该插件处理项目的资源文件拷贝到输出目录。可以分别处理main resources 和 test resources。

1
2
3
4
5
6
7
8
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

参考文档:

  1. https://blog.csdn.net/daerzei/java/article/details/82883472
  2. https://blog.csdn.net/jerry010101/java/article/details/93601563
  3. https://blog.csdn.net/liuzhuojava/article/details/86691218
  4. https://maven.apache.org/plugins

Maven使用
https://andrewjiao.github.io/2019/08/14/Maven发布及插件使用/
作者
Andrew_Jiao
发布于
2019年8月14日
许可协议