maven-shade-plugin
pom
1 | <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-shade-plugin --> |
使用
打包可执行 jar 包
通过设置主类的方式,打包可执行 jar 包。
1 | <plugin> |
目标 jar 包含指定包
1 | <plugin> |
配置示例
1 | <plugin> |
maven-assembly-plugin
pom
1 | <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-assembly-plugin --> |
使用
maven-assembly-plugin
插件的使用需要打包配置(通常命名为 assembly.xml、release.xml 等,具体内容参考配置示例),插件内置了一部分配置,比如 jar-with-dependencies
,使用如下配置,来启用。
1 | <plugin> |
这里需要注意两点。
-
如果指定了预设配置,那么再指定打包配置文件,会被忽略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<plugin>
<!-- ... -->
<executions>
<execution>
<!-- ... -->
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<descriptors> <!-- 会被忽略 -->
<descriptor>src/assemble/assembly.xml</descriptor>
</descriptors>
<!-- ... -->
<finalName>libs</finalName> <!-- 输出路径 -->
<appendAssemblyId>false</appendAssemblyId> <!-- 输出路径不包含 id -->
</configuration>
</execution>
</executions>
</plugin> -
jar-with-dependencies
插件,并不会合并 META-INF 下的内容-
如果,我们依赖的多个包里面定义了同样的 SPI 接口,那么只会保留其中一个的内容,这样程序在运行时,可能会抛出异常,比如
1
Caused by: java.lang.IllegalStateException: Could not find policy 'grpclb'. Make sure its implementation is either registered to LoadBalancerRegistry or included in META-INF/services/io.grpc.LoadBalancerProvider from your jar files.
-
遇到这种情况,需要自定义打包配置文件,参考配置示例
-
非配置文件示例
1 | <plugin> |
配置示例
1 | <plugin> |
assembly.xml
1 | <assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" |
exec-maven-plugin
执行命令。
1 | <build> |
参考
- https://stackoverflow.com/questions/47310215/merging-meta-inf-services-files-with-maven-assembly-plugin
- https://segmentfault.com/a/1190000016237395
- https://stackoverflow.com/questions/38548271/difference-between-maven-plugins-assembly-plugins-jar-plugins-shaded-plugi
- https://github.com/apache/maven-assembly-plugin/blob/master/src/main/resources/assemblies/jar-with-dependencies.xml
- https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html