java 依賴包沖突,使用maven的Shade方式解決

java 依賴包沖突,使用maven的Shade方式解決

[TOC]

問題描述

程序中同時(shí)使用了hadoop工具包與ElasticSearch工具導(dǎo)致jar包。
程序報(bào)錯(cuò)

java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;

內(nèi)容如下

java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
at org.elasticsearch.threadpool.ThreadPool.(ThreadPool.java:190)

原因分析

通過對(duì)上述錯(cuò)誤進(jìn)行g(shù)oogle可以判斷是由于Elasticsearch引用的guava包版本不正確而導(dǎo)致。程序中hadoop依賴的guava包版本為11版本,而ES所需要的版本為18以上。因此我們首先在maven中將guava的版本強(qiáng)制指定為18版本,但是將程序打包后上傳到linux生成環(huán)境程序仍然無法正常運(yùn)行。

解決方案

根據(jù)[官網(wǎng)博客][3]說明,我們將ElasticSearch以及它的相關(guān)依賴包以shade的打包成一個(gè)獨(dú)立的jar包,對(duì)應(yīng)ElasticSearch相關(guān)類的使用均從此jar包引用。

1、shade Elasticsearch包

  • 首先創(chuàng)建新的maven工程,pom.xml文件如下:
<groupId>my.elasticsearch</groupId>
    <artifactId>es-shaded</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <elasticsearch.version>2.1.2</elasticsearch.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <relocations>
                                <relocation>
                                    <pattern>com.google.guava</pattern>
                                    <shadedPattern>my.elasticsearch.guava</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.joda</pattern>
                                    <shadedPattern>my.elasticsearch.joda</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>com.google.common</pattern>
                                    <shadedPattern>my.elasticsearch.common</shadedPattern>
                                </relocation>
                                <relocation>
                                    <pattern>org.elasticsearch</pattern>
                                    <shadedPattern>my.elasticsearch</shadedPattern>
                                </relocation>
                            </relocations>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer" />
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

在pom.xml中我們指定了該項(xiàng)目依賴org.elasticsearch包,且版本為2.1.2,并強(qiáng)制指定了guava的版本為18(此處若不指定應(yīng)該也會(huì)自行依賴18以上的包,但并未進(jìn)行測(cè)試)。然后在build標(biāo)簽中可以看出,我們利用maven的shade工具完成打包情況如下:

  • org.joda映射為my.elasticsearch.joda
  • com.google.guava映射為my.elasticsearch.guava
  • com.google.common映射為my.elasticsearch.common
  • org.elasticsearch映射為my.elasticsearch

然后利用mvn clean install命令進(jìn)行打包得到es-shaded-1.0-SNAPSHOT.jar,創(chuàng)建一個(gè)屬于你自己版本的Elasticsearch包。之后將該包上傳到私服maven鏡像。

2、在工程中使用自己的Elasticsearch包

完成上數(shù)對(duì)Elasticsearch的打包之后,在自己工程中的pom.xml中,我們引用此包方式如下:

<dependencies>
       ...
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>${hadoop.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.antlr</groupId>
      <artifactId>ST4</artifactId>
      <version>4.0.8</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>my.elasticsearch</groupId>
      <artifactId>es-shaded</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>

在使用上述方式引用了Elasticsearch包之后,在程序中我們可以這樣對(duì)Elasticsearch包進(jìn)行引用
代碼如下:

import my.elasticsearch.ElasticsearchException;
import my.elasticsearch.action.bulk.BulkItemResponse;
import my.elasticsearch.action.bulk.BulkRequestBuilder;
import my.elasticsearch.action.bulk.BulkResponse;
import my.elasticsearch.action.index.IndexRequest;
import my.elasticsearch.client.transport.NoNodeAvailableException;

這樣確保了我們使用的elasticsearch包是我們之前創(chuàng)建的。對(duì)Elasticsearch所依賴版本的 joda相關(guān)包的引用方式也是類似:

import my.elasticsearch.joda.time.DateTime;

這樣就不會(huì)出現(xiàn)Elasticsearch依賴包不正確的情況。

參考文獻(xiàn)

[1] https://www.elastic.co/blog/to-shade-or-not-to-shade
[2] http://www.cnblogs.com/bigbigtree/p/6668542.html
[3]:https://www.elastic.co/blog/to-shade-or-not-to-shade

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,253評(píng)論 6 342
  • 最近做的個(gè)項(xiàng)目要在原工程里引入個(gè)二方hqueue的jar包,包里用了hbase的包又引用了Guava11版本,但是...
    死拍照的閱讀 5,393評(píng)論 3 2
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 原文鏈接:http://www.dropwizard.io/1.2.0/docs/getting-started....
    Lance_Xu閱讀 1,146評(píng)論 0 0
  • 簡(jiǎn)書 Wwwwei轉(zhuǎn)載請(qǐng)注明原創(chuàng)出處,謝謝! 一些概念 Maven 一個(gè)項(xiàng)目管理工具,簡(jiǎn)單的理解為一種標(biāo)準(zhǔn)化的方式...
    Wwwwei閱讀 9,306評(píng)論 13 57

友情鏈接更多精彩內(nèi)容