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í),跳過單元測試 -->
<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 反射獲取類詳細(xì)信息的常用方法總結(jié)
Java 反射獲取類詳細(xì)信息的常用方法總結(jié),需要的朋友可以參考一下2013-03-03
Java實(shí)現(xiàn)基于token認(rèn)證的方法示例
這篇文章主要介紹了Java實(shí)現(xiàn)基于token認(rèn)證的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表功能
這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)雙向鏈表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
如何實(shí)現(xiàn)nohup?java進(jìn)程號一直在變方法步驟詳解
這篇文章主要為大家介紹了如何實(shí)現(xiàn)nohup?java進(jìn)程號一直在變方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11

