使用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在編譯的時候就有可能報錯,因為找不到服務(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> <!-- 這個是本地項目,不知道為什么取消本項目就不能打進(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>
配置項,指定為本地依賴;另外還需要設(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>
是否將跟目錄打包到根目錄中,因為我們是將三方j(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>
運行時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)文章
關(guān)于Mybatis和JDBC的使用及區(qū)別
這篇文章主要介紹了關(guān)于Mybatis和JDBC的使用及區(qū)別,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-05-05SpringSecurity數(shù)據(jù)庫進(jìn)行認(rèn)證和授權(quán)的使用
本文主要介紹了用戶的賬號、密碼以及角色信息在數(shù)據(jù)庫中的認(rèn)證和授權(quán),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08springboot調(diào)用HTML文件注意事項及說明
這篇文章主要介紹了springboot調(diào)用HTML文件注意事項及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11java中的equals()和toString()方法實例詳解
這篇文章主要介紹了java中的equals()和toString()方法實例詳解的相關(guān)資料,這里舉例說明,并附實例代碼,和實現(xiàn)效果圖,需要的朋友可以參考下2016-11-11SpringBoot基于RabbitMQ實現(xiàn)消息延遲隊列方案及使用場景
在很多的業(yè)務(wù)場景中,延時隊列可以實現(xiàn)很多功能,此類業(yè)務(wù)中,一般上是非實時的,需要延遲處理的,需要進(jìn)行重試補償?shù)?這篇文章主要介紹了SpringBoot基于RabbitMQ實現(xiàn)消息延遲隊列方案及使用場景,需要的朋友可以參考下2024-04-04