1. 背景 最近向服务器上部署 spring boot项目时,会遇到一个问题,那就是 spring boot 打包时,会将自己写的代码和项目的所有依赖文件打成一个可执行的 jar 包。通常我们的项目都是运行在服务器上的,当项目更新时,每次都要向服务器上传这个包。如果项目的依赖包很多,那么这个文件就会非常大。最近堡垒机网络不稳定, 上传包的时候总是失败, 导致部署效率很低。
1 2 3 4 5 6 7 8 <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文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 <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 > bin</id > <formats > <format > zip</format > </formats > <includeBaseDirectory > false</includeBaseDirectory > <dependencySets > <dependencySet > <useProjectArtifact > false</useProjectArtifact > <outputDirectory > lib</outputDirectory > <unpack > false</unpack > </dependencySet > </dependencySets > <fileSets > <fileSet > <directory > ${project.basedir}</directory > <outputDirectory > </outputDirectory > <includes > <include > README*</include > <include > LICENSE*</include > <include > NOTICE*</include > </includes > </fileSet > <fileSet > <directory > ${project.basedir}/src/main/resources</directory > <outputDirectory > config</outputDirectory > </fileSet > <fileSet > <directory > ${project.basedir}/src/main/bin</directory > <outputDirectory > bin</outputDirectory > </fileSet > <fileSet > <directory > ${project.build.directory}</directory > <outputDirectory > </outputDirectory > <includes > <include > *.jar</include > </includes > </fileSet > </fileSets > </assembly >
2.4 pom.xml 的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-jar-plugin</artifactId > <configuration > <archive > <addMavenDescriptor > false</addMavenDescriptor > <manifest > <addClasspath > true</addClasspath > <classpathPrefix > lib/</classpathPrefix > <mainClass > </mainClass > </manifest > </archive > </configuration > </plugin > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-assembly-plugin</artifactId > <configuration > <descriptors > <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 打包效果
3. 项目部署与运行 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 [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 unzip milvus-api-0.0.1-SNAPSHOT-bin.zip -d milvus-api/ [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 [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/