SpringBoot依赖外置

背景

近期有个SpringBoot的项目需要频繁更新,但是每次上传到服务器上几十MB,实在是花时间,所以打算优化打包方案,将第三方依赖外置

流程

  • 首先使用SpringBoot打包插件将第三方排除,但是一些版本号同步更新的本地模块依赖需要放到一个jar中
  • 使用maven dependency插件将第三方依赖复制到构建目录中
  • 使用maven过滤功能实现一个启动脚本
  • 使用assembly打包一个完整版,包括boot jar,第三方依赖,启动脚本
  • 第一次部署使用完整版,后续更新只需要上传boot jar就行了

实现方法

首先在 resource 目录中准备一个脚本

内容如下

1java -Dloader.path=lib/ -Dfile.encoding=utf-8 -jar @project.build.finalName@.jar 2

其中 @project.build.finalName@ 是最后生成的可执行jar的文件名 path指定第三方依赖目录


其次修改pom文件如下

1 <build> 2 <resources> 3 <resource> 4 <!-- 启用maven过滤,主要为脚本做准备 --> 5 <directory>src/main/resources</directory> 6 <filtering>true</filtering> 7 </resource> 8 </resources> 9 10 <finalName>${project.artifactId}-${project.version}-${spring.active}</finalName> 11 12 <plugins> 13 <plugin> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-maven-plugin</artifactId> 16 <configuration> 17 <mainClass>xxx.Application</mainClass> 18 19 <layout>ZIP</layout> 20 <executable>true</executable> 21 <includes> 22 <!-- 以下为需要打包到jar中的本地模块依赖,主要是版本号需要更新,如果放到第三方依赖中,可能会出现多个版本 --> 23 <include> 24 <groupId>xxx.xxx</groupId> 25 <artifactId>upload-starter</artifactId> 26 </include> 27 <include> 28 <groupId>xxx.xxx</groupId> 29 <artifactId>pay</artifactId> 30 </include> 31 <include> 32 <groupId>${project.parent.groupId}</groupId> 33 <artifactId>swagger-starter</artifactId> 34 </include> 35 </includes> 36 37 </configuration> 38 </plugin> 39 40 <plugin> 41 <groupId>org.apache.maven.plugins</groupId> 42 <artifactId>maven-dependency-plugin</artifactId> 43 <executions> 44 <execution> 45 <id>copy-dependencies</id> 46 <phase>package</phase> 47 <goals> 48 <goal>copy-dependencies</goal> 49 </goals> 50 <configuration> 51 <!-- 输出的第三方依赖位置 --> <outputDirectory>${project.build.directory}/lib</outputDirectory> 52 <overWriteReleases>false</overWriteReleases> 53 <!-- 此处排除需要打到boot jar中的本地模块依赖 --> 54 <excludeGroupIds> 55 ${project.parent.groupId},xxx.xxx 56 </excludeGroupIds> 57 </configuration> 58 </execution> 59 </executions> 60 </plugin> 61 62 <plugin> 63 <groupId>org.apache.maven.plugins</groupId> 64 <artifactId>maven-assembly-plugin</artifactId> 65 <configuration> 66 <descriptors> 67 <descriptor>assembly.xml</descriptor> 68 </descriptors> 69 </configuration> 70 <executions> 71 <execution><!-- 配置执行器 --> 72 <id>make-assembly</id> 73 <phase>package</phase><!-- 绑定到package生命周期阶段上 --> 74 <goals> 75 <goal>single</goal><!-- 只运行一次 --> 76 </goals> 77 78 <!-- <configuration>--> 79 <!-- <finalName>${project.name}</finalName>--> 80 81 <!-- <descriptor>src/main/assembly.xml</descriptor>&lt;!&ndash;配置描述文件路径&ndash;&gt;--> 82 <!-- </configuration>--> 83 </execution> 84 </executions> 85 </plugin> 86 </plugins> 87 </build>

pom.xml 同级目录创建 assembly.xml

内容如下

1<assembly> 2 <id>release</id> 3 <formats> 4 <format>zip</format><!--打包的文件格式,也可以有:war zip--> 5 </formats> 6 <!--tar.gz压缩包下是否生成和项目名相同的根目录--> 7 <includeBaseDirectory>false</includeBaseDirectory> 8 9 <!-- 如果使用这个,也可以不使用maven-dependency插件 --> 10<!-- <dependencySets>--> 11<!-- <dependencySet>--> 12<!-- &lt;!&ndash;是否把本项目添加到依赖文件夹下&ndash;&gt;--> 13<!-- <useProjectArtifact>true</useProjectArtifact>--> 14<!-- <outputDirectory>lib</outputDirectory>--> 15<!-- &lt;!&ndash;将scope为runtime的依赖包打包&ndash;&gt;--> 16<!-- <scope>runtime</scope>--> 17<!-- </dependencySet>--> 18<!-- </dependencySets>--> 19 <fileSets> 20 <fileSet> 21 <directory>${project.build.directory}/lib</directory> 22 <outputDirectory>/lib</outputDirectory> 23 </fileSet> 24 </fileSets> 25 <files> 26 <file> 27 <source>target/${build.finalName}.jar</source> 28 <outputDirectory>/</outputDirectory> 29 </file> 30 <file> 31 <!-- 此处将脚本复制两份,分别对应类unix和windows系统,注意,maven过滤之后的文件在target目录下 --> <source>${project.build.directory}/classes/start.sh</source> 32 <outputDirectory>/</outputDirectory> 33 <destName>start.bat</destName> 34 </file> 35 <file> 36 <source>${project.build.directory}/classes/start.sh</source> 37 <outputDirectory>/</outputDirectory> 38 <destName>start.sh</destName> 39 </file> 40 </files> 41</assembly> 42 43

最终效果

TIM图片20191205091306

可以很明显的看出两个方式打包的大小差异


完整版的目录结构 04ACB025-7C19-4a98-9351-2E6AD9007E23

点赞
收藏

评论区

加载中...

相关推荐

springboot使用之快捷打包部署

本篇关键知识点:Maven的assembly插件实现自定义打包部署(包含依赖jar包) 目前springboot项目的几种常见的部署方式。1\.使用docker容器去部署,将springboot的应用构建成一个dockerimage,然后通过容器去启动镜像,这种方式在

springboot热部署实战

每次代码改动后都需要重新手动Run项目,心累,在网上找了下,发现SpringBoot提供了热部署的方案,改动代码后自动编译打包,现在将热部署的配置方法记下来:第一步:在pom.xml中添加依赖,导入springbootdevtools<dependency<groupIdorg.springframework

Spring Boot:jar中没有主清单属性

使用SpringBoot微服务搭建框架时,使用IDEA可以正常运行,但是使用MAVEN打包工具打包成jar后运行时,提示错误:未找到主清单目录。查看pom文件,发现已添加SpringBoot的构建插件xmlorg.springframework.bootspringbootmavenplugin2.4.1

Maven项目使用打包时使用本地jar包库

在使用maven管理项目时,有时候我们可能会使用一些第三方的jar包依赖库,但是这些jar包依赖库又没有在共有的maven仓库。通常只能下来放到本项目的lib目录下。但是我们打包时如果不做处理,那么打包后的fatjar中不会有lib文件夹中的相关jar包。打包后无法运行起来,因此需要做特殊处理,让maven打包时能够把使用到外部jar打进去。主要就是在

SpringBoot打包部署简单说明

SpringBoot项目打包部署1\.jar包部署添加一个插件<!打包插件<build<plugins<plugin<groupId

Spring Boot项目使用maven

springbootassembly1.在springboot项目中使用mavenprofiles和mavenassembly插件根据不同环境打包成tar.gz或者zip2.将springboot项目中的配置文件提取到外部config目录中3.将springboot项目中