如何使用maven-assembly-plugin覆盖依赖Jar中META-INF目录的同名配置文件
解决方案
出现该问题的核心原因是maven-assembly-plugin默认的jar-with-dependencies描述符会优先处理依赖包的资源,项目本地的同名资源不会覆盖已写入的依赖资源。你可以通过自定义assembly描述符调整处理规则解决,以下是两种常用方案:
方案1:调整资源处理顺序,用本地文件覆盖依赖同名文件
步骤1:创建自定义assembly描述符
在项目src/main/assembly目录下新建custom-jar-with-dependencies.xml,内容如下:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>jar-with-dependencies</id> <formats> <format>jar</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <!-- 先处理所有依赖的内容 --> <dependencySets> <dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> </dependencySet> </dependencySets> <!-- 后处理项目自身的资源,后写入的文件会覆盖之前的同名文件 --> <fileSets> <fileSet> <directory>${project.build.outputDirectory}</directory> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> </assembly>
步骤2:修改pom中的插件配置
将原有的descriptorRefs替换为自定义描述符引用:
<plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <archive> <manifest> <addDefaultImplementationEntries>true</addDefaultImplementationEntries> <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries> <mainClass>com.github.vatbub.autoHotkeyNounReplacer.CreateAutoHotkeyScriptKt</mainClass> </manifest> <manifestEntries> <Implementation-Build>20210702123553</Implementation-Build> <Custom-Implementation-Build>20210702123553</Custom-Implementation-Build> </manifestEntries> </archive> <!-- 替换原来的descriptorRefs --> <descriptors> <descriptor>src/main/assembly/custom-jar-with-dependencies.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
方案2:直接排除依赖中的目标文件(更精准)
如果你只想排除languagetool依赖中的指定文件,避免影响其他资源,可以在自定义描述符的dependencySet中添加排除规则:
<dependencySet> <outputDirectory>/</outputDirectory> <useProjectArtifact>false</useProjectArtifact> <unpack>true</unpack> <scope>runtime</scope> <!-- 添加排除规则 --> <unpackOptions> <excludes> <exclude>META-INF/org/languagetool/language-module.properties</exclude> </excludes> </unpackOptions> </dependencySet>
配置后依赖中的目标文件不会被打包,只会保留你本地项目中的自定义文件。
内容的提问来源于stack exchange,提问作者vatbub
相关产品推荐
相关产品推荐

