在同一个maven项目中使用多个guava版本

gkl3eglg  于 2021-06-04  发布在  Hadoop
关注(0)|答案(2)|浏览(569)

我的项目中有以下两个依赖项:

<dependency>
  <groupId>com.google.javascript</groupId>
  <artifactId>closure-compiler</artifactId>
  <version>v20141215</version>
  <exclusions>
      <exclusion>
          <groupId>com.google.protobuf</groupId>
          <artifactId>protobuf-java</artifactId>
      </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.apache.hadoop</groupId>
  <artifactId>hadoop-common</artifactId>
  <version>2.4.0</version>
</dependency>

正如您在依赖关系树中看到的,它们都包含不同版本的guava:

[INFO] --- maven-dependency-plugin:2.1:tree (default-cli) @ extraction ---

[INFO] +- com.google.javascript:closure-compiler:jar:v20141215:compile
[INFO] |  +- com.google.javascript:closure-compiler-externs:jar:v20141215:compile
[INFO] |  +- args4j:args4j:jar:2.0.26:compile
[INFO] |  +- com.google.guava:guava:jar:18.0:compile
[INFO] |  +- com.google.code.gson:gson:jar:2.2.4:compile
[INFO] |  \- com.google.code.findbugs:jsr305:jar:1.3.9:compile
[INFO] +- org.apache.hadoop:hadoop-common:jar:2.4.0:compile
[INFO] |  +- org.apache.hadoop:hadoop-annotations:jar:2.4.0:compile
[INFO] |  |  \- jdk.tools:jdk.tools:jar:1.7:system
[INFO] |  +- (com.google.guava:guava:jar:11.0.2:compile - omitted for conflict with 18.0)
[INFO] |  +- ...

众所周知的问题是Guava不能向后兼容。所以我需要两个jar。
我得到的错误如下:

Error:  tried to access method com.google.common.base.Stopwatch.<init>()V from class org.apache.hadoop.mapreduce.lib.input.FileInputFormat

这里已经报道了:https://issues.apache.org/jira/browse/hadoop-10961
此外,他们建议使用shading maven插件来处理:https://groups.google.com/a/cloudera.org/forum/#!主题/cdh用户/d5\U hqusvvl4
我在这里试过:

<build>
<plugins>
   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.6</source> <!-- If you want to use Java 8, change this to "1.8" -->
            <target>1.6</target> <!-- If you want to use Java 8, change this to "1.8" -->
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <relocations>
                        <relocation>
                            <pattern>com.google</pattern>
                            <shadedPattern>project.shaded.com.google</shadedPattern>
                        </relocation>
                    </relocations>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
</build>

但我还是犯了同样的错误。
有人能帮我解决这个问题吗?
谢谢你,菲利克斯

mwkjh3gx

mwkjh3gx1#

我建议找到使用hadoop2.4的最新版本的guava,并将其作为显式依赖项。然后从闭包编译器和hadoop deps中暂时排除guava。
我建议v16,因为它在stopwatch类上仍然有zero-args构造函数:参见guava16
当然,这个解决方案依赖于Guava16使用闭包编译器。

vfwfrxfs

vfwfrxfs2#

也许您应该查看结果jar,看看它是否构建正确。清理本地maven repo可以解决这个问题。

相关问题