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

如何在Maven项目中合并Jar包内容及相关配置疑问

Maven打包本地Jar到可执行Jar的问题解决

一、解决C.jar未包含A、B.jar内容的问题

你当前配置生成的Jar没包含A、B.jar内容,主要有两个核心原因:

  1. 依赖范围限制:pom里的依赖用了<scope>provided</scope>,这个范围的依赖是预期由运行环境提供的,jar-with-dependencies默认不会将其打包进最终Jar。
  2. 本地Jar未正确引入:如果A、B.jar是项目源码目录下的本地Jar(而非从Maven仓库拉取),当前的dependency配置可能无效——Maven会尝试从仓库查找这些依赖,找不到的话编译阶段就会出错,更别说打包了。

针对这两个问题,给你两种可行方案:

方案1:将本地Jar安装到本地Maven仓库

执行以下命令把A、B.jar安装到本地仓库(替换括号里的参数):

mvn install:install-file -Dfile=/path/to/A.jar -DgroupId=自定义组ID -DartifactId=A -Dversion=1.0 -Dpackaging=jar
mvn install:install-file -Dfile=/path/to/B.jar -DgroupId=自定义组ID -DartifactId=B -Dversion=1.0 -Dpackaging=jar

然后修改pom里的依赖配置,把scope改为compile(默认就是compile,可省略):

<dependencies>
    <dependency>
        <groupId>自定义组ID</groupId>
        <artifactId>A</artifactId>
        <version>1.0</version>
    </dependency>
    <dependency>
        <groupId>自定义组ID</groupId>
        <artifactId>B</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

此时maven-assembly-plugin的jar-with-dependencies会自动把这两个Jar的内容打包进最终的可执行Jar。

方案2:直接引入本地Jar(无需安装到仓库)

如果不想安装到仓库,用system范围的依赖,并让assembly插件包含system范围的依赖:
首先修改dependency配置:

<dependencies>
    <dependency>
        <groupId>自定义组ID</groupId>
        <artifactId>A</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/A.jar</systemPath> <!-- 替换为你本地Jar的实际路径 -->
    </dependency>
    <dependency>
        <groupId>自定义组ID</groupId>
        <artifactId>B</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/lib/B.jar</systemPath>
    </dependency>
</dependencies>

然后修改maven-assembly-plugin的配置,添加<includeSystemScope>true</includeSystemScope>,让它包含system范围的依赖:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4.1</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <includeSystemScope>true</includeSystemScope> <!-- 新增这一行 -->
        <archive>
            <manifest>
                <mainClass>sampler.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

另外注意:你当前pom里同时配置了maven-jar-plugin和maven-assembly-plugin,前者生成的是普通Jar,后者生成的是带依赖的Jar(名称通常是DemoMavenBuildPlugins-0.0.1-SNAPSHOT-jar-with-dependencies.jar),要确认你检查的是assembly生成的这个Jar,而不是maven-jar-plugin生成的Demo.jar。

二、关于是否可以跳过dependency标签的问题

绝对不能跳过。Maven编译项目时需要依赖这些Jar里的类完成编译,如果没有dependency标签,Maven找不到这些类,编译阶段就会报错。你必须通过dependency标签(无论是仓库依赖还是system依赖)告诉Maven这些Jar的位置,才能保证编译和打包正常进行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 23:36:21