如何配置Maven在构建可执行JAR时删除jython-slim依赖内的冗余文件
配置方案
你可以通过Maven插件的文件过滤能力实现需求,不需要修改本地仓库的原始依赖包,以下是两种常用实现方式:
方式一:使用maven-shade-plugin(推荐,适配复杂过滤需求)
如果你需要自定义可执行jar的打包逻辑,直接在shade插件中配置过滤器即可删除指定依赖包内的不需要文件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <!-- 指定要过滤的依赖坐标 --> <artifact>org.python:jython-slim</artifact> <!-- 配置要排除的文件,支持通配符:*匹配单级路径任意字符,**匹配多级目录 --> <excludes> <!-- 示例:排除jython包根目录下的LICENSE文件 --> <exclude>LICENSE</exclude> <!-- 示例:排除jython包内test目录下的所有文件 --> <exclude>test/**</exclude> <!-- 替换为你实际需要删除的文件路径即可 --> </excludes> </filter> <!-- 可添加多个filter配置处理不同依赖的过滤需求 --> </filters> <!-- 保留Spring Boot可执行jar的启动能力,需配置主类 --> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.xxx.你的启动类全限定名</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
方式二:使用spring-boot-maven-plugin配合解压过滤
如果你仍使用官方Spring Boot打包插件,可先解压jython依赖包删除不需要的文件,再重新打包进最终可执行jar:
<plugins> <!-- 第一步:解压jython-slim依赖到临时目录,同时排除不需要的文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <executions> <execution> <id>unpack-jython</id> <phase>process-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>org.python</groupId> <artifactId>jython-slim</artifactId> <version>2.7.2</version> <outputDirectory>${project.build.directory}/jython-temp</outputDirectory> <excludes>**/不需要的文件路径/*</excludes> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin> <!-- 第二步:排除原始的jython-slim依赖,将过滤后的临时目录内容打包进最终jar --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.python</groupId> <artifactId>jython-slim</artifactId> </exclude> </excludes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <folders> <folder>${project.build.directory}/jython-temp</folder> </folders> </configuration> </execution> </executions> </plugin> </plugins>
注意事项
- 所有路径均为
jython-slim-2.7.2.jar包内部的相对路径,你可以先解压原始jar包确认需要删除的文件的具体路径再配置 - 通配符
*匹配单级目录下的任意字符,**匹配任意多级目录
内容的提问来源于stack exchange,提问作者Jijo Antony
相关产品推荐
相关产品推荐

