springboot打包實現(xiàn)項目JAR包和依賴JAR包分離
寫在前面的
當我們使用spring boot寫項目時,一般都會遇到一個問題,那就是spring boot打包時,會將自己寫的代碼和項目的所有依賴文件打成一個可執(zhí)行的jar包。
通常我們的項目都是運行在服務(wù)器上的,當項目更新時,每次都要向服務(wù)器上傳這個包。如果項目的依賴包很多,那么這個文件就會非常大。
大文件上傳不僅浪費帶寬,有時候網(wǎng)絡(luò)不穩(wěn)定,傳輸一半斷網(wǎng),又要重新上傳,非常麻煩。
默認的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中,大家可以根據(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"> <!-- 必須寫,否則打包時會有 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,第三方j(luò)ar不要解壓,打包進zip文件的lib目錄 --> <useProjectArtifact>false</useProjectArtifact> <outputDirectory>lib</outputDirectory> <unpack>false</unpack> </dependencySet> </dependencySets> <fileSets> <!-- 把項目相關(guān)的說明文件,打包進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> <!-- 是否要把第三方j(luò)ar放到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>
最終打包后的效果
壓縮包里的文件內(nèi)容
lib中的文件
config配置文件
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java web spring異步方法實現(xiàn)步驟解析
這篇文章主要介紹了Java web spring異步方法實現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件
這篇文章主要介紹了Java如何獲取resources下的文件路徑和創(chuàng)建臨時文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12基于params、@PathVariabl和@RequestParam的用法與區(qū)別說明
這篇文章主要介紹了方法參數(shù)相關(guān)屬性params、@PathVariabl和@RequestParam用法與區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08