背景
首先項目是個多層級的多模塊springBoot項目,每次打出來的jar包都在90M,其中包含核心代碼以及所有依賴的jar包,上傳到服務器速度比較慢。
由於核心代碼(controller、service、dao、model)會經常改動進行發布上線,而依賴的jar包(pom文件的依賴引用)並不是經常更新,所以希望進行分離打包,改動代碼只需要上線發布核心jar包(幾十KB吧),提高效率。
打包方法
步驟1:清理之前的jar包
步驟2:進行重新打包
不分離打包
1、pom文件配置
<build> <finalName>TC_NLP_Platform</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
2、 箭頭指向的就是完整的可運行的jar包,根據項目的不同大小不同,整體來說還是很大的。
分離打包
1、pom文件配置,由於項目是多模塊的(分層的),所以打包的時候希望TCSP_Management(controller層)、TCSP_DAO、TCSP_Model、TCSP_Service可以合在一起打成一個核心jar包,其他的依賴打包到另外一個地方,達到核心代碼與依賴分離打包的目的。
部分配置解釋
a、<!--依賴jar包的輸出目錄,根據自己喜好配置-->
<outputDirectory>${project.build.directory}/lib</outputDirectory>
將不經常改動的依賴包,打包到lib文件夾下
b、<!--排除以下artifactId的jar包-->
<excludeArtifactIds>TCSP_Model,TCSP_DAO,TCSP_Service</excludeArtifactIds>
目的是排除TCSP_Management(controller層)、TCSP_DAO、TCSP_Model、TCSP_Service,核心的業務包,不打包到lib文件夾下面
<build> <finalName>TC_NLP_Platform</finalName> <plugins> <plugin> <!--打包時去除第三方依賴--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> <layout>ZIP</layout> <!--將下面三個模塊的jar包,打包到核心包中--> <includes> <include> <artifactId>TCSP_Model</artifactId> <groupId>com.toycloud.tcspeech</groupId> </include> <include> <artifactId>TCSP_DAO</artifactId> <groupId>com.toycloud.tcspeech</groupId> </include> <include> <artifactId>TCSP_Service</artifactId> <groupId>com.toycloud.tcspeech</groupId> </include> </includes> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <!--拷貝第三方依賴文件到指定目錄--> <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> <!--依賴jar包的輸出目錄,根據自己喜好配置--> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>false</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> <!--排除以下artifactId的jar包--> <excludeArtifactIds>TCSP_Model,TCSP_DAO,TCSP_Service</excludeArtifactIds> </configuration> </execution> </executions> </plugin> </plugins> </build>
2、 下圖中,1表示的就是所有依賴打包后所在的路徑,2表示核心代碼所在的路徑。
核心代碼解壓后,包含TCSP_DAO、TCSP_Model、TCSP_Service三個核心模塊的jar包
發布到服務器運行:
java -Dloader.path=./lib -Duser.timezone=Asia/Shanghai -jar TC_NLP_Platform.jar
注意:lib文件夾(依賴的jar包)必須和TC_NLP_Platform.jar(核心jar包)在同級目錄