使用maven-assembly-plugin如何將system 依賴范圍的jar以class 方式打包進(jìn) jar包中
List item 背景描述
服務(wù)A 有本地系統(tǒng)依賴(scope = system)如果服務(wù)A作為普通服務(wù)使用沒有任何問題,但如果將服務(wù)A 以jar 包方式 提供給 服務(wù)B使用,那么服務(wù)B在編譯的時候就有可能報錯,因?yàn)檎也坏椒?wù)A 依賴的本地Jar。
解決問題思路:將服務(wù)A依賴的本地Jar 以class 方式直接打包進(jìn)輸出jar包中,這樣服務(wù)B在使用服務(wù)A時,就不會報找不到本地依賴的問題了;
那要將本地依賴jar 打包進(jìn)輸出jar 中需要使用到 maven-assembly-plugin 插件;
1、添加插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
<descriptors>
<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>這里使用 assembly.xml 配置文件方式配置打包詳細(xì)
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 https://maven.apache.org/xsd/assembly-2.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>system</scope>
<includes>
<include>com.lop:CommonQueryOrderApi</include>
</includes>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>com.lop.open:lop-opensdk-support</include>
<!-- 這個是本地項(xiàng)目,不知道為什么取消本項(xiàng)目就不能打進(jìn)jar 包中 -->
<include>com.aimlin.local:express</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>2、本地依賴配置
<dependency>
<groupId>com.lop</groupId>
<artifactId>CommonQueryOrderApi</artifactId>
<version>${CommonQueryOrderApi.version}</version>
<scope>system</scope>
<systemPath>${basedir}/lib/CommonQueryOrderApi.jar</systemPath>
<optional>true</optional>
</dependency>說明:
對于本地依賴添加 <scope>system</scope> 配置項(xiàng),指定為本地依賴;另外還需要設(shè)置 <optional>true</optional> 防止依賴傳遞個服務(wù)B;
問題:
如果在編譯過程出現(xiàn)
[WARNING] Artifact: com.xxx:xxx:jar:1.0.0 references the same file as the assembly
destination file. Moving it to a temporary location for inclusion.
這個問題一般都是 默認(rèn) maven 已經(jīng)生成了jar ,這里使用maven-assembly-plugin 將原來的jar替換了,就會提示該異常,解決方案:排除到默認(rèn)編譯器生成jar 即可
<!-- 排除默認(rèn)jar生成器 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>default-jar</id>
<!-- 這里將 jar 插件 生命周期綁定到打包之后,注意 不能為none,否則將不能install和 deploy jar 文件 -->
<phase>install</phase>
</execution>
</executions>
</plugin>[WARNING] Configuration option 'appendAssemblyId' is set to false.
Instead of attaching the assembly file: /Users/xxx.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: /Users/xxx/target/classes
with assembly file: /Users/xxx.jar
出現(xiàn)這種告警,一般都是appendAssemblyId 設(shè)置為false ,并且 <attach>true</attach> 導(dǎo)致的,只需要設(shè)置 <attach>false</attach>即可取消告警,同時打包方式應(yīng)該指定為:single
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<attach>false</attach>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>默認(rèn) maven-assembly-plugin 生成的jar 不會覆蓋原來的jar,如果需要覆蓋原來的jar 則需要配置:<appendAssemblyId>false</appendAssemblyId> 則會只生成一個jar文件
assembly.xml 文件說明
指定生成jar 方式為: <id>jar-with-dependencies</id> 追加依賴模式;
<includeBaseDirectory>false</includeBaseDirectory>是否將跟目錄打包到根目錄中,因?yàn)槲覀兪菍⑷絡(luò)ar包中的class 輸出到j(luò)ar中,所以不能有根目錄;<outputDirectory>/</outputDirectory>指定輸出目錄為根目錄<unpack>true</unpack>需要解壓縮jar包,添加到j(luò)ar包中依賴是以class 路徑方式寫入的;<scope>system</scope>指定maven scope 范圍,只會過濾scope = system級別的依賴到j(luò)ar 包;<scope>runtime</scope>運(yùn)行時jar 依賴
到此這篇關(guān)于使用maven-assembly-plugin將 system 依賴范圍的jar以class 方式打包進(jìn) jar包中的文章就介紹到這了,更多相關(guān)maven-assembly-plugin打jar包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IDEA中springboot提示java:找不到符號符號:變量log問題
這篇文章主要介紹了IDEA中springboot提示java:找不到符號符號:變量log問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
JAVA初級項(xiàng)目——實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要介紹了JAVA如何實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06
Java中Map和Set練習(xí)項(xiàng)目實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Java中Map和Set練習(xí)項(xiàng)目的相關(guān)資料,首先介紹了如何使用map來統(tǒng)計(jì)字符串?dāng)?shù)組中每個字符串的出現(xiàn)次數(shù),然后討論了如何使用set來找出只出現(xiàn)一次的數(shù)字,最后提出了一個解決壞鍵盤打字問題的思路,需要的朋友可以參考下2024-11-11
在Java中創(chuàng)建對話框的最佳實(shí)戰(zhàn)指南
對話框是用于向用戶顯示信息(如錯誤提示、輸入請求)的圖形組件,通常作為Java GUI應(yīng)用的頂級容器存在,本文給大家分享了在Java中創(chuàng)建對話框的最佳實(shí)戰(zhàn)指南,需要的朋友可以參考下2025-06-06
springMVC 用戶登錄權(quán)限驗(yàn)證實(shí)現(xiàn)過程解析
這篇文章主要介紹了springMVC 用戶登錄權(quán)限驗(yàn)證實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
解決idea services窗口不見的一種特殊情況(小白采坑系列)
這篇文章主要介紹了解決idea services窗口不見的一種特殊情況(小白采坑系列),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
java equals和=,==的區(qū)別詳細(xì)介紹
這篇文章主要介紹了java equals和=,==的區(qū)別,學(xué)習(xí)Java的朋友對equals 和== 這個概念開始使用的時候會有疑問,很難辨別如何正確使用,這里幫大家詳細(xì)講解該知識點(diǎn),希望大家能掌握,有需要的小伙伴可以參考下2016-10-10

