SpringBoot服务拆包打包的详细实现过程

 更新时间:2026年02月26日 09:30:40   作者:长路 ㅤ  
文章详细介绍了如何使用Maven插件对SpringBoot服务进行拆包打包,包括使用maven-shade-plugin和maven-jar-plugin配置,以及如何通过assembly.xml实现自定义打包结构,需要的朋友可以参考下

前言

涵盖技术内容:Java后端、大数据、算法、分布式微服务、中间件、前端、运维等。

原始打包

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <includeSystemScope>true</includeSystemScope>
        <!--   指定主类     -->
        <mainClass>com.dtstack.knowledge.ai.server.KnowLedgeServerApplication</mainClass>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-source-plugin</artifactId>
      <executions>
        <execution>
          <id>attach-sources</id>
          <goals>
            <goal>jar-no-fork</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <attach>true</attach>
      </configuration>
    </plugin>
  </plugins>
</build>

编译打包之后为:

拆包实现

引入pom.xml依赖 & 新建assembly.xml

关键点为:

  1. maven-shade-plugin中的<include>com.changlu:knowledge-*</include>,表示对应的com.changlu 指的是groupId,后缀knowledge-*指的是artifactId。
  2. maven-jar-plugin中的指定启动器类:<mainClass>com.dtstack.knowledge.ai.server.KnowLedgeServerApplication</mainClass>
  3. maven-antrun-plugin中指定了拷贝路径。
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.0.0</version>
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring-boot.version}</version>
        </dependency>
      </dependencies>
      <configuration>
        <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
        <createDependencyReducedPom>false</createDependencyReducedPom>
        <filters>
          <filter>
            <artifact>*:*</artifact>
            <excludes>
              <exclude>META-INF/*.SF</exclude>
              <exclude>META-INF/*.DSA</exclude>
              <exclude>META-INF/*.RSA</exclude>
            </excludes>
          </filter>
        </filters>
      </configuration>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <artifactSet>
              <includes>
                <include>com.changlu:knowledge-*</include>
              </includes>
            </artifactSet>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>2.3.2</version>
      <configuration>
        <source>17</source>
        <target>17</target>
      </configuration>
    </plugin>
    
    <!--      后续打包可以识别到${git.branch}      -->
    <plugin>
      <groupId>pl.project13.maven</groupId>
      <artifactId>git-commit-id-plugin</artifactId>
      <version>2.2.6</version>
      <executions>
        <execution>
          <id>get-the-git-infos</id>
          <goals>
            <goal>revision</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <dateFormat>yyyy.MM.dd HH:mm:ss</dateFormat>
        <prefix>git</prefix>
        <verbose>true</verbose>
        <!-- 是否单独生成properties文件 -->
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <!-- 没有 .git 目录时则构建失败,false:继续构建-->
        <failOnNoGitDirectory>false</failOnNoGitDirectory>
        <!-- 生成文件路径-->
           <generateGitPropertiesFilename>${project.basedir}/src/main/resources/git.properties</generateGitPropertiesFilename>
           <gitDescribe>
               <always>false</always>
               <dirty>-dirty</dirty>
               <forceLongFormat>false</forceLongFormat>
           </gitDescribe>
       </configuration>
     </plugin>
  
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <version>3.2.0</version>
         <configuration>
             <archive>
                 <manifestEntries>
                     <Class-Path>.</Class-Path>
                 </manifestEntries>
                 <manifest>
                     <!-- 工程主入口 -->
                     <mainClass>com.dtstack.knowledge.ai.server.KnowLedgeServerApplication</mainClass>
                     <addClasspath>true</addClasspath>
                 </manifest>
             </archive>
             <excludes>
                 <exclude>application.properties</exclude>
                 <exclude>application.yaml</exclude>
                 <exclude>logback.xml</exclude>
             </excludes>
         </configuration>
     </plugin>
     <plugin>
         <artifactId>maven-antrun-plugin</artifactId>
         <version>1.8</version>
         <executions>
             <execution>
                 <id>copy-resources</id>
                 <!-- here the phase you need -->
                 <phase>package</phase>
                 <goals>
                     <goal>run</goal>
                 </goals>
                 <configuration>
                     <tasks>
                         <copy file="${basedir}/target/${project.name}-${project.version}.jar"
                               tofile="${basedir}/target/dtstack-platform/dtstack-platform/dtstack/${project.name}-${git.branch}.jar"/>
                     </tasks>
                 </configuration>
             </execution>
         </executions>
     </plugin>
     <!-- assembly 插件打包-->
     <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-assembly-plugin</artifactId>
         <version>2.6</version>
         <executions>
             <execution>
                 <id>data-module</id>
                 <phase>package</phase>
                 <goals>
                     <goal>single</goal>
                 </goals>
                 <configuration>
                     <descriptors>
                         <descriptor>assembly.xml</descriptor>
                     </descriptors>
                     <outputDirectory>target</outputDirectory>
                     <!-- 打包路径名称-->
                     <finalName>dtstack-platform</finalName>
                     <appendAssemblyId>false</appendAssemblyId>
                 </configuration>
             </execution>
         </executions>
     </plugin>
   </plugins>
</build>

assembly.xml:

<assembly
  xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>dt_public_service</id>
  <includeBaseDirectory>true</includeBaseDirectory>

  <!-- 输出格式为linux下的dir格式格式-->
  <formats>
    <format>dir</format>
  </formats>

  <dependencySets>
    <!-- 依赖所属jar包目录存储-->
    <dependencySet>
      <useProjectArtifact>true</useProjectArtifact>
      <!-- 改动解释:需要提出当前对应的源码包的打包,目的是将对应的依赖拆分出来
      com.changlu 指的是groupId,后缀knowledge-*指的是artifactId
      -->
      <excludes>
        <exclude>com.changlu:knowledge-*</exclude>
      </excludes>
      <outputDirectory>lib</outputDirectory>
      <outputFileNameMapping>${artifact.groupId}-${artifact.artifactId}.${artifact.extension}</outputFileNameMapping>
      <scope>runtime</scope>
    </dependencySet>

  </dependencySets>
</assembly>

执行打包命令与测试

输出的打包后文件目录为,实现了拆包效果:

执行的命令为:

本机:

/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java -cp “/Users/edy/changlu_workspace/daishuyun/材料/2025/2025.6.1黑客马拉松/code/knowledge-ai-chat/knowledge-ai-server/target/dtstack-platform/dtstack-platform/dtstack/:/Users/edy/changlu_workspace/daishuyun/材料/2025/2025.6.1黑客马拉松/code/knowledge-ai-chat/knowledge-ai-server/target/dtstack-platform/dtstack-platform/lib/” com.dtstack.knowledge.ai.server.KnowLedgeServerApplication

服务器:

# -cp 指定目录加载,这里指定两个位置:
#  /opt/tools/ai-chat/knowledge-server/lib/*、
/opt/tools/ai-chat/jdk/jdk-17.0.10+7/bin/java 、/opt/tools/ai-chat/knowledge-server/dtstack/* 
-cp /opt/tools/ai-chat/knowledge-server/lib/*:/opt/tools/ai-chat/knowledge-server/dtstack/* com.dtstack.knowledge.ai.server.KnowLedgeServerApplication

说明:后续真实项目的执行,后使用shell脚本形式来完成启动和关闭。

以上就是SpringBoot服务拆包打包的详细实现过程的详细内容,更多关于SpringBoot服务拆包打包的资料请关注脚本之家其它相关文章!

相关文章

  • SpringBoot制作Docker镜像接入SkyWalking的详细过程

    SpringBoot制作Docker镜像接入SkyWalking的详细过程

    本文通过实际操作完成了如何基于springboot项目接入skyalking的详细过程,并进一步将springboot项目制作容器对接skyalking的详细操作,感兴趣的朋友一起看看吧
    2025-05-05
  • java理论基础Stream管道流状态与并行操作

    java理论基础Stream管道流状态与并行操作

    这篇文章主要为大家介绍了java理论基础Stream管道流状态与并行操作,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-03-03
  • JPA的多表复杂查询的方法示例

    JPA的多表复杂查询的方法示例

    这篇文章主要介绍了JPA的多表复杂查询的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • 关于maven打包出错的解决方案

    关于maven打包出错的解决方案

    这篇文章主要介绍了关于maven打包出错的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04
  • SpringBoot集成Graphql Query实战示例

    SpringBoot集成Graphql Query实战示例

    这篇文章主要为大家介绍了SpringBoot集成Graphql Query实战示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 深入探究Spring底层核心原理

    深入探究Spring底层核心原理

    理解IOC与AOP的实现机制,优化应用性能与可维护性。Spring通过IOC容器管理Bean,AOP实现切面编程,支持事务管理、ORM框架等。深入理解Spring原理,可以帮助我们更好地使用Spring框架,提高开发效率与质量
    2023-04-04
  • Java实现查找Excel数据并高亮显示

    Java实现查找Excel数据并高亮显示

    在日常的开发工作中,我们经常需要处理各种格式的数据,本文将为您详细介绍如何利用强大的第三方库 Spire.XLS for Java,轻松实现 Excel 数据的查找与高亮功能,有需要的小伙伴可以了解下
    2025-09-09
  • logback日志级别设置无效问题及解决

    logback日志级别设置无效问题及解决

    文章总结:文章介绍了在Spring Boot项目中配置日志级别时,优先级问题,通过查阅资料发现,配置文件中的logging.level.xx也能配置日志级别,且优先级比logback.xml高,文章还提供了源码分析,说明了Spring Boot如何处理日志级别配置
    2025-11-11
  • SpringMVC工作原理实例详解

    SpringMVC工作原理实例详解

    这篇文章主要介绍了SpringMVC工作原理实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Java开发环境不再需要配置classpath问题

    Java开发环境不再需要配置classpath问题

    这篇文章主要介绍了Java开发环境不再需要配置classpath问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12

最新评论