springboot-静态资源及第三方依赖包分离打包
环境准备:
jdk 1.8
maven 3.6
springboot 2.2.2.RELEASE
为什么要分离打包
我们在使用maven
为springboot
项目打包时会发现打完的jar包会很大,而实际上里面绝大部分的大文件为静态资源和第三方依赖的jar包,而动辄百兆的jar包每次发布时候就很头痛了,上传服务器时由于带宽的限制每次都非常慢,其次如果只需要改静态文件,但还得重新打包发布并重启,很是麻烦,所以分离打包主要是为了解决2个痛点:
- 发布时上传时间太久
- 修改静态文件不用重启服务
Talk is cheap. Show me the code!
pom.xml
<build>
<finalName>${project.artifactId}-${profiles.active}</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profiles.active}.yml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>templates/**</include>
<include>static/**</include>
<include>mapper/**</include>
<include>freemark/**</include>
<include>spy.properties</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 分离打包开始 -->
<!-- 跳过测试代码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- maven打包成jar的插件 -->
<!-- 打包jar文件时,配置manifest文件,加入lib包的jar依赖 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 把target/classes/下的文件导入到jar内 -->
<classesDirectory>target/classes/</classesDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<archive>
<manifest>
<!-- 指定程序入口 -->
<mainClass>com.jingci.erp.ErpApplication</mainClass>
<!-- 打包时 MANIFEST.MF文件不记录的时间戳版本 -->
<useUniqueVersions>false</useUniqueVersions>
<addClasspath>true</addClasspath>
<!-- 服务依赖的jar包放在lib目录下 -->
<classpathPrefix>lib/</classpathPrefix>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<!-- jar包中不包括以下文件 -->
<excludes>
<exclude>mapper/**</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
<exclude>**/*.yml</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.properties</exclude>
</excludes>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!-- 分离打包关键代码 maven-dependency-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<type>jar</type>
<includeTypes>jar</includeTypes>
<!-- 存放服务依赖的jar包,存放在服务相同目录的lib文件夹下 -->
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 分离打包关键代码 maven-resources-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 指定resources文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<!-- 分环境过滤开启 -->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<!-- 分环境配置文件不用可删除 -->
<!-- <include>application-${profiles.active}.yml</include> -->
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<!-- 分环境过滤关闭 -->
<filtering>false</filtering>
<includes>
<include>templates/**</include>
<include>static/**</include>
<include>mapper/**</include>
<include>freemark/**</include>
<include>spy.properties</include>
</includes>
</resource>
</resources>
<!-- 输出路径 -->
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- 分离打包结束 -->
</plugins>
</build>
打包
然后执行
mvn clean package
等打包结束就可以看到项目根目录下的target
文件夹的结构如下
启动测试
随后就可以将lib
、resources
、xxx.jar
这三个文件夹发布到服务器上,定位到指定目录后执行
java -jar xxx.jar
便可启动项目
后续维护
至此分离打包结束、以后项目发布过程中,在第三方jar包或静态资源没有变化的情况下可以只发布项目源代码jar包,以节省发布时间提高效率