【问题标题】:Use multiple Guava versions in same maven project在同一个 Maven 项目中使用多个 Guava 版本
【发布时间】:2015-02-06 14:44:53
【问题描述】:

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

<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 不向后兼容。因此我有点需要两个罐子。

我得到的错误如下:

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

此外,他们建议使用着色 Maven 插件来处理它: https://groups.google.com/a/cloudera.org/forum/#!topic/cdh-user/d5_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>

但我仍然遇到同样的错误。

谁能帮我解决这个 Maven 问题?

谢谢, 费利克斯

【问题讨论】:

  • 你解决了吗?

标签: java maven hadoop guava


【解决方案1】:

我建议找到与 Hadoop 2.4 配合使用的最新版本的 Guava,并将其作为显式依赖项包含在内。然后将 Guava 排除在从闭包编译器和 Hadoop deps 中临时获取的之外。

我建议使用 v16,因为它在 StopWatch 类上仍然具有零参数构造函数:请参阅 Guava 16

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

【讨论】:

  • 这就是问题所在。闭包需要Guava 18
  • v20140303 只需要 Guava 16。即使指定了版本,也不一定是绝对要求。
  • 好想用最新版:(
  • 为什么要使用最新版本?你似乎“想要”,而不是“需要”。 Guava 版本 16.0.1 与当前版本相比相当完整。 Herehere 是当前版本 16 和 18 之间的最新版本差异。你真的需要这些新功能吗?
  • 我想使用最新版本的闭包编译器。因为它可能更稳定,这对我的应用程序非常重要。你对番石榴是对的^^
【解决方案2】:

也许您应该查看生成的 JAR 并查看它是否正确构建。清理本地 maven repo 可能会解决问题。

【讨论】:

  • 我清理了本地maven repo,也可以在jar的对应文件夹中找到Guava类^^
猜你喜欢
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多