問題說明
springboot構(gòu)建jar部署渊啰,通過使用 java -jar xxx.jar 命令啟動服務(wù),非常方便申屹,但是通過maven構(gòu)建的jar包含 \BOOT-INF\lib\下的所有依賴jar包绘证,導(dǎo)致jar包文件太大。
構(gòu)建思路
在maven構(gòu)建springboot項目jar時候哗讥,將lib文件夾分離出來嚷那。
在運行jar的時候,能夠應(yīng)用到分離的lib包
解決步驟分一共三步杆煞,只需要修改pom文件即可
1.修改前pom文件,注意要點 <mainClass>com.xxx.xxx.xxxApplication</mainClass>記得指定為自己項目入口函數(shù)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.xxx.xxx.xxxApplication</mainClass>
<layout>ZIP</layout>
<!--<includes>-->
<!--<include>-->
<!--<groupId>nothing</groupId>-->
<!--<artifactId>nothing</artifactId>-->
<!--</include>-->
<!--</includes>-->
</configuration>
</plugin>
執(zhí)行打包命令mvn clean install
生成瘦身前jar包魏宽,可以看到有145M
瘦身前jar包.png
進入到j(luò)ar包所在目錄解壓jar包,命令:jar -xvf xxx.jar
决乎,拷貝lib文件到目標目錄下
解壓后jar包目錄
2.修改后pom文件,注意要點 <mainClass>com.xxx.xxx.xxxApplication</mainClass>記得指定為自己項目入口函數(shù)
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.xxx.xxx.xxxApplication</mainClass>
<layout>ZIP</layout>
<includes>
<include>
<groupId>nothing</groupId>
<artifactId>nothing</artifactId>
</include>
</includes>
</configuration>
</plugin>
執(zhí)行打包命令mvn clean install
生成瘦身后jar包队询,可以看到只有9.2M
瘦身后jar包
3.把瘦身后jar包和lib文件放到相同的目錄下
執(zhí)行以下命令
相對路徑:java -Dloader.path=\lib -jar xxx.jar
絕對路徑:java -Dloader.path=/Users/apple/Desktop/testpdf/tes/lib -jar xxx.jar
其中springboot對于配置文件讀取有如下方式:
1、讀取jar包同級目錄下的config目錄中的properties文件构诚,優(yōu)先級最高蚌斩;
2、讀取jar包同級目錄下的properties文件范嘱,優(yōu)先級次之送膳;
3、讀取classpath下的config文件夾中的properties文件彤侍,優(yōu)先級第三肠缨;
4、在classpath下直接放配置文件盏阶,優(yōu)先級最低晒奕。