springboot打包JAR包瘦身lib和配置文件分离方式
作者:H90P0820
本文介绍了如何通过优化POM.xml配置来减小JAR包大小,提高传输速度,主要步骤包括:指定打包环境和跳过编译单元测试、JAR打包排除配置文件和lib、提供全量包便于开发环境使用、将lib和配置文件单独复制出来
springboot打包JAR包瘦身lib和配置文件分离
最近项目在部署使用的过程中,传输JAR包的时候发现JAR包有点大,传输速度有点慢。
笔者基于这个前提对项目的打包配置进行优化进行探索,一般来说lib是很少修改,配置文件会根据需求会有所变动,如果讲配置文件打进JAR生产环境会修改等不便等操作。
以下配置再pom.xml中 <build> <plugins>添加相关配置</plugins></build>
1.指定打包环境和跳过编译单元测试
<!--定义项目的编译环境--> <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> <!--默认执行src/test/java路径下的测试用例,建议跳过执行--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin>
2.JAR打包排除配置文件和lib
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <!-- 不打包资源文件,如果放开全量JAR也不会打进去--> <!-- <excludes>--> <!-- <exclude>*.yml</exclude>--> <!-- <exclude>*.properties</exclude>--> <!-- </excludes>--> <archive> <manifest> <addClasspath>true</addClasspath> <!-- MANIFEST.MF 中 Class-Path 加入前缀 --> <classpathPrefix>lib/</classpathPrefix> <!-- jar包不包含唯一版本标识 --> <useUniqueVersions>false</useUniqueVersions> <!--指定启动入口类 --> <mainClass>com.xxxx.Application</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 --> <Class-Path>./resources/</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin>
3.如果需要全量包也可以增加如下配置
便于开发环境打包使用
<!--全量JAR包,最初的打包方式,springboot的默认编译插件,默认会把所有的文件打包成一个jar,--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> <configuration> <!--设置springboot的启动类或者是JAR包的启动类--> <mainClass>com.xxxx.Application</mainClass> <fork>true</fork> <addResources>true</addResources> <outputDirectory>${project.build.directory}/jar</outputDirectory> </configuration> </plugin>
4.将lib和配置文件单独复制出来
<!--复制关联JAR包到指定lib打包目录 ,target目录下的文件夹下,按需修改 --> <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> <!-- 复制配置文件到指定resources打包目录 ,target目录下的文件夹下,按需修改--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <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> </includes> </resource> </resources> <outputDirectory>${project.build.directory}/resources</outputDirectory> </configuration> </execution> </executions> </plugin>
5.全量配置如下,拷贝即用
注意启动类的指定:
以下配置再pom.xml中 <build> 添加相关配置</build>
<!--拆分配置文件和LIB,给JAR瘦身--> <!--启动参考命令,再JAR目录执行,也可以指定绝对路径: ${project.build.directory}工程路径下的target目录 Dloader.path:加载本地lib Dspring.config.location:加载本地配置 server.port:指定端口 /-/- 这个有转义,注命令要去掉/ java -Dloader.path=lib/ -jar xxxx.jar /-/-Dspring.config.location=resources/ /-/-server.port=8080 --> <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> <!--默认执行src/test/java路径下的测试用例,建议跳过执行--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> <!--全量JAR包,最初的打包方式,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.xxxx</mainClass> <fork>true</fork> <addResources>true</addResources> <outputDirectory>${project.build.directory}/jar</outputDirectory> </configuration> </plugin> <!-- 打JAR包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <!-- 不打包资源文件,如果放开全量JAR也不会打进去--> <!-- <excludes>--> <!-- <exclude>*.yml</exclude>--> <!-- <exclude>*.properties</exclude>--> <!-- </excludes>--> <archive> <manifest> <addClasspath>true</addClasspath> <!-- MANIFEST.MF 中 Class-Path 加入前缀 --> <classpathPrefix>lib/</classpathPrefix> <!-- jar包不包含唯一版本标识 --> <useUniqueVersions>false</useUniqueVersions> <!--指定springboot启动入口类 --> <mainClass>com.xxxx</mainClass> </manifest> <manifestEntries> <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 --> <Class-Path>./resources/</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin> <!--复制关联JAR包到指定lib打包目录 ,target目录下的文件夹下,按需修改 --> <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> <!-- 复制配置文件到指定resources打包目录 ,target目录下的文件夹下,按需修改--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <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> </includes> </resource> </resources> <outputDirectory>${project.build.directory}/resources</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins>
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。