使用maven-assembly-plugin如何將system 依賴(lài)范圍的jar以class 方式打包進(jìn) jar包中
List item 背景描述
服務(wù)A 有本地系統(tǒng)依賴(lài)(scope = system)如果服務(wù)A作為普通服務(wù)使用沒(méi)有任何問(wèn)題,但如果將服務(wù)A 以jar 包方式 提供給 服務(wù)B使用,那么服務(wù)B在編譯的時(shí)候就有可能報(bào)錯(cuò),因?yàn)檎也坏椒?wù)A 依賴(lài)的本地Jar。
解決問(wèn)題思路:將服務(wù)A依賴(lài)的本地Jar 以class 方式直接打包進(jìn)輸出jar包中,這樣服務(wù)B在使用服務(wù)A時(shí),就不會(huì)報(bào)找不到本地依賴(lài)的問(wèn)題了;
那要將本地依賴(lài)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>
<!-- 這個(gè)是本地項(xiàng)目,不知道為什么取消本項(xiàng)目就不能打進(jìn)jar 包中 -->
<include>com.aimlin.local:express</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>2、本地依賴(lài)配置
<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>說(shuō)明:
對(duì)于本地依賴(lài)添加 <scope>system</scope> 配置項(xiàng),指定為本地依賴(lài);另外還需要設(shè)置 <optional>true</optional> 防止依賴(lài)傳遞個(gè)服務(wù)B;
問(wèn)題:
如果在編譯過(guò)程出現(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.
這個(gè)問(wèn)題一般都是 默認(rèn) maven 已經(jīng)生成了jar ,這里使用maven-assembly-plugin 將原來(lái)的jar替換了,就會(huì)提示該異常,解決方案:排除到默認(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>即可取消告警,同時(shí)打包方式應(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 不會(huì)覆蓋原來(lái)的jar,如果需要覆蓋原來(lái)的jar 則需要配置:<appendAssemblyId>false</appendAssemblyId> 則會(huì)只生成一個(gè)jar文件
assembly.xml 文件說(shuō)明
指定生成jar 方式為: <id>jar-with-dependencies</id> 追加依賴(lài)模式;
<includeBaseDirectory>false</includeBaseDirectory>是否將跟目錄打包到根目錄中,因?yàn)槲覀兪菍⑷絡(luò)ar包中的class 輸出到j(luò)ar中,所以不能有根目錄;<outputDirectory>/</outputDirectory>指定輸出目錄為根目錄<unpack>true</unpack>需要解壓縮jar包,添加到j(luò)ar包中依賴(lài)是以class 路徑方式寫(xiě)入的;<scope>system</scope>指定maven scope 范圍,只會(huì)過(guò)濾scope = system級(jí)別的依賴(lài)到j(luò)ar 包;<scope>runtime</scope>運(yùn)行時(shí)jar 依賴(lài)
到此這篇關(guān)于使用maven-assembly-plugin將 system 依賴(lài)范圍的jar以class 方式打包進(jìn) jar包中的文章就介紹到這了,更多相關(guān)maven-assembly-plugin打jar包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Mybatis和JDBC的使用及區(qū)別
這篇文章主要介紹了關(guān)于Mybatis和JDBC的使用及區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-05-05
SpringSecurity數(shù)據(jù)庫(kù)進(jìn)行認(rèn)證和授權(quán)的使用
本文主要介紹了用戶(hù)的賬號(hào)、密碼以及角色信息在數(shù)據(jù)庫(kù)中的認(rèn)證和授權(quán),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
SpringBoot集成cache緩存的實(shí)現(xiàn)
日常開(kāi)發(fā)中,緩存是解決數(shù)據(jù)庫(kù)壓力的一種方案,本文記錄springboot中使用cache緩存。需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
springboot調(diào)用HTML文件注意事項(xiàng)及說(shuō)明
這篇文章主要介紹了springboot調(diào)用HTML文件注意事項(xiàng)及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
java中的equals()和toString()方法實(shí)例詳解
這篇文章主要介紹了java中的equals()和toString()方法實(shí)例詳解的相關(guān)資料,這里舉例說(shuō)明,并附實(shí)例代碼,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
一文盤(pán)點(diǎn)Java創(chuàng)建實(shí)例對(duì)象的方式
Java對(duì)象是通過(guò)加載、鏈接、初始化三大步驟來(lái)完成對(duì)象的創(chuàng)建及初始化,那么接下來(lái)就說(shuō)一下Java創(chuàng)建實(shí)例對(duì)象的方式有哪幾種,文中并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下2025-02-02
SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列方案及使用場(chǎng)景
在很多的業(yè)務(wù)場(chǎng)景中,延時(shí)隊(duì)列可以實(shí)現(xiàn)很多功能,此類(lèi)業(yè)務(wù)中,一般上是非實(shí)時(shí)的,需要延遲處理的,需要進(jìn)行重試補(bǔ)償?shù)?這篇文章主要介紹了SpringBoot基于RabbitMQ實(shí)現(xiàn)消息延遲隊(duì)列方案及使用場(chǎng)景,需要的朋友可以參考下2024-04-04

