1. 程式人生 > >java jar包衝突解決方案

java jar包衝突解決方案

引言

在使用java語言開發,maven做專案管理時,我們經常遇到一個頭疼的問題就是jar包衝突。比如專案中不同的兩個依賴A和B,都同時依賴了一個專案C,但是依賴的版本不同,這時候我們就要考慮這兩個版本之間是否有衝突,不解決這些衝突,我們的專案雖然能編譯成功,但執行的時候總是會報各種異常。
這種衝突有兩種,比如A依賴C1.0,B依賴C2.0,如果依賴能夠向下相容,即A依賴變成C2.0也能執行,那這種情況很容易解決,我們只需要使用exclusion來排除一個版本即可,比如pom中maven配置如下:

    <dependencies>
        <dependency
>
<groupId>group.A</groupId> <artifactId>artifact.A</artifactId> <exclusions> <exclusion> <groupId>group.C</groupId> <artifactId>artifact.C</artifactId>
</exclusion> </exclusions> </dependency> <dependency> <groupId>group.B</groupId> <artifactId>artifact.B</artifactId> </dependency> </dependencies>

這裡就是排除A依賴的C,只保留B依賴的C的版本。

典型案例

但是使用過程中,我們常常發現這種方式不是完全有效,有些jar包是不能向下相容的,實際應用中幾個典型的例子如:
1.protobuf jar包衝突
當我們使用protobuf做序列化工具時,protobuf的2.4版本和2.5版本就不能夠相容,如果生成使用2.4,但實際專案依賴了2.5,就會出現執行異常:

Exception in thread "main" java.lang.UnsupportedOperationException: This is supposed to be overridden by subclasses.
    at com.google.protobuf.GeneratedMessage.getUnknownFields(GeneratedMessage.java:180)
    at com.test.utill.TextFormat$Printer.print(TextFormat.java:251)
    at com.test.utill.MessageLiteToStringUtil.toString(MessageLiteToStringUtil.java:20)
    at com.test.protocol.ProtocolTest.main(ProtocolTest.java:23)

2.elasticsearch和hbase依賴的guava包衝突
ES2.2.0 & Hbase1.1.2對 Guava的依賴,ES依賴於Guava 18.0,Hbase 依賴於Guava12.0 對於需要ES&Hbase 整合的專案中兩者不相容,會讓我們執行時候出現問題。

解決方案

解決方案就是使用Apache maven shade外掛,具體使用方法可以參考我的Apache maven shade plugin使用說明
我們以protobuf jar衝突解決方案來說明如何避免這些問題:
假設功能(或者模組)A依賴protobuf 2.4.1,B依賴protobuf 2.5.1,在同一個專案中需要使用A和B,那如何使用shade來解決問題呢?
首先,我們需要將A獨立處理,並用shade工具封裝成獨立的jar包,然後在專案中引用封裝的jar包,以及B和protobuf 2.5.1包。
1.A和protobuf 2.4.1的shade處理:
只貼出maven配置:

<project >
  ……
    <groupId>group.A</groupId>
    <artifactId>shade.artifact.A</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.4.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>com.google.protobuf</pattern>
                                    <shadedPattern>shaded.protobuf</shadedPattern>
                                </relocation>
                            </relocations>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
……
</project>

為了區分,我們將shade封裝後的artifact定義為shade.artifact.A。
2.整個專案的依賴

<dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.5.1</version>
        </dependency>
            <groupId>group.A</groupId>
            <artifactId>shade.artifact.A</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

我們注意到,專案依賴了protobuf的2.5.1,以及1中shade封裝的版本,這樣,就能解決我們的問題。