Maven Shade生成Uber Jar时能否将dependency reduced pom写入Jar内META-INF/pom.xml
结论
完全可以实现该需求,Maven Shade插件支持将生成的dependency reduced pom打入uber jar的META-INF/pom.xml路径,其他Maven项目依赖该uber jar时会自动读取该内置pom的依赖配置。
配置步骤
在你的项目pom.xml中,调整maven-shade-plugin的配置如下:
<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> <!-- 显式开启精简pom生成,默认值即为true,可省略 --> <createDependencyReducedPom>true</createDependencyReducedPom> <!-- 指定精简pom的本地生成路径 --> <dependencyReducedPomLocation>${project.build.directory}/reduced-pom.xml</dependencyReducedPomLocation> <!-- 将生成的精简pom打入uber jar的指定路径 --> <resources> <resource> <directory>${project.build.directory}</directory> <includes> <include>reduced-pom.xml</include> </includes> <targetPath>META-INF</targetPath> <destFileName>pom.xml</destFileName> </resource> </resources> <!-- 此处保留你原本的shade其他配置,比如依赖排除、类转换器等 --> </configuration> </execution> </executions> </plugin>
注意事项
- 如果你使用的是3.0以下的低版本maven-shade-plugin,不支持直接在插件配置内加入resources规则,可以额外配置maven-resources-plugin,在
prepare-package阶段将生成的reduced-pom.xml复制到${project.build.outputDirectory}/META-INF/pom.xml,shade打包时会自动将该文件纳入uber jar。 - 如果该uber jar需要发布到Maven仓库,建议同时将这份精简pom作为构件的正式pom发布到仓库,优先级高于jar内置pom,兼容性更好。
内容的提问来源于stack exchange,提问作者felix_w
相关产品推荐
相关产品推荐

