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

如何让可导入的Spring Boot项目JAR包包含依赖?

Spring Boot项目作为依赖导入并包含依赖库的解决办法

方案一:用maven-shade-plugin打包(推荐)

Spring Boot默认打出来的是带BOOT-INF的可执行jar,没法被其他项目直接依赖。要生成能导入且包含所有依赖的jar,用shade插件最省心:

  1. 修改pom.xml的build插件配置:
<build>
    <plugins>
        <!-- 让Spring Boot生成单独的可执行jar,不影响依赖jar -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
        <!-- 用shade插件打包包含所有依赖的普通jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <!-- 排除签名文件避免冲突,可选 -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. 执行打包命令:
mvn clean package

打包后会生成两个jar:

  • 你的项目名-版本号.jar:普通结构,包含所有依赖,直接导入其他项目就能用
  • 你的项目名-版本号-exec.jar:Spring Boot可执行jar,用来单独运行项目

方案二:调整maven-assembly-plugin配置

如果坚持用assembly插件,得自定义打包结构,让依赖和项目代码都放在jar根目录:

  1. 在src/main/resources/assembly下创建dependency-included.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
    <id>with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>compile</scope>
        </dependencySet>
    </dependencySets>
</assembly>
  1. 修改pom.xml里的assembly插件配置:
<build>
    <plugins>
        <!-- 同样配置Spring Boot插件生成单独的可执行jar -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <classifier>exec</classifier>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/resources/assembly/dependency-included.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,会生成你的项目名-版本号-with-dependencies.jar,这个就是能导入且包含依赖的jar。

关键注意点

  • 一定要给Spring Boot插件加classifier,不然它会覆盖普通jar,导致还是生成带BOOT-INF的文件
  • 核心是把项目代码和依赖都解压到jar根目录,而不是塞进BOOT-INF,这样其他项目才能正常加载类

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 02:35:21