解決項目版本沖突之maven-shade插件使用方式
背景
當(dāng)我們在maven項目中引入第三方組件時,三方組件中的依賴可能會與項目已有組件發(fā)生沖突。
比如三方組件中依賴httpclient的版本是4.5.x,而項目中已有的httpclient版本是3.1.x,那么此時就會產(chǎn)生一下兩種情況:
- 如果用三方組件的高版本httpclient覆蓋原有的低版本httpclient,有可能會導(dǎo)致原來項目啟動運行失敗。即使高版本兼容低版本,也不能允許開發(fā)人員有這樣高風(fēng)險的操作
- 如果在三方maven依賴中對其對依賴的httpclient在引入時使用進(jìn)行排除,使三方組件使用項目中的低版本httpclient,此時可能會因為版本不一致導(dǎo)致三方組件無法使用
在這樣的情況下我們應(yīng)當(dāng)如何保證不影響項目原有依賴版本的情況下正常使用三方組件呢?此時可以考慮使用maven-shade-plugin插件
maven-shade-plugin介紹
maven-shade-plugin在maven官方網(wǎng)站中提供的一個插件,官方文檔中定義其功能如下:
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
簡單來說就是將依賴的包在package階段一起打入jar包中,以及對依賴的jar包進(jìn)行重命名從而達(dá)到隔離的作用。這里為了解決上面的問題我們主要使用第二個功能特性,使得相同依賴不同版本達(dá)到共存的目的。
解決問題
1.環(huán)境準(zhǔn)備
這里用fastjson來模擬使用maven-shade-plugin解決項目中不同版本共存問題。原項目此時使用的是1.1.15版本的fastjson
<!-- 原項目 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.15</version>
</dependency>假引入一個三方依賴,該依賴使用1.2.75版本的fastjson
<!-- 將引入依賴 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>2.解決方案
搭建一個新的模塊rename-dependencies,專門用于存放1.2.75依賴。在pom文件中添加1.2.75的依賴,然后添加maven-shade-plugin插件。rename-dependencies的pom如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.sk</groupId>
<artifactId>rename-dependencies</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.alibaba</pattern>
<shadedPattern>shade.com.alibaba</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>從配置文件中可以看到,由于maven-shade-plugin插件在解決這個問題上其實是通過對依賴進(jìn)行重命名而達(dá)到隔離的目的,所以配置主要是集中在relocations中。這里將以com.alibaba開頭的包全部重命名為以shade.com.alibaba開頭。
3.引入依賴
將rename-dependencies進(jìn)行打包,打包好之后在原項目中引入rename-dependencies的依賴。此時在引入rename-dependencies之后,可以在項目下看到該依賴中的fastjson包名發(fā)生了變化。此時在代碼中調(diào)用fastjson相關(guān)方法,會提示選擇所需要包,此時問題解決,兩個版本的fastjson可同時使用已經(jīng)兼容。
一些需要注意的坑
描述: 引入依賴找不到重命名的shade包
原因:重命名的模塊和需要引入依賴的模塊在一個項目中,idea優(yōu)先找本項目,所以沒有走倉庫
解決方案:
將模塊從項目maven中移除,右鍵項目-maven-unlink maven projects 新建一個項目專門來做依賴
maven-shade-plugins的其他使用打入和排除指定jar包。maven-shade-plugins還有個功能就是打入和排除指定的jar包,通過和指定。
官方配置示例
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>classworlds:classworlds</exclude>
<exclude>junit:junit</exclude>
<exclude>jmock:*</exclude>
<exclude>*:xml-apis</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
<exclude>log4j:log4j:jar:</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>官方配置示例
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>排除包內(nèi)資源。
在上面的pom中使用maven-shade-plugin時,使用來對包內(nèi)META-INF下的一些資源進(jìn)行排除。
如上面的配置中排除META-INF下的資源文件
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot整合ehcache 實現(xiàn)支付超時限制的方法
在線支付系統(tǒng)需要極高的穩(wěn)定性,在有限的系統(tǒng)資源下,穩(wěn)定性優(yōu)先級要高于系統(tǒng)并發(fā)以及用戶體驗,因此需要合理的控制用戶的支付請求。下面通過本文給大家介紹springboot整合ehcache 實現(xiàn)支付超時限制的方法,一起看看吧2018-01-01
SpringBoot項目中實現(xiàn)接口冪等的五種方法
本文介紹了SpringBoot項目中實現(xiàn)接口冪等性的五種主要方案,Token令牌機制、數(shù)據(jù)庫唯一約束、樂觀鎖、分布式鎖和請求摘要,具有一定的參考價值,感興趣的可以了解一下2025-10-10
SpringBoot項目整合Log4j2實現(xiàn)自定義日志打印失效問題解決
這篇文章主要介紹了SpringBoot項目整合Log4j2實現(xiàn)自定義日志打印失效問題解決,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2024-01-01
如何解決Idea沒有elementui標(biāo)簽的代碼提示問題
這篇文章主要介紹了如何解決Idea沒有elementui標(biāo)簽的代碼提示問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

