springboot打包實(shí)現(xiàn)項(xiàng)目JAR包和依賴JAR包分離
寫在前面的
當(dāng)我們使用spring boot寫項(xiàng)目時(shí),一般都會(huì)遇到一個(gè)問題,那就是spring boot打包時(shí),會(huì)將自己寫的代碼和項(xiàng)目的所有依賴文件打成一個(gè)可執(zhí)行的jar包。
通常我們的項(xiàng)目都是運(yùn)行在服務(wù)器上的,當(dāng)項(xiàng)目更新時(shí),每次都要向服務(wù)器上傳這個(gè)包。如果項(xiàng)目的依賴包很多,那么這個(gè)文件就會(huì)非常大。
大文件上傳不僅浪費(fèi)帶寬,有時(shí)候網(wǎng)絡(luò)不穩(wěn)定,傳輸一半斷網(wǎng),又要重新上傳,非常麻煩。

默認(rèn)的maven配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>如果能將項(xiàng)目外部依賴和自己的代碼包分開打包,當(dāng)修改項(xiàng)目后,只需要再次覆蓋修改后的包,那豈不是美滋滋?
解決方案
使用maven的assembly打包插件
assembly配置
在項(xiàng)目中創(chuàng)建一個(gè)文件,我放在src/main/assembly/assembly.xml中,大家可以根據(jù)喜好自己創(chuàng)建。

assembly中的具體配置
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<!--
必須寫,否則打包時(shí)會(huì)有 assembly ID must be present and non-empty 錯(cuò)誤
這個(gè)名字最終會(huì)追加到打包的名字的末尾,如項(xiàng)目的名字為 speed-api-0.0.1-SNAPSHOT,
則最終生成的包名為 speed-api-0.0.1-SNAPSHOT-bin.zip
-->
<id>bin</id>
<!-- 打包后的文件格式,可以是zip,tar,tar.gz,tar.bz2,jar,war,dir -->
<formats>
<format>zip</format>
</formats>
<!-- 壓縮包下是否生成和項(xiàng)目名相同的根目錄 -->
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!-- 不使用項(xiàng)目的artifact,第三方j(luò)ar不要解壓,打包進(jìn)zip文件的lib目錄 -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把項(xiàng)目相關(guān)的說明文件,打包進(jìn)zip文件的根目錄 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把項(xiàng)目的配置文件,打包進(jìn)zip文件的config目錄 -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<!-- 把項(xiàng)目的腳本文件,打包進(jìn)zip文件的bin目錄 -->
<fileSet>
<directory>${project.basedir}/src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
<!-- 把項(xiàng)目自己編譯出來的jar文件,打包進(jìn)zip文件的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>maven中的配置
<build>
<plugins>
<!-- 指定啟動(dòng)類,將依賴打成外部jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- 生成的jar中,不要包含pom.xml和pom.properties這兩個(gè)文件 -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<!-- 是否要把第三方j(luò)ar放到manifest的classpath中 -->
<addClasspath>true</addClasspath>
<!-- 外部依賴jar包的最終位置 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 項(xiàng)目啟動(dòng)類 -->
<mainClass>com.zbrx.speed.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- 使用assembly打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<!-- assembly配置文件位置 -->
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 打包發(fā)布時(shí),跳過單元測(cè)試 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>最終打包后的效果

壓縮包里的文件內(nèi)容

lib中的文件

config配置文件

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java web spring異步方法實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Java web spring異步方法實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java SHA-256加密的兩種實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Java SHA-256加密的兩種實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)SHA-256加密的實(shí)現(xiàn)代碼與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-08-08
Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時(shí)文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明
這篇文章主要介紹了方法參數(shù)相關(guān)屬性params、@PathVariabl和@RequestParam用法與區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

