Maven“无法解析错误消息"(Java 7 + Maven 2) [英] Maven "could not parse error message" (Java 7 + Maven 2)

查看:18
本文介绍了Maven“无法解析错误消息"(Java 7 + Maven 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 Guava 的基于 Maven 的 GWT 项目.我在 Maven 尝试(但失败)编译它在 guava-gwt*.jar 中找到的源时遇到了麻烦:

I have a maven-based GWT project that includes Guava. I am running into trouble with Maven trying (and failing) to compile the sources that it finds in 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 中的源代码.这是我的项目的样子:

I can't figure out why Maven thinks it needs to compile the sources in guava-gwt. Here's what my project looks like:

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

我已经尝试了以下方法:

I have already tried the following:

  • 移除guava依赖(只留下guava-gwt)
  • 范围 guava-gwtprovided
  • Removing the guava dependency (leaving only guava-gwt)
  • Scoping guava-gwt to provided

我不知道还能尝试什么.guava-gwt 包含源代码,因为 GWT 会将其编译为等效的 Javascript.但我不希望 Maven 尝试编译这些源代码.

I'm not sure what else to try. guava-gwt includes sources because GWT will compile it into equivalent Javascript. But I don't want Maven to try to compile these sources.

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

Just a note...the test files themselves have no real need for guava-gwt over guava since they are compiled and run as Java code (they don't go through the GWT compile step). I don't need guava-gwt specifically for these tests but it needs to be available for my actual GWT client code.

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 版本有关(参见我的回答),我已经更新了标题和问题,试图为未来的用户提供更多帮助.

Edit (again)

Having found that the source of the problem has nothing to do with Guava but rather the Maven version (see my answer), I've updated the title and question to try to be a lot more helpful to future users.

推荐答案

tl;dr

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

tl;dr

Maven 2 and JDK 7 are incompatible, as Maven tries to parse javac output which has changed in JDK 7.

Raghuram 指出这在 Maven 3+ 中对他有用,这让我走上了探索这条道路,而不是将其作为配置问题,而是作为实际的 Maven 问题.我开始做更多的测试,发现这个问题:

Raghuram's note that this worked for him in Maven 3+ took me down the road of exploring this not as a config problem but as an actual Maven problem. I started doing more testing and found that this problem:

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

所以那时我很清楚无法解析错误消息"错误是相关的,并且问题可能与guava-gwt编译发生的关系不大 以及更多与 Maven 不知道如何正确处理错误有关.

So at that point it became clear to me that the "could not parse error message" errors were relevant, and the problem probably had less to do with the guava-gwt compilation occurring and more to do with Maven not knowing how to handle the errors properly.

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

To test this I created a separate Maven project that has nothing to do with Guava:

├── 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 {}

你瞧,Maven 在使用 Java 7 时也参与了这个项目:

Lo and behold, Maven tanks on this project as well when using 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 输出,因此不会停止:

With Java 6, it still reports the warnings, but can parse the Javac output and so doesn't tank:

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 版本进行编译时没有给出警告.

So it seems as if the problem was that the latest Maven 2 release doesn't know how to parse error messages from Java 7+ javac. Maven 3 does. I still haven't found documentation of this and am a little surprised that Maven doesn't give a warning when it tries to compile against a JDK version that it doesn't know how to support properly.

这篇关于Maven“无法解析错误消息"(Java 7 + Maven 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆