NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor conflits on Elastic Search jar

2022-09-03 05:20:19

在创建Elasticsearch客户端时,我得到了异常java.lang.NoSuchMethodError:com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;经过一些查找,像Guava-18这样的接缝在运行时被旧版本覆盖,而Guava-18仅在编译任务期间工作。

我的 Maven 配置如下:

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

如何在执行时强制使用番石榴-18版本?


答案 1

您应该尝试找到“旧”版本的番石榴是从哪里取来的,并一劳永逸地将其排除在外。

查找依赖项 :

mvn dependency:tree | grep guava

排除它 :

<dependency>
  <groupId>org.whatever</groupId>
  <artifactId>the_lib_that_includes_guava</artifactId>
  <version>0.97</version>
  <exclusions>
    <exclusion>
      <artifactId>com.google</artifactId>
      <groupId>guava</groupId>
    </exclusion>
  </exclusions>
</dependency>

有关依赖项排除的详细信息,请参阅 https://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html。


答案 2

我添加正确的弹性搜索依赖关系解决了问题

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

推荐