SpringBoot打jar包瘦身-依赖文件夹lib和配置文件夹config分离出来
背景
测试环境修复bug
时,经常需要修改代码替换jar
包,由于之前使用SpringBoot
的默认配置,导致依赖lib
文件夹和配置文件夹config
都打进jar
,使得jar
包很大,传输到测试机器上浪费时间,于是琢磨打jar
包时将依赖文件夹lib
和配置文件夹config
分离出来。
原先的pom.xml
的配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
.............
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<!--定义项目的编译环境-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--maven的测试用例插件,建议跳过。-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!--打成jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.iflytek.screen.LlmScreenServiceApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
打成jar
包时,大小约98M,如下所示:
解决方案
现在开始进行改造,pom.xml
修改如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
......
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<!--定义项目的编译环境-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--maven的测试用例插件,建议跳过。-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<!-- 打JAR包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!-- 不打包资源文件(配置文件和依赖包分开) -->
<!-- 根据实际情况 -->
<excludes>
<exclude>*.yml</exclude>
<exclude>*.properties</exclude>
<exclude>logback-spring.xml</exclude>
<exclude>shell/*.sh</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- jar包不包含唯一版本标识 -->
<useUniqueVersions>false</useUniqueVersions>
<!--指定启动类 -->
<mainClass>com.iflytek.screen.LlmScreenServiceApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
<Class-Path>./config/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!-- 该插件的作用是用于复制依赖的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>
</configuration>
</execution>
</executions>
</plugin>
<!-- 该插件的作用是用于复制指定的文件 -->
<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>logback-spring.xml</include>
<include>shell/*.sh</include>
</includes>
</resource>
</resources>
<outputDirectory>${project.build.directory}/config</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
重新打包之后,发现target
文件夹下多了config
和lib
文件夹,jar
包大小也缩小为166K。
这样当修改完bug
重新打包之后,只需要上传166K的jar
即可,减少上传时间。