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

求助:Maven无法将依赖项添加至Jar包并部署至仓库

解决Maven打包包含依赖的Jar并部署至仓库的问题

问题根源

你当前配置的maven-dependency-plugin只会把指定依赖(比如HikariCP)复制到localrepo目录,不会将依赖合并到你的主Jar包中,所以生成的Jar只有自身代码。你之前尝试的maven-assembly-plugin和maven-shade-plugin才是实现“包含依赖的Jar”的正确工具,只是配置或执行逻辑有误。

方案1:使用maven-shade-plugin(推荐)

Shade插件会将依赖包的字节码合并到你的主Jar中,还能处理依赖冲突(比如重命名包),打包后的Jar会覆盖原主Jar,部署时默认上传的就是带依赖的版本。

完整配置示例:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.5.1</version> <!-- 建议使用最新稳定版 -->
        <executions>
            <execution>
                <phase>package</phase> <!-- 绑定到package阶段,deploy会自动触发 -->
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <!-- 可选:如果需要生成可执行Jar,指定主类 -->
                    <!--
                    <transformers>
                        <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>你的主类全路径,比如com.example.Main</mainClass>
                        </transformer>
                    </transformers>
                    -->
                    <!-- 可选:只打包指定依赖(比如只包含HikariCP) -->
                    <!--
                    <artifactSet>
                        <includes>
                            <include>com.zaxxer:HikariCP</include>
                        </includes>
                    </artifactSet>
                    -->
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

方案2:使用maven-assembly-plugin

Assembly插件通过描述符定义打包内容,预定义的jar-with-dependencies可以快速生成包含所有依赖的Jar(默认会生成带-jar-with-dependencies后缀的Jar)。

完整配置示例:

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.7.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef> <!-- 预定义描述符 -->
                    </descriptorRefs>
                    <!-- 可选:指定主类生成可执行Jar -->
                    <!--
                    <archive>
                        <manifest>
                            <mainClass>你的主类全路径</mainClass>
                        </manifest>
                    </archive>
                    -->
                    <!-- 可选:过滤只打包指定依赖 -->
                    <!--
                    <artifactSet>
                        <includes>
                            <include>com.zaxxer:HikariCP</include>
                        </includes>
                    </artifactSet>
                    -->
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

正确执行命令

不需要额外加dependency:copy-dependencies目标,直接执行以下命令即可:

clean deploy -Dmaven.resolver.transport=wagon

插件已经绑定到package阶段,deploy会自动先执行package,触发插件打包包含依赖的Jar,再将Jar部署到Maven仓库。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:53:14