Maven 打可执行 jar 包时的类覆盖问题
当要把依赖包打包进 jar 时,自己重写了某些依赖包的类,这时会被依赖包的类覆盖(相当于被还原了)
使用 maven-shade-plugin 打包可以排除掉依赖包中的一些类或文件
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.4</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!--自己项目的启动类--> <mainClass>org.apache.catalina.startup.Tomcat</mainClass> </transformer> </transformers> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/**</exclude> <exclude>META-INF/*.*</exclude> <exclude>META-INF/**/*.*</exclude> <exclude>**/module-info.*</exclude> </excludes> </filter> <filter> <!--这里重写了 tomcat-embed-core 包的 Tomcat 类--> <!--就让 tomcat-embed-core 包的 Tomcat 类在打包时排除,让自己写的不被覆盖--> <artifact>org.apache.tomcat.embed:tomcat-embed-core</artifact> <excludes> <exclude>org/apache/catalina/startup/Tomcat*.*</exclude> </excludes> </filter> </filters> <shadedArtifactAttached>true</shadedArtifactAttached> <!--生成 jar 包的名称后缀--> <shadedClassifierName>jar-with-dependencies</shadedClassifierName> <!--是否不打包没有用到的类,可能会有问题,可以自己测试精简后是否正常运行--> <!--<minimizeJar>true</minimizeJar>--> </configuration> </execution> </executions> </plugin>
打可执行 jar 包(含依赖)的几种方式
- maven-jar-plugin 和 maven-dependency-plugin
- maven-assembly-plugin
- maven-shade-plugin
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.spring.FileRename</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
https://www.jianshu.com/p/7a0e20b30401
https://maven.apache.org/plugins/maven-shade-plugin
https://maven.apache.org/plugins/maven-assembly-plugin/usage.html