Maven“无法解析错误消息” (Java 7 + Maven 2)
我有一个基于 Maven 的 GWT 项目,其中包括 Guava。我在 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 认为它需要编译以下源代码 :番石榴-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 客户端代码。
完整的 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 版本(参见 我的回答),我更新了标题和问题,以尽量为未来的用户提供更多帮助。
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);
^
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:
- Removing the
guava
dependency (leaving onlyguava-gwt
) - Scoping
guava-gwt
toprovided
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.
Edit
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.
Full Maven Output
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] ------------------------------------------------------------------------
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
tl;dr
Maven 2 和 JDK 7 不兼容,因为 Maven 尝试解析在 JDK 7 中已更改的 javac 输出。
完整解释
Raghuram 的注释表明这在 Maven 3+ 中对他有用,这让我走上了探索这一点的道路,而不是作为配置问题,但作为一个实际的 Maven 问题。我开始做更多的测试,发现这个问题:
所以在在那一点上,我清楚地意识到“无法解析错误消息”错误是相关的,并且该问题可能与 guava-gwt 编译关系不大发生更多是因为 Maven 不知道如何正确处理错误。
为了测试这一点,我创建了一个与 Guava 无关的单独 Maven 项目:
pom.xml
ClassWithWarnings.java
你瞧,在使用 Java 7 时,Maven 也在这个项目上坦克:
使用 Java 6,它仍然报告警告,但是可以解析 Javac 输出,因此不会坦克:
所以问题似乎是最新的 Maven 2 版本不知道如何解析来自 Java 7+ javac 的错误消息。 Maven 3 可以。我仍然没有找到这方面的文档,并且有点惊讶的是,当 Maven 尝试针对它不知道如何正确支持的 JDK 版本进行编译时,它没有发出警告。
tl;dr
Maven 2 and JDK 7 are incompatible, as Maven tries to parse javac output which has changed in JDK 7.
Full explanation
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:
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.To test this I created a separate Maven project that has nothing to do with Guava:
pom.xml
ClassWithWarnings.java
Lo and behold, Maven tanks on this project as well when using Java 7:
With Java 6, it still reports the warnings, but can parse the Javac output and so doesn't tank:
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.
将我的评论转换为答案...
确切的 pom 文件以及上面的测试类在我的 Windows 机器上使用 maven 3.0.4 编译得很好。
问题可能出在您使用的 Maven 版本上。或者实际 pom 中可能存在其他 Maven 目标,这可能会导致问题。
Converting my comment to an answer...
The exact pom file along with the test class above compiles fine on my Windows box with maven 3.0.4.
The problem could be with the maven version that you are using. Or there could be other maven goals in the actual pom, which may be causing an issue.
对于类似的问题,我将 maven-compiler-plugin 升级到更高版本。
For a similar problem I upgraded maven-compiler-plugin to a later version.
我们遇到过同样的失败,但使用的是 gradle 而不是 Maven。从 ArrayListMultimap 切换到 LinkedListMultimap 后,错误消失了。看来,在 11.0.2 版本中,至少 ArrayListMultimap 被破坏了。
Happened to us, that we received the exact same failure, but with gradle instead of maven. After switching from ArrayListMultimap to LinkedListMultimap to error is gone. So it seems, that in version 11.0.2 at least the ArrayListMultimap is broken.
看起来它并没有尝试编译 Guava 库,但如果没有完整的 Maven 构建日志,我们就无法判断。
根据您迄今为止发布的信息来看,在编译期间,您的类路径上似乎有两个不兼容版本的类或库。
我将尝试您的测试项目,看看是否可以为您提供更多信息。
编辑:
所以我发现了一些有趣的事情。首先,我能够让你的项目在没有大张旗鼓的情况下工作:(
我将你的 pom 更改为:
默认情况下,你的测试文件不会运行。我重构了它,所以它现在被命名为
SomeTestFileTest< /code> 它将实际运行测试。
我在 OSX 上运行 Maven v2.2.1。在开始之前我还清理了我的 ~/.m2/repository:nuke 你的本地存储库文件夹并重试。如果这不起作用,请告诉我您正在运行的 Maven 版本。
It appears that it's not trying to compile the Guava libraries, but without the full maven build log we can't tell.
Judging by the information you've posted so far, it would appear instead that you have two incompatible versions of a class or library on your classpath during compilation.
I'm going to try your test project and see if I can give you more information.
EDIT:
So I've found a couple of interesting things. First, I was able to get your project to work without a whole lot of fanfare :(
I changed your pom to:
By default though, your test file will not run. I refactored it so it was is now named
SomeTestFileTest
which will actually run the test.I'm running Maven v2.2.1 on OSX. I also cleaned out my ~/.m2/repository before starting. I suggest you try the same: nuke your local repository folder and retry your build. If that doesn't work, let me know what version of maven you're running.