4

我已经安装Maven 3.0.4Homebrew,每当我运行mvn命令时,我都会得到以下信息:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableSet;

从谷歌搜索错误似乎与某种谷歌番石榴工具/库有关——我对此一无所知。我试图替换/bin目录中的一些罐子,maven但没有影响

有什么帮助吗?我不知道问题从哪里开始,但现在我无法使用mvn.

这似乎是一个相关的问题,但我的项目没有直接引用guava*.jar文件:

获取 gdata 服务时出现 NoSuchMethod 错误

这是完整的 mvn -X (我在当前目录中使用 pom.xml 运行)

    Jordans-iMac:spring-mvc-setup jordanbaucke$ mvn -X
Apache Maven 3.0.4 (r1232337; 2012-01-17 01:44:56-0700)
Maven home: /usr/local/Cellar/maven/3.0.4/libexec
Java version: 1.7.0_09, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.8.2", arch: "x86_64", family: "mac"
---------------------------------------------------
constituent[0]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/aether-api-1.13.1.jar
constituent[1]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/aether-connector-wagon-1.13.1.jar
constituent[2]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/aether-impl-1.13.1.jar
constituent[3]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/aether-spi-1.13.1.jar
constituent[4]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/aether-util-1.13.1.jar
constituent[5]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/commons-cli-1.2.jar
constituent[6]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-aether-provider-3.0.4.jar
constituent[7]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-artifact-3.0.4.jar
constituent[8]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-compat-3.0.4.jar
constituent[9]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-core-3.0.4.jar
constituent[10]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-embedder-3.0.4.jar
constituent[11]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-model-3.0.4.jar
constituent[12]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-model-builder-3.0.4.jar
constituent[13]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-plugin-api-3.0.4.jar
constituent[14]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-repository-metadata-3.0.4.jar
constituent[15]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-settings-3.0.4.jar
constituent[16]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/maven-settings-builder-3.0.4.jar
constituent[17]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/plexus-cipher-1.7.jar
constituent[18]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/plexus-component-annotations-1.5.5.jar
constituent[19]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/plexus-interpolation-1.14.jar
constituent[20]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/plexus-sec-dispatcher-1.3.jar
constituent[21]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/plexus-utils-2.0.6.jar
constituent[22]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/sisu-guava-0.9.9.jar
constituent[23]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/sisu-guice-3.1.0-no_aop.jar
constituent[24]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/sisu-inject-bean-2.3.0.jar
constituent[25]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/sisu-inject-plexus-2.3.0.jar
constituent[26]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/wagon-file-2.2.jar
constituent[27]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/wagon-http-2.2-shaded.jar
constituent[28]: file:/usr/local/Cellar/maven/3.0.4/libexec/lib/wagon-provider-api-2.2.jar
---------------------------------------------------
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableSet;
    at com.google.inject.internal.InstanceBindingImpl.<init>(InstanceBindingImpl.java:51)
    at com.google.inject.internal.BindingBuilder.toInstance(BindingBuilder.java:95)
    at com.google.inject.internal.InjectorShell$RootModule.configure(InjectorShell.java:329)
    at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:229)
    at com.google.inject.spi.Elements.getElements(Elements.java:103)
    at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:136)
    at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:104)
    at com.google.inject.Guice.createInjector(Guice.java:94)
    at com.google.inject.Guice.createInjector(Guice.java:71)
    at com.google.inject.Guice.createInjector(Guice.java:61)
    at org.codehaus.plexus.DefaultPlexusContainer.addPlexusInjector(DefaultPlexusContainer.java:470)
    at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:196)
    at org.codehaus.plexus.DefaultPlexusContainer.<init>(DefaultPlexusContainer.java:160)
    at org.apache.maven.cli.MavenCli.container(MavenCli.java:375)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:191)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

更新

降级到 Java 1.6,同时卸载 Macports。同样的问题。

4

4 回答 4

3

问题是错误的 MAVEN_HOME 变量集,因为它指向错误的文件夹

Maven home: /usr/local/Cellar/maven/3.0.4/libexec

此外,您使用的安装看起来不像 maven.apache.org 下载页面中的默认安装。

您应该从 maven.apache.org 下载apache-maven-3.0.4-bin.tar.gz解压缩它并将 MAVEN_HOME 指向提取的存档的根目录,例如:

/usr/local/apache-maven-3.0.4

并将 bin 文件夹 /usr/local/apache-maven-3.0.4/bin 放入 PATH 中。如果你打电话

mvn --version

应该打印出或多或少类似于以下内容的内容:

mac:~ km$ mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 09:44:56+0100)
Maven home: /usr/share/maven
Java version: 1.6.0_26, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.5.8", arch: "x86_64", family: "mac"
mac:~ km$ 
于 2013-01-04T21:30:57.337 回答
2

看起来您安装的 Maven 版本依赖于不正确的 Guava 版本。Maven 3 显然依赖于 Guice 和 Guava,因此该错误并非完全是虚假的。这可能是您使用的 Homebrew 包的问题,​​它具有不正确的库依赖项(它似乎使用的是 Guava 的 0.9.9。您应该向包维护者提出这个问题和/或安装 tarball / zip 版本而不是使用自制。

于 2013-01-04T21:30:12.240 回答
0

可能是您的某些 jar 将依赖于 google/guava jar,如果它们不在构建路径中,或者如果它们中有多个,则可能会引发不一致,从而导致错误。一个快速的解决方案是将最新版本的番石榴添加到您的 pom

 <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>24.0-jre</version>
    </dependency>

现在检查依赖层次结构,如果除了 guava 之外的任何 Jar 引用任何其他旧 jar 的 guava/google-collections。如果是这样,那么排除它,像这样

<exclusions>
            <exclusion>
                <groupId>com.google.collections</groupId>
                <artifactId>google-collections</artifactId>
            </exclusion>
 </exclusions>
于 2020-08-03T18:26:53.717 回答
0

在 OS X 上遇到了类似的问题,使用 maven 3.2.2 和 Java 1.8。'mvn clean test' 失败,没有找到类定义错误。将 maven 升级到 3.3.9 并且能够成功运行 mvn 命令。

于 2016-07-07T23:57:13.183 回答