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

如何借助Maven将内部依赖Jar包合并到主Jar包并分离外部依赖

如何借助Maven将内部依赖Jar包合并到主Jar包并分离外部依赖

没问题,这事儿完全可以通过Maven的插件组合实现,刚好能满足你把内部依赖合并到主Jar、同时保留外部依赖单独放在lib目录的需求。我给你详细说下实现步骤和配置:

核心思路

我们会用到两个关键插件:

  • maven-shade-plugin:负责把你的tools.jar和内部依赖business.jar、utils.jar合并成一个tools-with-dep.jar,同时排除外部依赖(比如commons-cli)。
  • maven-dependency-plugin:调整它的配置,只复制外部第三方依赖到lib目录,不再复制内部的business和utils。

具体配置

替换你原来的插件配置为以下内容(记得根据你的实际项目信息替换占位符):

<plugins>
    <!-- 可选:保留原jar插件,生成未合并的tools.jar;如果不需要可以移除 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>my.mainClass</mainClass>
                    <classpathPrefix>lib/</classpathPrefix>
                </manifest>
            </archive>
            <excludes>
                <exclude>log4j.xml</exclude>
            </excludes>
        </configuration>
    </plugin>

    <!-- 合并内部依赖到主Jar,生成tools-with-dep.jar -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.4</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <!-- 不生成简化的pom文件(如果需要可以改为true) -->
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <!-- 将合并后的Jar作为附属产物生成,后缀为with-dep -->
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>with-dep</shadedClassifierName>
                    <filters>
                        <!-- 包含你的内部依赖 -->
                        <filter>
                            <artifact>你的内部groupId:business</artifact>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>你的内部groupId:utils</artifact>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </filter>
                        <!-- 排除所有其他依赖(外部第三方依赖) -->
                        <filter>
                            <artifact>*:*</artifact>
                            <excludes>
                                <exclude>**/*</exclude>
                            </excludes>
                        </filter>
                    </filters>
                    <transformers>
                        <!-- 保持主类配置和类路径前缀 -->
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>my.mainClass</mainClass>
                            <classpathPrefix>lib/</classpathPrefix>
                        </transformer>
                    </transformers>
                </configuration>
            </execution>
        </executions>
    </plugin>

    <!-- 只复制外部第三方依赖到lib目录 -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
            <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                    <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                    <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                    <!-- 排除内部项目的groupId,只复制外部依赖 -->
                    <excludeGroupIds>你的内部groupId</excludeGroupIds>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

配置要点说明

  1. 替换占位符:把配置里的你的内部groupId改成你实际项目的groupId,比如如果business和utils的groupId都是com.yourcompany,就填这个值。
  2. shade插件的作用:通过filters指定只包含内部的business和utils,排除其他所有依赖,这样合并后的Jar里就只有这三个项目的代码。
  3. dependency插件的调整:用excludeGroupIds排除内部依赖,这样lib目录里就只会出现commons-cli.jar这类外部依赖。

运行结果

执行mvn clean package之后,你的target目录下会生成:

  • tools.jar:未合并的原主Jar(如果保留了maven-jar-plugin)
  • tools-with-dep.jar:合并了business和utils的Jar
  • lib/目录:只包含外部依赖commons-cli.jar

完全符合你想要的结构~

备注:内容来源于stack exchange,提问作者Kiran Kyle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.15 10:44:29