Maven“无法解析错误消息”(Java 7 + Maven 2)

时间:2012-02-15 17:24:22

标签: java gwt maven guava

我有一个基于maven的GWT项目,其中包括番石榴。我遇到麻烦,Maven尝试(并且失败)编译它在guava-gwt*.jar中找到的来源:

could not parse error message:   symbol:   static setCountImpl
  location: class
/home/mark/.m2/repository/com/google/guava/guava-gwt/11.0.1/guava-gwt-11.0.1.jar(com/google/common/collect/AbstractMultiset.java):100: error: cannot find symbol
    return setCountImpl(this, element, count);
           ^

我无法弄清楚为什么Maven认为需要在guava-gwt中编译源代码。这是我的项目的样子:

├── pom.xml
└── src
    ├── main
    │   └── java
    └── test
        └── java
            └── SomeTestFile.java

SomeTestFile.java

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import org.junit.Test;

public class SomeTestFile {
    @Test
    public void testMethod() {
        Multimap<Integer, String> someMap = ArrayListMultimap.create();
        someMap.put(5, "five");
        System.out.println(someMap);
    }

}

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>guava-problem</groupId>
    <artifactId>guava-problem</artifactId>
    <version>1.0</version>

    <dependencies>

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

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava-gwt</artifactId>
            <version>11.0.1</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我已经尝试了以下内容:

  • 删除guava依赖项(仅保留guava-gwt
  • guava-gwt范围限定为provided

我不确定还有什么可以尝试的。 guava-gwt包含源代码,因为GWT会将其编译为等效的Javascript。但我不希望Maven尝试编译这些来源。

修改

只是注意......测试文件本身并不需要guava-gwt超过guava,因为它们是作为Java代码编译和运行的(它们不通过GWT编译步骤) 。我不需要guava-gwt专门用于这些测试,但它需要可用于我的实际GWT客户端代码。

Full Maven输出

mark@mark-peters:~/devel/guava-problem$ mvn -V clean test-compile
Apache Maven 2.2.1 (rdebian-1)
Java version: 1.7.0
Java home: /usr/lib/jvm/jdk1.7.0/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "2.6.32-38-generic" arch: "amd64" Family: "unix"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - guava-problem:guava-problem:jar:1.0
[INFO]    task-segment: [clean, test-compile]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting file set: /home/mark/devel/guava-problem/target (included: [**], excluded: [])
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/mark/devel/guava-problem/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/mark/devel/guava-problem/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to /home/mark/devel/guava-problem/target/test-classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure

/home/mark/.m2/repository/com/google/guava/guava-gwt/11.0.1/guava-gwt-11.0.1.jar(com/google/common/collect/AbstractMultiset.java):[19,0] error: cannot find symbol

could not parse error message:   symbol:   static setCountImpl
  location: class
/home/mark/.m2/repository/com/google/guava/guava-gwt/11.0.1/guava-gwt-11.0.1.jar(com/google/common/collect/AbstractMultiset.java):100: error: cannot find symbol
    return setCountImpl(this, element, count);
           ^

could not parse error message:   symbol:   method setCountImpl(AbstractMultiset<E>,E,int)
  location: class AbstractMultiset<E>
  where E is a type-variable:
    E extends Object declared in class AbstractMultiset
/home/mark/.m2/repository/com/google/guava/guava-gwt/11.0.1/guava-gwt-11.0.1.jar(com/google/common/collect/AbstractMultiset.java):105: error: cannot find symbol
    return setCountImpl(this, element, oldCount, newCount);
           ^


[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Tue Feb 21 12:49:42 EST 2012
[INFO] Final Memory: 18M/212M
[INFO] ------------------------------------------------------------------------

编辑(再次)

发现问题的根源与Guava无关,而是Maven版本(请参阅my answer),我更新了标题和问题,试图对未来的用户更有帮助

5 个答案:

答案 0 :(得分:22)

TL;博士

Maven 2和JDK 7不兼容,因为Maven尝试解析JDK 7中已更改的javac输出。

完整解释

Raghuram注意到,在Maven 3+中这对他有用,这让我开始探索这个不是配置问题,而是作为一个真正的Maven问题。我开始做更多测试,发现这个问题:

  • 发生在Java 7和Maven 2.2.1
  • Java 7和Maven 3 +不会出现
  • Java 6和Maven 2.2.1不会出现

所以在那一点上,我清楚地知道&#34;无法解析错误消息&#34;错误是相关的,问题可能与guava-gwt编译发生的关系较少,而更多的是与Maven不知道如何正确处理错误。

为了测试这个,我创建了一个与Guava无关的独立Maven项目:

├── pom.xml
└── src
    └── main
        └── java
            └── ClassWithWarnings.java

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>maven-problem</groupId>
    <artifactId>maven-problem</artifactId>
    <version>1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ClassWithWarnings.java

public class ClassWithWarnings implements java.io.Serializable {}

瞧,在使用Java 7时,Maven还可以使用Java 7:

mark@mark-peters:~/devel/maven-problem$ mvn -V compile
Apache Maven 2.2.1 (rdebian-1)
Java version: 1.7.0
Java home: /usr/lib/jvm/jdk1.7.0/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "2.6.32-38-generic" arch: "amd64" Family: "unix"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - maven-problem:maven-problem:jar:1.0
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/mark/devel/maven-problem/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/mark/devel/maven-problem/target/classes
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Compilation failure
could not parse error message: warning: [options] bootstrap class path not set in conjunction with -source 1.3
/home/mark/devel/maven-problem/src/main/java/ClassWithWarnings.java:1: warning: [serial] serializable class ClassWithWarnings has no definition of serialVersionUID
public class ClassWithWarnings implements java.io.Serializable {}
       ^


[INFO] ------------------------------------------------------------------------
[INFO] For more information, run Maven with the -e switch
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Feb 21 13:10:47 EST 2012
[INFO] Final Memory: 14M/150M
[INFO] ------------------------------------------------------------------------

使用Java 6,它仍会报告警告,但可以解析Javac输出,因此无法解决:

Apache Maven 2.2.1 (rdebian-1)
Java version: 1.6.0_20
Java home: /usr/lib/jvm/java-6-openjdk/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux" version: "2.6.32-38-generic" arch: "amd64" Family: "unix"
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - maven-problem:maven-problem:jar:1.0
[INFO]    task-segment: [compile]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/mark/devel/maven-problem/src/main/resources
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to /home/mark/devel/maven-problem/target/classes
[WARNING] /home/mark/devel/maven-problem/src/main/java/ClassWithWarnings.java:[1,7] [serial] serializable class ClassWithWarnings has no definition of serialVersionUID

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue Feb 21 13:18:39 EST 2012
[INFO] Final Memory: 9M/150M
[INFO] ------------------------------------------------------------------------

所以似乎问题是最新的Maven 2版本不知道如何解析来自Java 7+ javac的错误消息。 Maven 3确实如此。我仍然没有找到这方面的文档,并且有点惊讶Maven在尝试针对JDK版本进行编译时没有发出警告,因为它不知道如何正确支持。

答案 1 :(得分:2)

将我的评论转换为答案......

确切的pom文件和上面的测试类在maven 3.0.4的Windows机器上编译得很好。

问题可能在于您正在使用的maven版本。或者在实际的pom中可能存在其他maven目标,这可能会导致问题。

答案 2 :(得分:2)

对于类似的问题,我将maven-compiler-plugin升级到更高版本。

答案 3 :(得分:2)

发生在我们身上,我们收到了完全相同的失败,但是用gradle而不是maven。从ArrayListMultimap切换到LinkedListMultimap后,错误消失了。所以看来,在版本11.0.2中,至少ArrayListMultimap被破坏了。

答案 4 :(得分:1)

看起来它并没有尝试编译Guava库,但是如果没有完整的maven构建日志,我们无法分辨。

根据您目前发布的信息判断,在编译期间,您的类路径上会出现两个不兼容的类或库版本。

我将尝试您的测试项目,看看我是否可以为您提供更多信息。

编辑:

所以我发现了一些有趣的东西。首先,我能够让你的项目无需大肆宣传:(

我改变了你的pom:

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>11.0.1</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava-gwt</artifactId>
        <version>11.0.1</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>

默认情况下,您的测试文件不会运行。我重构了它,因此它现在被命名为SomeTestFileTest,它将实际运行测试。

我在OSX上运行Maven v2.2.1。在开始之前我也清理了我的〜/ .m2 / repository。我建议您尝试相同的方法:nuke您的本地存储库文件夹并重试您的构建。如果这不起作用,请告诉我你正在运行的maven版本。