简体   繁体   English

Eclipse Mars:ANT不再支持JDK 1.6了吗?

[英]Eclipse Mars: ANT doesn't support JDK 1.6 anymore?

When I use JDK6 for calling an ANT script in Eclipse Mars (Run as > Ant Build ... > JRE > Separate JRE), I get following error message: 当我使用JDK6在Eclipse Mars中调用ANT脚本(运行方式> Ant Build ...> JRE> Separate JRE)时,出现以下错误消息:

Problem occured: JRE version less than 1.7 is not supported. 出现问题: 不支持小于1.7的JRE版本。

Is this only a bug or intentionally? 这只是一个bug还是故意的? I can't find a corresponding bug report at Eclipse. 我在Eclipse上找不到相应的错误报告。 So Eclipse dropped Java 6 support for ANT?! 那么Eclipse为ANT放弃了Java 6支持?!

We fixed the problem with a custom ANT plugin. 我们使用自定义ANT插件修复了问题。 It's a replacement of the Mars bundled ANT plugin. 它取代了Mars捆绑的ANT插件。 The original plugin didn't support Java < 7 because it was written with Java 7 syntax and it had a check for Java version. 原始插件不支持Java <7,因为它是用Java 7语法编写的,它检查了Java版本。 It was easy to replace Java7 syntax to be compatible with >= 5 and to remove the Java 7 check. 很容易替换Java7语法以与> = 5兼容并删除Java 7检查。

The two syntax "problems" were: 两个语法“问题”是:

  • Diamond operator, eg List<MyObject> list = new ArrayList<>(); 菱形运算符,例如List<MyObject> list = new ArrayList<>();
  • try-with-resources, eg try (InputStream stream = createInputStream()) { ...} try-with-resources,例如try (InputStream stream = createInputStream()) { ...}

Backwards compatibility for Diamond operator: Diamond运营商的向后兼容性:

List<MyObject> list = new ArrayList<MyObject>();

and for try-with-resources: 并尝试资源:

InputStream stream;

try
{
  ...
}
finally
{
   stream.close();
}

After we replaced the bundled plugin with our custom plugin, it was possible to start an ANT task with a custom JRE, as usual. 在我们使用自定义插件替换捆绑插件后,可以像往常一样使用自定义JRE启动ANT任务。

It's possible to create your own ANT plugin with original sources from Eclipse git repository: http://git.eclipse.org/c/platform/eclipse.platform.git/refs/tags (use Tag ID: I20150430-1445) or to use my pre-compiled bundle: Eclipse Mars ANT plugin with support for Java < 7 可以使用Eclipse git存储库中的原始源创建自己的ANT插件: http ://git.eclipse.org/c/platform/eclipse.platform.git/refs/tags(使用标签ID:I20150430-1445)或者使用我的预编译包: Eclipse Mars ANT插件,支持Java <7

Installation is easy: 安装很简单:

  • Download the zip archive*, extract the content to <eclipse_dir>/plugins. 下载zip存档*,将内容解压缩到<eclipse_dir> / plugins。
  • Start eclipse with parameter -clean (only once) 用参数-clean启动eclipse(只有一次)
  • Configure JRE6 for your ANT task, via Externals Tool configuration... 通过外部工具配置为您的ANT任务配置JRE6 ...

More details about the solution can be found in this blog post . 有关该解决方案的更多详细信息,请参阅此博客文章

Eclipse Mars dropped support for Java 6 :( Eclipse Mars放弃了对Java 6的支持:(

So we have to change to IntelliJ IDEA. 所以我们必须改为IntelliJ IDEA。

See: 看到:

There's something rather messed up about this!? 有一些相当混乱的事情!? My (Windows 7) environment is like this: 我的(Windows 7)环境是这样的:

  • Eclipse Neon R2 (4.6.2) Eclipse Neon R2(4.6.2)
  • JDK 1.8 to start Eclipse JDK 1.8启动Eclipse
  • JDK 1.6 as default JRE in Eclipse JDK 1.6是Eclipse中的默认JRE

In this configuration, one "solution"(!) to this problem is to run the build file using the keyboard shortcut (or RMC->Run as->Ant Build), without any customization in the "External Tools Configuration" dialogs !? 在此配置中,此问题的一个“解决方案”(!)是使用键盘快捷键(或RMC-> Run as-> Ant Build)运行构建文件,而不在“外部工具配置”对话框中进行任何自定义

The build file works fine when I use the keyborard shortcut (Alt-Shift-X, Q), (and it does start javaw , and javac from JDK 1.6 as I've observed from Process Explorer), but once I do a modification to the "launch configuration" (say choose a new target for example), then I also start getting the "JRE version less than 1.7 is not supported" error!? 当我使用keyborard快捷方式(Alt-Shift-X,Q)时,构建文件工作正常(并且它确实从JDK 1.6启动javawjavac ,就像我在Process Explorer中观察到的那样),但是一旦我修改了“启动配置”(例如选择一个新目标),然后我也开始得到“不支持小于1.7的JRE版本”错误!?

If, after getting the error, I go "External Tools Configuration..." -> "Delete selected launch configuration(s)", and start the build with the keyboard shortcut, it works again!? 如果在收到错误后,我进入“外部工具配置...” - >“删除选定的启动配置”,然后使用键盘快捷键启动构建,它再次起作用!?

Apparently the "JDK must be >= 1.7" check (of the default ant plug-in) mentioned by @rjahn above doesn't always get executed, but I haven't debugged it all the way to see why... 显然,@ rjahn上面提到的“JDK必须> = 1.7”检查(默认的ant插件)并不总是被执行,但我没有一直调试它以查看原因...

Java 1.7 required to launch ant build environment, but you can compile class files with any JDK. 启动ant构建环境需要Java 1.7,但您可以使用任何JDK编译类文件。 By default ant using javac and javaw from launch environment, but you can override it within ant tasks. 默认情况下,ant使用来自启动环境的javac和javaw,但您可以在ant任务中覆盖它。

I used the following constants in the examples: 我在示例中使用了以下常量:

<property name="javac.location" value="c:/Program Files/Java/jdk1.6.0_45/bin/javac.exe" />
<property name="java.location" value="c:/Program Files/Java/jdk1.6.0_45/bin/javaw.exe" />

Java compilation: You can define javac location parameter named "executable": Java编译:您可以定义名为“executable”的javac位置参数:

<javac srcdir="@{source.dir}" destdir="@{target.dir}" debug='@{debug}' encoding="UTF-8" fork="true" source="@{source}" target="@{target}" executable="${javac.location}">

After this, ant use javac to compile class files from JDK 1.6. 在此之后,ant使用javac从JDK 1.6编译类文件。

To run application from ant, with java 1.6, use jvm argument for java task: 要使用java 1.6从ant运行应用程序,请对java任务使用jvm参数:

<java classname="com.google.gwt.dev.Compiler" fork="yes" failonerror="true" maxmemory="${gwt.compile.maxmemory}" jvm="${java.location}">

Some ant task use the default compiler by default, for example the wsimport-ant task (it's generate source files from wsdl and compile it, with default java). 默认情况下,某些ant任务使用默认编译器,例如wsimport-ant任务(它从wsdl生成源文件并使用默认java编译它)。 To prevent this, run wsimport with -Xnocompile argument, and compile generated source files with javac (see above). 要防止这种情况,请使用-Xnocompile参数运行wsimport,并使用javac编译生成的源文件(参见上文)。

<wsimport-ant xadditionalHeaders="true">
    <xjcarg value="-XautoNameResolution" />
    <arg value="-d" />
    <arg value="${src-gen.dir}/wsdls" />
    <arg value="-keep" />
    <arg value="@{wsdlsource}" />
    <arg value="-Xnocompile" />
</wsimport-ant>

This methods work flawlessly in latest eclipse (Neon .3) with Oracle JDK 1.6 (or any other JDK). 这种方法在最新的Eclipse(Neon .3)和Oracle JDK 1.6(或任何其他JDK)中完美运行。

Java 6 and 7 no longer receive public updates from Oracle. Java 6和7不再从Oracle 接收公共更新 While you're fine compiling against the runtime libraries from older versions to suit those requirements, you should use a newer JRE to actually run anything. 虽然您可以很好地针对旧版本的运行时库进行编译以满足这些要求,但您应该使用较新的JRE来实际运行任何内容。

This is just plain stupidity. 这只是简单的愚蠢。 We also either have to switch back to older version of eclipse or switch to other IDE. 我们还必须切换回旧版eclipse或切换到其他IDE。 Legacy systems still run in 1.6 and when you can create project with 1.6 jre in Mars,ant should also be able to compile it natively against 1.6. 遗留系统仍然在1.6中运行,当你可以在火星上创建1.6 jre的项目时,ant也应该能够在1.6中本地编译它。

The other solution is to update your build.xml where ever you call javac. 另一个解决方案是在你调用javac的地方更新你的build.xml。 In a corporate build it would be 100s of places. 在企业建设中,它将是100个地方。

Seems every eclipse version screws up some basic things. 似乎每个eclipse版都搞砸了一些基本的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM