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

Spring Boot项目Maven构建拆分输出:分离依赖Jar与自定义代码Jar

拆分Spring Boot项目为依赖Jar与自定义代码Jar

针对乡村网络带宽不足的问题,我们可以通过配置Maven插件,将项目拆分为两个Jar包:一个包含所有依赖库(仅需初次或依赖变更时传输),另一个仅包含你的自定义代码(体积小,适合频繁更新)。

修改后的POM.xml配置

替换原项目中<build>节点的内容为以下配置:

<build>
    <plugins>
        <!-- 复制所有依赖到临时目录 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 将依赖打包成单独的聚合Jar -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>package-dependency-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <classifier>dependencies</classifier>
                        <appendAssemblyId>false</appendAssemblyId>
                        <basedirectory>${project.build.directory}/lib</basedirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <!-- 生成仅含自定义代码的可执行Jar -->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <layout>ZIP</layout>
                <includes>
                    <include>
                        <groupId>none</groupId>
                        <artifactId>none</artifactId>
                    </include>
                </includes>
                <!-- 替换为你的Spring Boot启动类全路径 -->
                <mainClass>com.example.appa.AppAApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

构建与使用

  1. 执行构建命令:
mvn clean package
  1. 构建完成后,target目录下会生成三个关键文件:
    • AppA-0.0.1-SNAPSHOT.jar:仅包含自定义代码的普通Jar
    • AppA-0.0.1-SNAPSHOT-exec.jar:可执行的自定义代码Jar(用于启动项目)
    • AppA-0.0.1-SNAPSHOT-dependencies.jar:所有依赖库的聚合Jar
  2. 运行项目:
    • 将两个Jar(exec后缀的可执行Jar和dependencies后缀的依赖Jar)放在同一目录
    • 执行启动命令:
      # Linux/Mac环境
      java -jar AppA-0.0.1-SNAPSHOT-exec.jar
      # Windows环境
      java -jar AppA-0.0.1-SNAPSHOT-exec.jar
      
    或者通过类路径指定:
    # Linux/Mac环境
    java -cp "AppA-0.0.1-SNAPSHOT.jar:AppA-0.0.1-SNAPSHOT-dependencies.jar" com.example.appa.AppAApplication
    # Windows环境
    java -cp "AppA-0.0.1-SNAPSHOT.jar;AppA-0.0.1-SNAPSHOT-dependencies.jar" com.example.appa.AppAApplication
    

注意事项

  • 务必将配置中的com.example.appa.AppAApplication替换为你项目中实际的Spring Boot启动类完整路径
  • 依赖Jar仅在项目的依赖(如Spring Boot版本、第三方库)发生变更时需要重新传输,日常更新仅需传输体积较小的自定义代码Jar
  • 若需要调整依赖Jar的命名规则,可修改maven-assembly-plugin中的classifier参数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 19:04:54