java 在 Gradle 中,有没有办法将 JDK 设置为 Eclipse Default?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11901874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Gradle, is there a way to set JDK to the Eclipse Default?
提问by JoeG
I am integrating my gradle build files into our eclipse development environment which supports multiple JDKs. While most developers have several versions installed, the correct behavior would be to use the "default JRE" as checked on the System Preferences->Java->Installed JREs page.
我正在将我的 gradle 构建文件集成到我们支持多个 JDK 的 eclipse 开发环境中。虽然大多数开发人员安装了多个版本,但正确的行为是使用“默认 JRE”,如在系统偏好设置->Java->安装的 JRE 页面上选中的。
Is there a way to have gradle set JAVA_HOME (or "org.gradle.java.home"??) to this? If not, any suggestions on the best way to go about this for such a group of developers? This isn't really a problem for just a single person, it is trying to find a general approach that will scale across the group of us that has me searching!
有没有办法让gradle将JAVA_HOME(或“org.gradle.java.home”??)设置为这个?如果没有,对于这样的一组开发人员,有什么关于最好的方法的建议吗?这对于一个人来说并不是真正的问题,它正在尝试找到一种通用方法,可以在让我搜索的我们这群人中扩展!
thanks!
谢谢!
回答by ajoberstar
I'm still not completely sure what you are asking for, but here's a few different takes on it.
我仍然不完全确定你在要求什么,但这里有一些不同的看法。
If what you want to do is have your code (in Gradle and Eclipse) compile so that the bytecode is compatible with a specific version of Java, use something like this. This does not change the version of Java that either Gradle or Eclipse uses during compilation, just makes the end result "bytecode compatible" with the version you specify. The settings that Luis mentions default to the values set at the more general Java plugin level.
sourceCompatibility = '1.6' //or '1.5' or '1.7', etc.
By default, the Gradle Eclipse plugin will generate the following entry in your
.classpath
file. I believe this always points to the default you specify in Eclipse, but I may be wrong.<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
If you want to change what that container is, pick the one you are looking for in Eclipse and then look in the
.classpath
file for the correct container value. Then you can specify it in the build file:eclipse.classpath.containers 'whatever the container value is'
However if what you want is to be able to change the JAVA_HOME that Gradle runs with to match the default chosen in Eclipse, I think you'll have a tough time. I'm not sure if there's a easy place to find that value programmatically. You could probably set it up from the opposite direction though. Have the developers set JAVA_HOME to match what their Eclipse default is. Then they can reference the JAVA_HOME environment variable in the Eclipse config for their JRE.
如果您想要做的是编译您的代码(在 Gradle 和 Eclipse 中),以便字节码与特定版本的 Java 兼容,请使用类似的方法。这不会更改 Gradle 或 Eclipse 在编译期间使用的 Java 版本,只会使最终结果与您指定的版本“兼容字节码”。Luis 提到的设置默认为在更通用的 Java 插件级别设置的值。
sourceCompatibility = '1.6' //or '1.5' or '1.7', etc.
默认情况下,Gradle Eclipse 插件将在您的
.classpath
文件中生成以下条目。我相信这总是指向您在 Eclipse 中指定的默认值,但我可能错了。<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
如果您想更改该容器是什么,请在 Eclipse 中选择您要查找的容器,然后在
.classpath
文件中查找正确的容器值。然后你可以在构建文件中指定它:eclipse.classpath.containers 'whatever the container value is'
但是,如果您想要的是能够更改 Gradle 运行的 JAVA_HOME 以匹配 Eclipse 中选择的默认值,我认为您会遇到困难。我不确定是否有一个容易的地方以编程方式找到该值。不过,您可能可以从相反的方向进行设置。让开发人员设置 JAVA_HOME 以匹配他们的 Eclipse 默认值。然后他们可以在他们的 JRE 的 Eclipse 配置中引用 JAVA_HOME 环境变量。
回答by Luis Ramirez-Monterosa
if you have different versions of jdk it's easy
如果你有不同版本的 jdk,那很容易
apply plugin: 'java'
apply plugin: 'eclipse'
eclipse {
jdt {
//if you want to alter the java versions (by default they are configured with gradle java plugin settings):
sourceCompatibility = 1.6
targetCompatibility = 1.5
now if you're using different JVM 1.6.XX and want to target to specific one I am not sure
现在,如果您使用不同的 JVM 1.6.XX 并希望针对特定的 JVM,我不确定
回答by Adil B
Try installing the "Gradle IDE Pack" from Eclipse Marketplace. I'm on Eclipse Luna and can see this option under the "Preferences > Gradle > Arguments" menu:
尝试从 Eclipse Marketplace 安装“Gradle IDE Pack”。我在 Eclipse Luna 上,可以在“Preferences > Gradle > Arguments”菜单下看到这个选项:
You can verify that your chosen JRE has been used by using the 'file' command, as stated in this answer.
您可以使用“文件”命令来验证您选择的 JRE 是否已被使用,如本答案所述。