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

时间:2021-04-12 17:03:43

I have the following two dependencies in my project:

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

<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>

As you can see in the dependency tree, they both contain a different version of Guava:

正如您在依赖关系树中看到的,它们都包含不同版本的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] |  +- ...

The well known problem is that Guava is not backward compatible. Therefore I kind of need both jars.

众所周知的问题是,番石榴并不是向后兼容的。所以我需要两个罐子。

The error - I get - is the following:

我得到的误差是:

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

This has already reported here: https://issues.apache.org/jira/browse/HADOOP-10961

这里已经报告了:https://. apache.org/jira/browse/hadoop -10961。

Moreover they suggest to handle it by using the shading Maven plugin: https://groups.google.com/a/cloudera.org/forum/#!topic/cdh-user/d5_HqUSvVl4

此外,他们建议使用shading Maven插件:https://groups.google.com/a/cloudera.org/forum/#!topic/cdh-user/ d5_hqusvl4。

Which I tried here:

我试着在这里:

<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>

But I still get the same error.

但我还是会犯同样的错误。

Can anyone help me with this Maven issue?

有人能帮我解决这个Maven的问题吗?

Thank you, Felix

谢谢你,费利克斯

2 个解决方案

#1


1  

I would suggest finding the latest version of Guava that functions with Hadoop 2.4 and including that as an explicit dependency. Then exclude Guava from being fetched transiently from the closure compiler and Hadoop deps.

我建议找到使用Hadoop 2.4的最新版本的Guava,并将其作为显式依赖项。然后将Guava排除在关闭编译器和Hadoop deps之外。

I'd suggest v16 as that still has the zero-args constructor on the StopWatch class: see Guava 16

我建议v16在StopWatch类上仍然有zero-args构造函数:见Guava 16。

Of course this solution depends on Guava 16 working with the closure compiler.

当然,这个解决方案依赖于Guava 16与闭包编译器一起工作。

#2


0  

Perhaps you should look into the resulting JAR and see whether it's built correctly. Cleaning the local maven repo may solve the problem.

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

#1


1  

I would suggest finding the latest version of Guava that functions with Hadoop 2.4 and including that as an explicit dependency. Then exclude Guava from being fetched transiently from the closure compiler and Hadoop deps.

我建议找到使用Hadoop 2.4的最新版本的Guava,并将其作为显式依赖项。然后将Guava排除在关闭编译器和Hadoop deps之外。

I'd suggest v16 as that still has the zero-args constructor on the StopWatch class: see Guava 16

我建议v16在StopWatch类上仍然有zero-args构造函数:见Guava 16。

Of course this solution depends on Guava 16 working with the closure compiler.

当然,这个解决方案依赖于Guava 16与闭包编译器一起工作。

#2


0  

Perhaps you should look into the resulting JAR and see whether it's built correctly. Cleaning the local maven repo may solve the problem.

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