springboot增量打包更新--静态资源分离打包

    技术2022-07-15  82

    springboot部署打包为jar,一般都是全量打包,jar包的大小通常都是超过100M的,并且在进行一般的页面html微调、js修改、img替换、css样式修改时也需要重新打包进行部署;每次微小的调整都需要重新打包就太烦了,一般在项目开发稳定以后项目中引用的jar就不再改变

    为了方便进行静态资源管理及增量部署,对项目引用jar包以及静态资源分离打包,提高打包的效率及部分前端微调项修改后及时进行无重启更新;

    具体步骤如下:

    1、初次打包进行全量打包,对打包的jar进行解压,解压后的文件如下图展示: 2、分离引用的jar包:进入BOOT-INF中,copy文件夹lib到指定目录下,如jar包运行目录;如图3, 3、分离静态文件:在lib同目录下创建resource文件夹,进入classes文件夹内copy文件夹static及templates文件夹到resource文件下;如图: 4、删除jar包及解压文件,当前目录结构如下: 5、增量打包,打包不再将引用jar及static文件夹、templates文件夹打到jar包中:首先修改pom.xml文件中打包相关配置,如下图: 打包配置增加了如下代码:

    <layout>ZIP</layout> <includes> <include> <groupId>non-exists</groupId> <artifactId>non-exists</artifactId> </include> </includes>

    resource打包配置增加如下过滤:

    <exclude>static/**/*</exclude> <exclude>templates/**/*</exclude>

    最终pom.xml中打包配置如下:

    <build> <finalName>web</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> <mainClass>com.***.Application</mainClass> <!--增量打包配置【start】--> <layout>ZIP</layout> <includes> <include> <groupId>non-exists</groupId> <artifactId>non-exists</artifactId> </include> </includes> <!--增量打包配置【end】--> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.4.RELEASE</version> <configuration> <fork>false</fork> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> <excludes> <!--【增加静态文件过滤】--> <exclude>static/**/*</exclude> <exclude>templates/**/*</exclude> </excludes> </resource> </resources> </build>

    6、执行maven clean install,获得最终jar包,如下图所示,只有5M大小; 7、最终运行时,jar的执行命令中增加lib及resource的路径指向,否则项目无法正常运行;

    java -Dloader.path=./lib,./resource -jar ./web.jar

    8、如上进行增量打包后,如果前端有不涉及到后端的修改时都可以对resource中的文件进行替换进行实时更新,不再进行重启;后端有变动也不用上传100多M的jar到服务器,影响效率;

    Processed: 0.010, SQL: 9