SpringBoot 增量部署发布(第2版)
一、背景介绍
书接上一篇《SpringBoot 增量部署发布_springboot增量部署-CSDN博客》,上一篇内容实现了将静态资源与jar分离,但是即使是打包成**-exec.jar,解压jar文件,可以看到里面包含了static,resource目录,整个jar文件还是偏大,有没有办法彻底分离静态资源和jar呢,这篇文章就是解决这个问题的。
二、解决办法
还是修改项目pom.xml文件的build节点。
1.打包完整包
这跟springboot默认的打包方式一致,将整个程序打包成一个jar,可以根据实际情况看是否使用,有,可以不用。
<!--0.完整包:这个是springboot的默认编译插件,他默认会把所有的文件打包成一个jar-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!--程序入口类 -->
<mainClass>com.rc114.web.BlogApplication</mainClass>
<addResources>true</addResources>
<outputDirectory>${project.build.directory}/jar-one-package</outputDirectory>
</configuration>
</plugin>
2.打包jar,生成项目对应的jar
此步是将web项目打包成jar,但是不包含static,templates目录。
<!--分离包:步骤1.打包jar,生成项目对应的jar,如:rc_web_rb-0.0.1.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包资源文件(配置文件和依赖包分开) -->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>mybatis/**</exclude>
<exclude>mapper/**</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- jar包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
<!--程序入口类 -->
<mainClass>com.rc114.web.BlogApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
注意:
1.mainClass 需要根据你实际情况进行修改。
2.excludes 排除了不必要的文件夹,所以打包出来的jar会比较小。
3.打包lib,将引用jar放到lib文件夹
<!--分离包:步骤2:打包lib:该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
<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>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
4.打包resource,将resources目录复制到target目录
<!--分离包:步骤3.复制文件:该插件的作用是用于复制指定的文件(将resources目录复制到target目录) -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<!-- 复制配置文件 -->
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>*.properties</include>
<include>mybatis/**</include>
<include>logback/**</include>
<include>mapper/**</include>
<include>static/**</include>
<include>templates/**</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
5.清理非必要目录
在target目录下,还会生成classes,generated-sources,maven-archiver,maven-status等目录,这些目录在真正部署时,往往是不需要的,可以在打包完成后排除掉。通过maven-antrun-plugin插件即可排除指定的目录。
<!--打包完成后,移除非必要目录(根据实际情况进行配置)-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete dir="${project.build.directory}/antrun" />
<delete dir="${project.build.directory}/classes" />
<delete dir="${project.build.directory}/generated-sources" />
<delete dir="${project.build.directory}/maven-archiver" />
<delete dir="${project.build.directory}/maven-status" />
</target>
</configuration>
</execution>
</executions>
</plugin>
三、完整的配置
<build>
<plugins>
<!--0.完整包:这个是springboot的默认编译插件,他默认会把所有的文件打包成一个jar-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<!--程序入口类 -->
<mainClass>com.rc114.web.BlogApplication</mainClass>
<addResources>true</addResources>
<outputDirectory>${project.build.directory}/jar-one-package</outputDirectory>
</configuration>
</plugin>
<!--分离包:步骤1.打包jar,生成项目对应的jar,如:rc_web_rb-0.0.1.jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包资源文件(配置文件和依赖包分开) -->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>mybatis/**</exclude>
<exclude>mapper/**</exclude>
<exclude>static/**</exclude>
<exclude>templates/**</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- jar包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
<!--程序入口类 -->
<mainClass>com.rc114.web.BlogApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!--分离包:步骤2:打包lib:该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
<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>
<outputDirectory>${project.build.directory}/lib/</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!--分离包:步骤3.复制文件:该插件的作用是用于复制指定的文件(将resources目录复制到target目录) -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<!-- 复制配置文件 -->
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.yml</include>
<include>*.properties</include>
<include>mybatis/**</include>
<include>logback/**</include>
<include>mapper/**</include>
<include>static/**</include>
<include>templates/**</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
最终打包输出的target目录结构:
lib和***.jar 供后端更换,发布新程序。
resource 供前端更换,发布新程序。
这样就可以增量更新了,每次只用传很小的文件上去就可以了。
启动程序跟原来一样,直接把文件放在同级目录,然后运行 java -jar rc_web_blog-0.0.1.jar 即可。
四、参考文档
SpringBoot打包实现静态文件、配置文件、jar包分离
https://www.cnblogs.com/pxblog/p/14645043.html