Eclipse 构建器与 m2Eclipse 和 Maven 不兼容
我遇到一个问题,即 Maven 构建的给定源文件生成的类文件与 Eclipse 构建的类文件不同。
Eclipse 构建器正在优化以下方法:
public SomeClass clone() {
SomeClass clone = (SomeClass) super.clone();
return clone;
}
该方法显然是不必要的,但我无法选择更改源,因为它是生成的。
Eclipse 使用哪个构建器来编译这些类?我正在使用 m2Eclipse 插件,我认为它只是调用 Maven 来执行构建。为什么 Eclipse 认为它需要使用不同的构建器执行另一个构建?我可以禁用此 Eclipse 构建器或配置它以防止优化吗?
- Eclipse版本:3.7
- m2Eclipse版本:1.0.100.20110804-1717(配置为使用外部Maven)
- Maven版本:2.2.1
更新
我发现了一个Eclipse bug 报告 描述了正在发生的情况。需要澄清的是,实际上 Eclipse 编译器并没有删除任何内容,它只是没有引入标准 JDK 编译器引入的合成“桥接”方法。该错误报告被标记为“已解决 - 不会修复”。
我仍然没有找到一种方法来阻止 Eclipse 构建器在 Maven 构建器运行后运行。但我们的修复方法是修改代码生成器,将显式的serialVersionUID 添加到生成的代码中。
I'm seeing an issue where a given source file built by Maven produces a class file that is different than the one built by Eclipse.
The Eclipse builder is optimising away the following method:
public SomeClass clone() {
SomeClass clone = (SomeClass) super.clone();
return clone;
}
This method is obviously unnecessary but I don't have the option to change the source because it is generated.
Which builder is Eclipse using to compile these classes? I'm using the m2Eclipse plugin which I thought just invoked Maven to perform the build. Why is it that Eclipse then thinks it needs to perform another build with a different builder? Can I disable this Eclipse builder or configure it to prevent optimisation?
- Eclipse version: 3.7
- m2Eclipse version: 1.0.100.20110804-1717 (configured to use external Maven)
- Maven version: 2.2.1
Update
I found an Eclipse bug report that describes what is going on. To clarify, its not actually the Eclipse compiler that is removing anything, it is just not introducing a synthetic 'bridge' method that the standard JDK compiler introduces. The bug report is marked as Resolved- Won't Fix.
I still haven't found a way to prevent the Eclipse builder from running after the Maven builder has run. But our fix is to modify the code generator to add an explicit serialVersionUID to the generated code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果为项目进行了配置,您可以尝试禁用
Java Builder
。您可以通过单击Project
中的Builders
选项卡来检查这一点 ->属性
。从 Eclipse 帮助...
You could try disabling
Java Builder
if configured for the project. You can check this by clicking onBuilders
tab inProject
->Properties
.From Eclipse help...
我没有 Eclipse 3.7,但我希望这些事情自 3.6 以来没有改变。如果你打开属性->在您的项目构建器中,您应该会看到 Maven 构建器排在最后,或者至少在 Java 构建器之后。这意味着当 Maven Builder 看到您的 Java 源文件时,它们已经被编译了。顺便说一句,您配置的外部Maven仅在您执行Run as -> >时使用。而不是使用 Maven 构建时。
所有这些都是说,您所看到的可能是这样的事实:当您从 Eclipse 中编译时,使用内部 Java 编译器,而当您从命令行运行 Maven 时,我希望使用 JDK 编译器,具体取决于什么你已经安装了。我认为你无法做任何事情来改变这种状态。您可以尝试的一件事是,通过添加类似于“到您的构建”部分的内容,在 POM 中指定要兼容的 Java 版本,以确保它们的行为尽可能接近
。
我不得不说,我期望 Java 编译器不允许从类中删除公共方法,即使它们是无用的。
I don't have Eclipse 3.7, but I expect that these things haven't changed since 3.6 . If you open Properties -> Builder on your project you should see that the Maven Builder comes last, or at least after the Java Builder. This means that when the Maven Builder sees your Java source files they are already compiled. By the way, the external Maven you configured is only used when you perform Run as -> and not when building with Maven.
All this is to say that what you see is likely do to the fact that when you compile from within Eclipse the internal Java compiler is used, while when you run Maven from the command line I expect that a JDK compiler is used, depending on what you have installed. I don't think there's anything you can do to change this state of things. One thing you could try is to ensure that their behaviour is as close as possible by specifying in your POM which Java version to be compliant to by adding something like
To your build section.
I have to say that I expected that the Java compiler was not allowed to remove public methods from classes, even if they are useless.