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

默認的maven配置
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>如果能將項目外部依賴和自己的代碼包分開打包,當修改項目后,只需要再次覆蓋修改后的包,那豈不是美滋滋?
解決方案
使用maven的assembly打包插件
assembly配置
在項目中創(chuàng)建一個文件,我放在src/main/assembly/assembly.xml中,大家可以根據喜好自己創(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">
<!--
必須寫,否則打包時會有 assembly ID must be present and non-empty 錯誤
這個名字最終會追加到打包的名字的末尾,如項目的名字為 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>
<!-- 壓縮包下是否生成和項目名相同的根目錄 -->
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<!-- 不使用項目的artifact,第三方jar不要解壓,打包進zip文件的lib目錄 -->
<useProjectArtifact>false</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<unpack>false</unpack>
</dependencySet>
</dependencySets>
<fileSets>
<!-- 把項目相關的說明文件,打包進zip文件的根目錄 -->
<fileSet>
<directory>${project.basedir}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>README*</include>
<include>LICENSE*</include>
<include>NOTICE*</include>
</includes>
</fileSet>
<!-- 把項目的配置文件,打包進zip文件的config目錄 -->
<fileSet>
<directory>${project.basedir}/src/main/resources</directory>
<outputDirectory>config</outputDirectory>
</fileSet>
<!-- 把項目的腳本文件,打包進zip文件的bin目錄 -->
<fileSet>
<directory>${project.basedir}/src/main/bin</directory>
<outputDirectory>bin</outputDirectory>
</fileSet>
<!-- 把項目自己編譯出來的jar文件,打包進zip文件的根目錄 -->
<fileSet>
<directory>${project.build.directory}</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet>
</fileSets>
</assembly>maven中的配置
<build>
<plugins>
<!-- 指定啟動類,將依賴打成外部jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<!-- 生成的jar中,不要包含pom.xml和pom.properties這兩個文件 -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<!-- 是否要把第三方jar放到manifest的classpath中 -->
<addClasspath>true</addClasspath>
<!-- 外部依賴jar包的最終位置 -->
<classpathPrefix>lib/</classpathPrefix>
<!-- 項目啟動類 -->
<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ā)布時,跳過單元測試 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>最終打包后的效果

壓縮包里的文件內容

lib中的文件

config配置文件

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何實現(xiàn)nohup?java進程號一直在變方法步驟詳解
這篇文章主要為大家介紹了如何實現(xiàn)nohup?java進程號一直在變方法步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11

