Helidon MP/Maven:子项目合并共享库自定义配置源路径问题
解决META-INF/services文件合并问题
方案一:使用maven-shade-plugin的ServicesResourceTransformer
maven-shade-plugin专门提供了ServicesResourceTransformer来处理META-INF/services文件的合并,这比通用的AppendingTransformer更精准。修改子项目的pom.xml配置:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.5.3</version> <!-- 使用最新稳定版 --> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <!-- 专门处理services文件合并 --> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> <!-- 按需配置主类,不需要可删除 --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>your.main.class.path</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
执行mvn clean package后,target目录下的META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource会自动合并共享库和子项目的两个类路径。
方案二:使用maven-resources-plugin手动合并
如果不想用shade插件,可以结合maven-dependency-plugin和maven-resources-plugin手动实现合并:
- 先提取共享库中的services文件到临时目录:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.1</version> <executions> <execution> <id>extract-services</id> <phase>process-resources</phase> <goals> <goal>unpack</goal> </goals> <configuration> <artifactItems> <artifactItem> <groupId>你的共享库groupId</groupId> <artifactId>你的共享库artifactId</artifactId> <version>共享库版本号</version> <includes>META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource</includes> <outputDirectory>${project.build.directory}/tmp-services</outputDirectory> </artifactItem> </artifactItems> </configuration> </execution> </executions> </plugin>
- 合并子项目与共享库的services文件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.3.1</version> <executions> <execution> <id>merge-services</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}/META-INF/services</outputDirectory> <resources> <resource> <directory>${project.build.directory}/tmp-services/META-INF/services</directory> </resource> <resource> <directory>src/main/resources/META-INF/services</directory> <filtering>false</filtering> </resource> </resources> <overwrite>false</overwrite> <!-- 不覆盖,实现内容追加 --> </configuration> </execution> </executions> </plugin>
该方式会先复制共享库的services内容,再将子项目的内容追加到目标文件中。
注意事项
- 确保子项目的META-INF/services文件放在
src/main/resources目录下,避免放在src/test/resources导致打包时被忽略。 - 使用shade插件时,默认会覆盖原jar包,若想保留原jar可添加
<shadedArtifactAttached>true</shadedArtifactAttached>配置,生成单独的shaded jar。
内容的提问来源于stack exchange,提问作者swing1234
相关产品推荐
相关产品推荐

