如何使用Maven Shade插件重定位外部库的Properties文件?
问题
我想用Maven Shade插件将第三方库shade并重定位到自己的插件中,目前类已经完成重定位,但运行插件时出现错误。咨询后得知是ServiceLoader配置文件(而非普通Properties文件)未被正确处理。以下是相关配置和报错信息:
依赖配置
<dependencies> <dependency> <groupId>net.kyori</groupId> <artifactId>adventure-text-minimessage</artifactId> <version>4.14.0</version> </dependency> <dependency> <groupId>net.kyori</groupId> <artifactId>adventure-text-serializer-json</artifactId> <version>4.14.0</version> </dependency> <dependency> <groupId>net.kyori</groupId> <artifactId>adventure-text-serializer-gson</artifactId> <version>4.14.0</version> </dependency> </dependencies>
已尝试的Maven Shade插件配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <minimizeJar>true</minimizeJar> <filters> <filter> <artifact>NmsTest:*</artifact> <includes> <include>**</include> </includes> </filter> </filters> <artifactSet> <includes> <include>net.kyori:*</include> <include>NmsTest:*</include> </includes> </artifactSet> <relocations> <relocation> <pattern>net.kyori</pattern> <shadedPattern>nt-shaded.net.kyori</shadedPattern> </relocation> </relocations> </configuration> </plugin>
报错信息
Caused by: java.lang.UnsupportedOperationException: No JsonComponentSerializer implementation found Are you missing an implementation artifact like adventure-text-serializer-gson? Is your environment configured in a way that causes ServiceLoader to malfunction?
解决方案
核心问题是Adventure库依赖的SPI配置文件未被合并和重定位,Shade插件默认不会处理META-INF/services下的文件内容,且minimizeJar可能误删SPI实现类。按以下步骤修改配置:
禁用或调整minimizeJar
minimizeJar=true会自动移除它判定为"未使用"的类,但SPI实现类常被误判。如果不需要精简Jar,直接禁用该选项:<minimizeJar>false</minimizeJar>若必须保留精简,需添加过滤规则确保Adventure的SPI实现类被保留。
添加ResourceTransformer处理SPI文件
在插件配置中加入resourceTransformers,用于合并META-INF/services下的配置文件,并确保内容中的类路径被重定位:<resourceTransformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </resourceTransformers>完善重定位规则
确保重定位规则包含所有相关资源文件(类、配置文件、SPI文件):<relocations> <relocation> <pattern>net.kyori</pattern> <shadedPattern>nt-shaded.net.kyori</shadedPattern> <includes> <include>net/kyori/**/*.class</include> <include>net/kyori/**/*.properties</include> <include>META-INF/services/net.kyori.*</include> </includes> </relocation> </relocations>完整修改后的Shade插件配置
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> <configuration> <minimizeJar>false</minimizeJar> <filters> <filter> <artifact>NmsTest:*</artifact> <includes> <include>**</include> </includes> </filter> <filter> <artifact>net.kyori:*</artifact> <includes> <include>**/*.class</include> <include>**/*.properties</include> <include>META-INF/services/**</include> </includes> </filter> </filters> <artifactSet> <includes> <include>net.kyori:*</include> <include>NmsTest:*</include> </includes> </artifactSet> <relocations> <relocation> <pattern>net.kyori</pattern> <shadedPattern>nt-shaded.net.kyori</shadedPattern> <includes> <include>net/kyori/**</include> <include>META-INF/services/net.kyori.*</include> </includes> </relocation> </relocations> <resourceTransformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </resourceTransformers> </configuration> </plugin>
修改后执行mvn clean package重新构建,即可解决SPI加载失败的问题。
内容的提问来源于stack exchange,提问作者user22204428
相关产品推荐
相关产品推荐

