使用 assembly 对 SpringBoot 代码打包


1. 背景

最近向服务器上部署 spring boot项目时,会遇到一个问题,那就是 spring boot 打包时,会将自己写的代码和项目的所有依赖文件打成一个可执行的 jar 包。通常我们的项目都是运行在服务器上的,当项目更新时,每次都要向服务器上传这个包。如果项目的依赖包很多,那么这个文件就会非常大。最近堡垒机网络不稳定, 上传包的时候总是失败, 导致部署效率很低。

  • 默认的 maven 配置
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

如果能将项目外部依赖和自己的代码包分开打包,当修改项目后,只需要再次覆盖修改后的包,那就可以完美解决这个问题

2. 解决方案

可以使用 maven 的assembly插件 将项目代码与依赖分别打包。

2.1 Assembly 介绍

Maven Assembly 插件是 Maven 的一个插件,用于将多个 Maven 项目构建成一个归档文件(如 jar、zip、tar.gz 等),方便部署和发布。

该插件通过描述归档文件的内容和组成部分,可以将一个项目的多个模块打包成一个单独的归档文件,也可以将一个模块的多个子模块打包成一个单独的归档文件。该插件支持不同类型的归档文件,例如 jar、zip、tar.gz 等,还可以支持自定义的归档文件类型。

Maven Assembly 插件的使用通常需要配置一个 XML 文件,该文件描述了归档文件的组成部分和各个部分的打包规则。通过该文件可以定义哪些文件需要包含在归档文件中,以及这些文件的排列方式、文件名和目录结构等。同时,还可以通过 Maven 的过滤器机制对文件进行过滤和替换,以适应不同的环境需求。

使用 Maven Assembly 插件,可以将多个项目、模块或者组件组合成一个归档文件,实现一次性部署和发布的效果,方便开发和维护。

2.2 创建配置文件

在项目中创建一个文件与配置文件,src/main/assembly/assembly.xml

2.3 配置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">

    <!--
        必须写,否则打包时会有 assembly ID must be present and non-empty 错误
        这个名字最终会追加到打包的名字的末尾,如项目的名字为 speed-api-0.0.1-SNAPSHOT,
        则最终生成的包名为 speed-api-0.0.1-SNAPSHOT-bin.zip
     -->
    <id>bin</id>

    <!-- 打包后的文件格式,可以是zip,tar,tar.gz,tar.bz2,jar,war,dir -->
    <formats>
        <format>zip</format>
    </formats>

    <!-- 压缩包下是否生成和项目名相同的根目录 -->
    <includeBaseDirectory>false</includeBaseDirectory>

    <dependencySets>
        <dependencySet>
            <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 -->
            <useProjectArtifact>false</useProjectArtifact>
            <outputDirectory>lib</outputDirectory>
            <unpack>false</unpack>
        </dependencySet>
    </dependencySets>

    <fileSets>
        <!-- 把项目相关的说明文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
        </fileSet>

        <!-- 把项目的配置文件,打包进zip文件的config目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
        </fileSet>

        <!-- 把项目的脚本文件,打包进zip文件的bin目录 -->
        <fileSet>
            <directory>${project.basedir}/src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>

        <!-- 把项目自己编译出来的jar文件,打包进zip文件的根目录 -->
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

2.4 pom.xml 的配置

<build>
    <plugins>
        <!-- 指定启动类,将依赖打成外部jar包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <!-- 生成的jar中,不要包含pom.xml和pom.properties这两个文件 -->
                    <addMavenDescriptor>false</addMavenDescriptor>
                    <manifest>
                        <!-- 是否要把第三方jar放到manifest的classpath中 -->
                        <addClasspath>true</addClasspath>
                        <!-- 外部依赖jar包的最终位置 -->
                        <classpathPrefix>lib/</classpathPrefix>
                        <!-- 项目启动类 -->
                        <mainClass><!-- 这里写你的主类 --> </mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <!-- 使用assembly打包 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <!-- assembly配置文件位置 -->
                    <descriptor>src/main/assembly/assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>


        <!-- 打包发布时,跳过单元测试 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

2.5 打包效果

target 文件夹内容

压缩文件中的内容

3. 项目部署与运行

# 1. 将压缩文件上传到服务器
[root@node1 opt]# ll
total 543916
-rw-r--r-- 1 root root 474052725 Mar 17 15:42 flink-1.16.1-bin-scala_2.12.tgz
-rw-r--r-- 1 root root  82900972 Mar 17 16:15 milvus-api-0.0.1-SNAPSHOT-bin.zip

# 2. 解压
unzip milvus-api-0.0.1-SNAPSHOT-bin.zip -d milvus-api/

# 3. 切换路径
[root@node1 opt]# cd milvus-api

# 查看文件列表
[root@node1 milvus-api]# ll
total 72
drwxr-xr-x 2 root root    56 Mar  2 14:41 config
drwxr-xr-x 2 root root  8192 Mar 17 11:17 lib
-rw-r--r-- 1 root root 54706 Mar 17 11:18 milvus-api-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 root root    35 Aug 18  2022 README.md

# 4. 执行  治理没有另外写执行脚本, 可以自己编写一个
[root@node1 milvus-api]# java -jar milvus-api-0.0.1-SNAPSHOT.jar
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/milvus-api/lib/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/milvus-api/lib/log4j-slf4j-impl-2.10.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

后续再次更新服务只需要更新业务代码就好, 无需更新繁多的依赖文件了。

参考文档: https://www.zhangjava.com/springboot%E5%B0%86%E9%A1%B9%E7%9B%AE%E4%B8%8E%E4%BE%9D%E8%B5%96%E5%88%86%E5%BC%80%E6%89%93%E5%8C%85/


文章作者: hnbian
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 hnbian !
评论
 上一篇
DolphinScheduler 部署文档 DolphinScheduler 部署文档
本文档部署了最新的 3.14 版本, 官网上的部署文档有些错误, 本文中进行了修复 1. DolphinScheduler 简介Apache DolphinScheduler 官网地址: https://dolphinscheduler
下一篇 
Async Hbase 时频繁遇到 too many open file 异常 Async Hbase 时频繁遇到 too many open file 异常
在使用 Async Hbase 时频繁遇到 too many open file 异常,程序自动重启后会立即报错,具体报错日志如下: flink版本:1.16.0 asynchbase版本:1.8.2 2023-03-08 16:15:
2023-03-14
  目录