当要把依赖包打包进 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>