一、介绍
- 官网:https://maven.apache.org/plugins/maven-shade-plugin/
- 功能:功能类似assembly,但支持依赖包的重命名(避免版本冲突)、配置文件的追加(避免覆盖)等,可用于制作fat-jar。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<! --
原始jar名称:xx.jar
最终输出的jar名称:
true: xx-shaded.jar
false: xx.jar并修改原始jar名称为original-xx.jar
-->
<shadedArtifactAttached>false</shadedArtifactAttached>
<!-- 生成名为dependency-reduce-pom.xml,这个xml去掉了原始pom中已被包含进fat-jar的依赖项 -->
<createDependencyReducePom>true</createDependencyReducePom>
<!-- 类重定向 -->
<relocations>
<relocation>
<!-- 指定需要被重命名的包名 -->
<pattern>net.</pattern>
<!-- 指定重命名后的包名 -->
<shadedPattern>xxx.xxx.net.</shadedPattern>
</relocation>
</relocations>
<transformers>
<!-- 所有transformer:https://maven.apache.org/plugins/maven-shade-plugin/usage.html -->
<!-- 等价于manifestEntries下的Main-Class -->
<!-- 为MANIFEST.MF文件添加指定的key和value -->
<manifestEntries>
<Main-Class>com.xxx.xxx.Test</MainClass>
<Build-Number>123</Build-Number>
</manifestEntries>
</transformer>
<!-- 指定要排除或包含的依赖 -->
<artifactSet>
<!-- 默认所有的依赖都会被解压后平铺到最终的fat-jar中,这里指定要排除哪些依赖,格式:groupId:artifactId[[:type]:classifier], 支持“*”和“?”通配符 -->
<excludes>
<exclude>*:hutool-all</exclude>
</excludes>
</artifactSet>
<!-- 更细粒度的控制每个依赖;指定要排除或包含的内容 -->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>java.**</exclude>
<exclude>META-INF/**</exclude>
<exclude>module-info.class</exclude>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.MF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
<exclude>META-INF/LICENSE</exclude>
<exclude>META-INF/NOTICE</exclude>
<exclude>META-INF/DEPENDENCIES</exclude>
<exclude>META-INF/*.txt</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
参考:
- https://blog.csdn.net/h_bpdwn/article/details/133909695?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-133909695-blog-80619481.235v43pc_blog_bottom_relevance_base7&spm=1001.2101.3001.4242.1&utm_relevant_index=3
- https://www.bilibili.com/video/BV1ne411978x/?spm_id_from=333.1007.top_right_bar_window_history.content.click