You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 00:48:00