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

如何通过mvn clean package实现普通Jar排除、Uber Jar包含persistence.xml

解决Maven普通Jar排除persistence.xml、Uber-JAR包含该文件的打包问题

问题根源在于默认配置下,maven-shade-plugin会基于maven-jar-plugin生成的普通Jar制作Uber-JAR,导致普通Jar中被排除的META-INF/persistence.xml也会在Uber-JAR中缺失。要实现一次打包生成符合要求的两个Jar,只需调整maven-shade-plugin的打包来源,让它直接从项目编译后的输出目录构建Uber-JAR,绕过普通Jar的排除规则。

1. 配置maven-jar-plugin(生成排除persistence.xml的普通Jar)

保留原有排除规则,确保普通Jar不包含目标XML:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <excludes>
            <exclude>META-INF/persistence.xml</exclude>
        </excludes>
    </configuration>
</plugin>

2. 调整maven-shade-plugin(生成包含persistence.xml的Uber-JAR)

关键配置是让插件直接从项目编译输出目录(${project.build.outputDirectory})打包,而非依赖普通Jar,同时指定classifier区分构件:

<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>
                <!-- 为Uber-JAR设置classifier,生成如xxx-VERSION-uber.jar的文件 -->
                <classifier>uber</classifier>
                <!-- 仅打包当前项目的编译输出,绕过普通Jar的排除规则 -->
                <artifactSet>
                    <includes>
                        <include>${project.groupId}:${project.artifactId}</include>
                    </includes>
                </artifactSet>
                <filters>
                    <!-- 明确包含persistence.xml,同时保留所有其他文件 -->
                    <filter>
                        <artifact>${project.groupId}:${project.artifactId}</artifact>
                        <includes>
                            <include>META-INF/persistence.xml</include>
                            <include>**/*</include>
                        </includes>
                    </filter>
                    <!-- 可选:排除第三方依赖的签名文件,避免打包冲突 -->
                    <filter>
                        <artifact>*:*</artifact>
                        <excludes>
                            <exclude>META-INF/*.SF</exclude>
                            <exclude>META-INF/*.DSA</exclude>
                            <exclude>META-INF/*.RSA</exclude>
                        </excludes>
                    </filter>
                </filters>
                <!-- 可选:如果Uber-JAR是可执行的,添加主类配置 -->
                <!--
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.your.package.MainClass</mainClass>
                    </transformer>
                </transformers>
                -->
            </configuration>
        </execution>
    </executions>
</plugin>

3. 执行打包与部署

  • 运行mvn clean package,会在target目录生成两个Jar:
    • 普通Jar:your-artifact-id-VERSION.jar(已排除persistence.xml)
    • Uber-JAR:your-artifact-id-VERSION-uber.jar(包含persistence.xml及所有依赖)
  • 执行mvn clean deploy时,Maven会自动将两个Jar作为主构件和附属构件,一并部署到Maven中央仓库。

内容的提问来源于stack exchange,提问作者Rui

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 12:50:03