maven 笔记

maven 安装

配置

配置文件路径:

1
/安装目录/conf/settings.xml

注意

  1. 配置多个 mirror 时,只有第一个连接不上时才会依次使用后续的 mirror
  2. 当第一个 mirror 可以连接上,但是找不到对应的包时,不会查找后续的 mirror
  3. 想让多个仓库生效需要在 profiles 节点下配置多个 profile ,而且配置之后要激活

配置文件示例

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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
| This is the configuration file for Maven. It can be specified at two levels:
|
| 1. User Level. This settings.xml file provides configuration for a single user,
| and is normally provided in ${user.home}/.m2/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -s /path/to/user/settings.xml
|
| 2. Global Level. This settings.xml file provides configuration for all Maven
| users on a machine (assuming they're all using the same Maven
| installation). It's normally provided in
| ${maven.conf}/settings.xml.
|
| NOTE: This location can be overridden with the CLI option:
|
| -gs /path/to/global/settings.xml
|
| The sections in this sample file are intended to give you a running start at
| getting the most out of your Maven installation. Where appropriate, the default
| values (values used when the setting is not specified) are provided.
|
|-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>${user.home}/.m2/repository</localRepository>

<!-- interactiveMode
| This will determine whether maven prompts you when it needs input. If set to false,
| maven will use a sensible default value, perhaps based on some other setting, for
| the parameter in question.
|
| Default: true
<interactiveMode>true</interactiveMode>
-->

<!-- offline
| Determines whether maven should attempt to connect to the network when executing a build.
| This will have an effect on artifact downloads, artifact deployment, and others.
|
| Default: false
<offline>false</offline>
-->

<!-- pluginGroups
| This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
| when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
| "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
|-->
<pluginGroups>
<!-- pluginGroup
| Specifies a further group identifier to use for plugin lookup.
<pluginGroup>com.your.plugins</pluginGroup>
-->
</pluginGroups>

<!-- proxies
| This is a list of proxies which can be used on this machine to connect to the network.
| Unless otherwise specified (by system property or command-line switch), the first proxy
| specification in this list marked as active will be used.
|-->
<proxies>
<!-- proxy
| Specification for one proxy, to be used in connecting to the network.
|
<proxy>
<id>optional</id>
<active>true</active>
<protocol>http</protocol>
<username>proxyuser</username>
<password>proxypass</password>
<host>proxy.host.net</host>
<port>80</port>
<nonProxyHosts>local.net|some.host.com</nonProxyHosts>
</proxy>
-->
</proxies>

<!-- servers
| This is a list of authentication profiles, keyed by the server-id used within the system.
| Authentication profiles can be used whenever maven must make a connection to a remote server.
|-->
<servers>
<!-- server
| Specifies the authentication information to use when connecting to a particular server, identified by
| a unique name within the system (referred to by the 'id' attribute below).
|
| NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
| used together.
|
<server>
<id>deploymentRepo</id>
<username>repouser</username>
<password>repopwd</password>
</server>
-->

<!-- Another sample, using keys to authenticate.
<server>
<id>siteServer</id>
<privateKey>/path/to/private/key</privateKey>
<passphrase>optional; leave empty if not used.</passphrase>
</server>
-->
</servers>

<!-- mirrors
| This is a list of mirrors to be used in downloading artifacts from remote repositories.
|
| It works like this: a POM may declare a repository to use in resolving certain artifacts.
| However, this repository may have problems with heavy traffic at times, so people have mirrored
| it to several places.
|
| That repository definition will have a unique id, so we can create a mirror reference for that
| repository, to be used as an alternate download site. The mirror site will be the preferred
| server for that repository.
|-->
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>github-repo</id>
<name>Gthub Repository</name>
<url>https://raw.github.com/thinkgem/repository/master</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<name>Mirror from Maven Repo2</name>
<url>http://repo2.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>uk</id>
<name>Mirror from UK</name>
<url>http://uk.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>

<!-- profiles
| This is a list of profiles which can be activated in a variety of ways, and which can modify
| the build process. Profiles provided in the settings.xml are intended to provide local machine-
| specific paths and repository locations which allow the build to work in the local environment.
|
| For example, if you have an integration testing plugin - like cactus - that needs to know where
| your Tomcat instance is installed, you can provide a variable here such that the variable is
| dereferenced during the build process to configure the cactus plugin.
|
| As noted above, profiles can be activated in a variety of ways. One way - the activeProfiles
| section of this document (settings.xml) - will be discussed later. Another way essentially
| relies on the detection of a system property, either matching a particular value for the property,
| or merely testing its existence. Profiles can also be activated by JDK version prefix, where a
| value of '1.4' might activate a profile when the build is executed on a JDK version of '1.4.2_07'.
| Finally, the list of active profiles can be specified directly from the command line.
|
| NOTE: For profiles defined in the settings.xml, you are restricted to specifying only artifact
| repositories, plugin repositories, and free-form properties to be used as configuration
| variables for plugins in the POM.
|
|-->
<profiles>
<!-- profile
| Specifies a set of introductions to the build process, to be activated using one or more of the
| mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
| or the command line, profiles have to have an ID that is unique.
|
| An encouraged best practice for profile identification is to use a consistent naming convention
| for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
| This will make it more intuitive to understand what the set of introduced profiles is attempting
| to accomplish, particularly when you only have a list of profile id's for debug.
|
| This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.
<profile>
<id>jdk-1.4</id>

<activation>
<jdk>1.4</jdk>
</activation>

<repositories>
<repository>
<id>jdk14</id>
<name>Repository for JDK 1.4 builds</name>
<url>http://www.myhost.com/maven/jdk14</url>
<layout>default</layout>
<snapshotPolicy>always</snapshotPolicy>
</repository>
</repositories>
</profile>
-->

<!--
| Here is another profile, activated by the system property 'target-env' with a value of 'dev',
| which provides a specific path to the Tomcat instance. To use this, your plugin configuration
| might hypothetically look like:
|
| ...
| <plugin>
| <groupId>org.myco.myplugins</groupId>
| <artifactId>myplugin</artifactId>
|
| <configuration>
| <tomcatLocation>${tomcatPath}</tomcatLocation>
| </configuration>
| </plugin>
| ...
|
| NOTE: If you just wanted to inject this configuration whenever someone set 'target-env' to
| anything, you could just leave off the <value/> inside the activation-property.
|
<profile>
<id>env-dev</id>

<activation>
<property>
<name>target-env</name>
<value>dev</value>
</property>
</activation>

<properties>
<tomcatPath>/path/to/tomcat/instance</tomcatPath>
</properties>
</profile>
-->
</profiles>

<!-- activeProfiles
| List of profiles that are active for all builds.
|
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
<activeProfile>anotherAlwaysActiveProfile</activeProfile>
</activeProfiles>
-->
</settings>

maven 使用

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
25
26
27
28
mvn clean package -Dmaven.test.skip=true		-- 跳过单测打包
mvn clean install -Dmaven.test.skip=true -- 跳过单测打包,并把打好的包上传到本地仓库
mvn clean deploy -Dmaven.test.skip=true -- 跳过单测打包,并把打好的包上传到远程仓库
mvn clean -T 1C package -DskipTests -- 跳过单测并发打包 -T 1C 表示CPU线程的倍数

mvn -v //查看版本
mvn archetype:create //创建 Maven 项目
mvn compile //编译源代码
mvn test-compile //编译测试代码
mvn test //运行应用程序中的单元测试
mvn site //生成项目相关信息的网站
mvn package //依据项目生成 jar 文件
mvn install //在本地 Repository 中安装 jar
mvn -Dmaven.test.skip=true //忽略测试文档编译
mvn clean //清除目标目录中的生成结果
mvn clean compile //将.java类编译为.class文件
mvn clean package //进行打包
mvn clean test //执行单元测试
mvn clean deploy //部署到版本仓库
mvn clean install //使其他项目使用这个jar,会安装到maven本地仓库中
mvn archetype:generate //创建项目架构
mvn dependency:list //查看已解析依赖
mvn dependency:tree com.xx.xxx //看到依赖树
mvn dependency:analyze //查看依赖的工具
mvn help:system //从中央仓库下载文件至本地仓库
mvn help:active-profiles //查看当前激活的profiles
mvn help:all-profiles //查看所有profiles
mvn help:effective -pom //查看完整的pom信息

定义 resource 目录

定义 resource 目录

include 或 exclude (引用或排除) resource 目录下的资源时可以直接用相对路径

1
2
3
4
5
6
7
8
9
10
<build>
<resources>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>

maven 清理

maven-clean-plugin

用 maven-clean-plugin 插件清除上一次打包结果

  1. 是否排除默认编译目录 <excludeDefaultDirectories>
  2. 指定额外清除的目录
  3. 官方文档参数

配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- clean package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<configuration>
<!-- 是否排除默认编译目录 -->
<excludeDefaultDirectories>false</excludeDefaultDirectories>
<!-- 额外清除的目录 -->
<filesets>
<fileset>
<directory>${package.dir}</directory>
</fileset>
</filesets>
</configuration>
</plugin>

maven 编译

maven-compiler-plugin

用 maven-compiler-plugin 插件编译代码

  1. 指定 java 版本
  2. 指定 encoding
  3. 设置编译参数 compilerArgs
  4. 官方文档参数

配置示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!-- compile package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- java 版本 -->
<source>${java.version}</source>
<target>${java.version}</target>
<!-- encoding -->
<encoding>${project.build.sourceEncoding}</encoding>
<!-- use this before version 3.1 -->
<!-- <compilerArguments>-->
<!-- <extdirs>src/main/resources/lib</extdirs>-->
<!-- </compilerArguments>-->
<!-- 编译参数 -->
<compilerArgs>
<!-- 引用本地包的目录 -->
<arg>-extdirs</arg>
<arg>src/main/resources/lib</arg>
</compilerArgs>
</configuration>
</plugin>

引用本地包后编译报错

问题: Maven 项目中引用了本地的 jar 包, 编译时报错: compile (default-compile) on project xxx: Compilation failure: Compilation failure: 程序包*.*.*不存在

解决方案:

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
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<excludes>
<exclude>**/old/**</exclude>
</excludes>
<!-- 引入外部包所在的目录 -->
<compilerArguments>
<extdirs>lib</extdirs>
</compilerArguments>
</configuration>
</plugin>

// 3.1版本之后用下面
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<excludes>
<exclude>**/old/**</exclude>
</excludes>
<!-- 引入外部包所在的目录 -->
<compilerArgs>
<arg>-extdirs</arg>
<arg>lib</arg>
</compilerArgs>
</configuration>
</plugin>

maven 打包

maven-jar-plugin

用 maven-jar-plugin 打不包含任何依赖的独立包

  1. 设置 manifest 参数
  2. 自定义输出目录
  3. 自定义输出包的名称
  4. 排除指定目录

配置示例

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
<!-- jar package -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<!-- addClasspath: 是否在manifest文件中添加classpath。默认为false。
如果为true,则会在manifest文件中添加classpath,这样在启动的时候就不用再手动指定classpath了。 -->
<addClasspath>false</addClasspath>
<!-- classpathPrefix: classpath的前缀。 -->
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.jnitest.Main</mainClass>
</manifest>
<manifestEntries>
<Built-By>${user.name}</Built-By>
</manifestEntries>
</archive>
<!-- 自定义输出目录 -->
<outputDirectory>${package.dir}</outputDirectory>
<!-- 自定义输出包的名称 -->
<finalName>${project.name}-${project.version}</finalName>
<!-- 排除指定目录 -->
<excludes>
<exclude>maven/**</exclude>
</excludes>
</configuration>
</plugin>

maven-assembly-plugin

用 maven-assembly-plugin 打自定义格式包

  1. 设置 manifest 参数
  2. 配置 descriptor (assembly 描述文件) 路径
  3. 自定义输出目录
  4. 自定义输出包的名称
  5. 设置打包文件是否带上描述文件的id <appendAssemblyId>
  6. 配置 execution
  7. 官方文档参数

assembly*.xml 文件说明

易混淆参数说明

  1. baseDirectory: 定义包内打包根目录
    • 建议使用 baseDirectory, 原因: baseDirectory 会覆盖 includeBaseDirectory
  2. jar 包 baseDirectory 建议统一设置成 /
assembly-jar1-1.xml
  1. 用缩写 groupId:artifactId 打包不包含依赖的独立 jar 包
  2. 包内是 class 文件
  3. 包内根路径是: /

配置示例

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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 描述文件的id -->
<id>deploy1-1</id>
<!-- 指定程序集格式 -->
<formats>
<format>jar</format>
</formats>

<!-- 包内根路径 -->
<baseDirectory>/</baseDirectory>

<dependencySets>
<dependencySet>
<includes>
<!-- 缩写 groupId:artifactId 或全限定符 groupId:artifactId:type[:classifier]:version -->
<!-- 这里是缩写 -->
<include>com.test:test-project</include>
</includes>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
<exclude>maven/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>
assembly-jar1-2.xml
  1. 用全限定符 groupId:artifactId:type[:classifier]:version 打包不包含依赖的独立 jar 包
  2. 包内是独立 jar 包
  3. 包内根路径是: /${project.name}

配置示例

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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 描述文件的id -->
<id>deploy1-2</id>
<!-- 指定程序集格式 -->
<formats>
<format>jar</format>
</formats>

<!-- 包内根路径 -->
<baseDirectory>/${project.name}</baseDirectory>

<dependencySets>
<dependencySet>
<includes>
<!-- 缩写 groupId:artifactId 或全限定符 groupId:artifactId:type[:classifier]:version -->
<!-- 这里用全限定符 -->
<include>com.test:test-project:jar:${project.version}</include>
</includes>
<scope>runtime</scope>
<unpack>false</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
<exclude>maven/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>
assembly-gz1.xml
  1. 把编译好的 jar 包, 再打包成 tar.gz 格式, 并且把启动脚本、配置文件、jar 包打包到各自目录
  2. 项目 jar 包和依赖的包, 是一个一个独立的包, 统一放在 lib/ 目录下面
  3. 包内根路径是: /

配置示例

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
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/plugins/ASSEMBLY/2.0.0"
xsi:schemaLocation="http://maven.apache.org/plugins/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 描述文件的id -->
<id>deploy1</id>
<!-- 指定程序集格式 -->
<formats>
<format>tar.gz</format>
</formats>

<!-- 包内根路径 -->
<baseDirectory>/</baseDirectory>

<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<!-- <lineEnding>unix</lineEnding>-->
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/version</directory>
<outputDirectory>version</outputDirectory>
<fileMode>0666</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/lib</directory>
<outputDirectory>lib</outputDirectory>
</fileSet>
<fileSet>
<directory>src/main/resources/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>data</directory>
<outputDirectory>data</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
assembly-jar2.xml
  1. 把本程序和所有的依赖包打进一个 jar 包
  2. 包内是 class 文件
  3. 包内根路径是: /

配置示例

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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 描述文件的id -->
<id>deploy2</id>
<!-- 指定程序集格式 -->
<formats>
<format>jar</format>
</formats>

<!-- 包内根路径 -->
<baseDirectory>/</baseDirectory>

<dependencySets>
<dependencySet>
<scope>system</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
<exclude>maven/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
<exclude>maven/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>
assembly-gz2.xml
  1. 把编译好的 jar 包, 再打包成 tar.gz 格式, 并且把启动脚本、配置文件、jar 包打包到各自目录
  2. 项目 jar 包和依赖的包, 是一个包(项目代码和依赖的包打在一个 jar 包里), 放在 lib/ 目录下面
  3. 包内根路径是: /${project.name}

配置示例

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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<!-- 描述文件的id -->
<id>deploy2</id>
<!-- 指定程序集格式 -->
<formats>
<format>tar.gz</format>
</formats>

<!-- 包内根路径 -->
<baseDirectory>/${project.name}</baseDirectory>

<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<!-- <lineEnding>unix</lineEnding>-->
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/version</directory>
<outputDirectory>version</outputDirectory>
<fileMode>0666</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/${profiles.active}</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
<fileSet>
<directory>data</directory>
<outputDirectory>data</outputDirectory>
<fileMode>0644</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>${package.dir}/${project.name}-deploy2.jar</source>
<outputDirectory>lib</outputDirectory>
<destName>${project.name}.jar</destName>
<fileMode>0755</fileMode>
</file>
</files>
</assembly>

其他示例

目标

  1. 想要把本程序和所有的依赖包打进一个 jar 包, 里面是 class 文件

  2. jar 包里面根目录是 “/“, 不是 “/程序名”

  3. 把不需要的资源文件排除出去

  4. 为了方便部署, 把编译好的 jar 包, 再打包成 tar.gz 格式, 并且把启动脚本、配置文件、jar 包打包到各自目录

用的是 maven-assembly-plugin 3.3.0 插件

实现目标 1 和 2

用自带的模式: jar-with-dependencies 可以实现上面的 1、3, 如下:

pom 文件配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cc.apomelo.test.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

同时实现目标 3

有两种方式可以同时实现目标 2

第一种方法
  1. 需要自己定义 Descriptor Format,

  2. 需要在 dependencySet/unpackOptions/excludes/exclude 中定义

  3. 不是在 dependencySet/excludes/exclude 中定义

  4. dependencySet/unpackOptions/excludes/exclude 中定义的是在打包好之后的目录结构中需要排除内容

  5. dependencySet/excludes/exclude 中定义的是需要排除的包, 如: http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/including-and-excluding-artifacts.html

例子: 排除 netty

1
2
3
<excludes>
<exclude>io.netty:netty-all</exclude>
</excludes>

例子如下:

pom 文件配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cc.apomelo.test.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
<finalName>${name}</finalName>
</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
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>deploy</id>
<formats>
<format>jar</format>
</formats>

<!--
表示unpack出的class文件是根目录, 而不是pom文件中定义的工程名
官方定义:
Sets the base directory of the resulting assembly archive.
If this is not set and includeBaseDirectory == true,
${project.build.finalName} will be used instead.
(Since 2.2-beta-1)
-->
<baseDirectory>/</baseDirectory>

<dependencySets>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<!--
排除src/main/resources/proto 目录及目录下所有内容,
最终exclude中如何写,
需要看打包里面基于baseDirectory的相对目录。
-->
<exclude>proto/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>

<!-- 用以下方式不能排除jar包中的proto文件 -->
<!-- <fileSets>-->
<!-- <fileSet>-->
<!-- <directory>src/main/resources</directory>-->
<!-- <outputDirectory>/</outputDirectory>-->
<!-- <excludes>-->
<!-- <exclude>proto/**</exclude>-->
<!-- </excludes>-->
<!-- </fileSet>-->
<!-- </fileSets>-->
</assembly>
第二种方法

pom 文件配置:

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
<resources>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 把不想要打包进jar包的内容过滤 -->
<excludes>
<exclude>test/*</exclude>
<exclude>online/*</exclude>
<exclude>proto/*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cc.apomelo.test.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
</descriptors>
<finalName>${name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

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
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>deploy</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory>/</baseDirectory>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>

同时实现目标 4

pom 文件配置:

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
<resources>
<resource>
<directory>src/main/resources/${profiles.active}</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 把不想要打包进jar包的内容过滤 -->
<excludes>
<exclude>test/*</exclude>
<exclude>online/*</exclude>
<exclude>proto/*</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>cc.apomelo.test.Main</mainClass>
</manifest>
</archive>
<descriptors>
<descriptor>src/main/resources/assembly.xml</descriptor>
<descriptor>src/main/resources/assembly-gz.xml</descriptor>
</descriptors>
<finalName>${name}</finalName>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

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
<?xml version='1.0' encoding='UTF-8'?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>deploy</id>
<formats>
<format>jar</format>
</formats>
<baseDirectory>/</baseDirectory>
<dependencySets>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>

assembly-gz.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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>deploy</id>
<formats>
<format>tar.gz</format>
</formats>

<baseDirectory>/${name}</baseDirectory>

<fileSets>
<fileSet>
<directory>src/main/resources/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
<fileSet>
<directory>src/main/resources/${profiles.active}</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
</fileSets>
<files>
<file>
<source>target/${name}-deploy.jar</source>
<outputDirectory>lib</outputDirectory>
<destName>${name}.jar</destName>
<fileMode>0755</fileMode>
</file>
</files>
</assembly>

打入本地包

可能遇到的问题: Maven 项目中引用了本地的 jar 包, 打包发现打包不进去(已经可以编译通过)

解决方案: pom 文件中加入本地包依赖

pom 文件中增加:

1
2
3
4
5
6
7
<dependency>
<groupId>cc.apomelo.test</groupId> <!-- 随便写, 但是尽量有规律 -->
<artifactId>test-proto</artifactId> <!-- 随便写, 但是尽量有规律 -->
<version>1.0.0</version> <!-- 随便写, 但是尽量有规律 -->
<scope>system</scope> <!-- 一定是system -->
<systemPath>${basedir}/lib/battle.jar</systemPath> <!-- 包所在目录 -->
</dependency>

assembly.xml 文件增加 scope 为 system 的依赖包:

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
<?xml version="1.0" encoding="UTF-8" ?>
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>deploy</id>
<formats>
<format>jar</format>
</formats>

<baseDirectory>/</baseDirectory>

<dependencySets>
<dependencySet>
<!-- 增加scope为system的依赖包 -->
<scope>system</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
<dependencySet>
<scope>runtime</scope>
<unpack>true</unpack>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>/</outputDirectory>
<unpackOptions>
<excludes>
<exclude>proto/**</exclude>
</excludes>
</unpackOptions>
</dependencySet>
</dependencySets>
</assembly>

参考资料

  1. my test project
  2. Apache Maven Assembly Plugin / Assembly
  3. Apache Maven Compiler Plugin / clean:clean
  4. Apache Maven Compiler Plugin / compiler:compile
  5. Apache Maven Compiler Plugin / jar:jar
  6. Maven 引入外部jar包的打包配置
  7. java编译参数大全