SpringBoot - 使用assembly进行项目打包教程2(将项目代码与依赖分开打包)
我在前文介绍了如何使用 assembly 对 Spring Boot 项目进行打包(点击查看),打包后会发现项目的代码和项目所有的依赖文件会一起打成一个可执行的 jar 包。

如果项目的依赖包很多,那么这个文件就会非常大。每次发布版本如果都要上传这个整合的 jar 包,既浪费带宽也浪费时间。下面演示如何将项目的外部依赖跟自己的代码包分开打包,这样当项目修改后,只需要上传覆盖修改后的包即可。
(2)接着编辑项目的 pom.xml 文件,先前使用的是 spring-boot-maven-plugin 来打包,这个插件会将项目所有的依赖打入项目 jar 包里面。我们将其替换为 maven-jar-plugin,并进行相关设置。

二、将项目与依赖分开打包
1,修改配置
(1)首先我们编辑 assembly.xml 配置文件,在前文的基础上新增第三方依赖设置(高亮部分),实现将第三方的 jar 包添加到压缩包里的 lib 目录:
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | < assembly > <!-- 必须写,否则打包时会有 assembly ID must be present and non-empty 错误 这个名字最终会追加到打包的名字的末尾,如项目的名字为 hangge-test-0.0.1-SNAPSHOT, 则最终生成的包名为 hangge-test-0.0.1-SNAPSHOT-bin.tar.gz --> < id >bin</ id > <!-- 打包的类型,如果有N个,将会打N个类型的包 --> < formats > < format >tar.gz</ format > <!--<format>zip</format>--> </ formats > < includeBaseDirectory >true</ includeBaseDirectory > <!--第三方依赖设置--> < dependencySets > < dependencySet > <!-- 不使用项目的artifact,第三方jar不要解压,打包进zip文件的lib目录 --> < useProjectArtifact >false</ useProjectArtifact > < outputDirectory >lib</ outputDirectory > < unpack >false</ unpack > </ dependencySet > </ dependencySets > <!--文件设置--> < fileSets > <!-- 0755->即用户具有读/写/执行权限,组用户和其它用户具有读写权限; 0644->即用户具有读写权限,组用户和其它用户具有只读权限; --> <!-- 将src/main/assembly/bin目录下的所有文件输出到打包后的bin目录中 --> < fileSet > < directory >src/main/assembly/bin</ directory > < outputDirectory >bin</ outputDirectory > < fileMode >0755</ fileMode > <!--如果是脚本,一定要改为unix.如果是在windows上面编码,会出现dos编写问题--> < lineEnding >unix</ lineEnding > < filtered >true</ filtered > <!-- 是否进行属性替换 --> </ fileSet > <!-- 将src/main/assembly/config目录下的所有文件输出到打包后的config目录中 --> < fileSet > < directory >src/main/assembly/config</ directory > < outputDirectory >config</ outputDirectory > < fileMode >0644</ fileMode > </ fileSet > <!-- 将src/main/resources下配置文件打包到config目录 --> < fileSet > < directory >src/main/resources</ directory > < outputDirectory >/config</ outputDirectory > < includes > < include >**/*.xml</ include > < include >**/*.properties</ include > < include >**/*.yml</ include > </ includes > < filtered >true</ filtered > <!-- 是否进行属性替换 --> </ fileSet > <!-- 将项目启动jar打包到lib目录中 --> < fileSet > < directory >target</ directory > < outputDirectory >lib</ outputDirectory > < includes > < include >*.jar</ include > </ includes > </ fileSet > <!-- 将项目说明文档打包到docs目录中 --> < fileSet > < directory >.</ directory > < outputDirectory >docs</ outputDirectory > < includes > < include >*.md</ include > </ includes > < fileMode >0644</ fileMode > </ fileSet > < fileSet > < directory >docs</ directory > < outputDirectory >docs</ outputDirectory > < fileMode >0644</ fileMode > </ fileSet > < fileSet > < directory >src/main/assembly/docs</ directory > < outputDirectory >docs</ outputDirectory > < fileMode >0644</ fileMode > </ fileSet > </ fileSets > </ assembly > |
(2)接着编辑项目的 pom.xml 文件,先前使用的是 spring-boot-maven-plugin 来打包,这个插件会将项目所有的依赖打入项目 jar 包里面。我们将其替换为 maven-jar-plugin,并进行相关设置。
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 | < 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加入到类构建路径 --> < addClasspath >true</ addClasspath > <!-- 外部依赖jar包的最终位置 --> <!-- 因为我们将第三方jar和本项目jar放在同一个目录下,这里就使用./ --> < classpathPrefix >./</ classpathPrefix > <!-- 项目启动类 --> < mainClass >com.example.hanggetest.HanggeTestApplication</ mainClass > </ manifest > </ archive > </ configuration > </ plugin > < plugin > <!--主要使用的是maven提供的assembly插件完成--> < artifactId >maven-assembly-plugin</ artifactId > < version >3.1.1</ version > < configuration > < descriptors > <!--具体的配置文件--> < descriptor >src/main/assembly/assembly.xml</ descriptor > </ descriptors > </ configuration > < executions > < execution > < id >make-assembly</ id > <!--绑定到maven操作类型上--> < phase >package</ phase > <!--运行一次--> < goals > < goal >single</ goal > </ goals > </ execution > </ executions > </ plugin > </ plugins > </ build > |
2,打包测试
上面两个配置修改完毕后,我们重新对项目进行打包。将生成的压缩包解压后可以发现,lib 文件夹下项目 jar 包以及第三方 jar 都分开了,并且项目 jar 体积也十分小巧。
