[英]Choosing the correct JRE version in Gradle with Eclipse
I'm using Gradle with the Eclipse plugin to generate project files for my project, but I can't get it to put the correct JRE version in .classpath
. 我正在使用Gradle和Eclipse插件为我的项目生成项目文件,但我无法让它在
.classpath
放入正确的JRE版本。 I can add a JRE container, but I can't figure out how to remove the default one - and since this project is shared between developers, who might have varying defaults set in Eclipse, I want to control this manually. 我可以添加一个JRE容器,但我无法弄清楚如何删除默认容器 - 并且由于这个项目是在开发人员之间共享的,他们可能在Eclipse中设置了不同的默认值,我想手动控制它。
The way I think this should work, is like so: 我觉得这应该有用的方式是这样的:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.6
Since targetCompatibility
is the same as sourceCompatibility
, I expect this setup to go to the Eclipse settings, find a JRE that matches the source version (and yes, there is one on my machine - both a JRE installation and a separate JDK installation) and go with it. 由于
targetCompatibility
与sourceCompatibility
相同,我希望这个设置转到Eclipse设置,找到一个与源版本匹配的JRE(是的,我的机器上有一个JRE安装和单独的JDK安装)然后去用它。
Instead, however, it picks the default, which on my machine happens to be Java 7. 相反,它选择了默认值,在我的机器上恰好是Java 7。
I tried adding some stuff to the Eclipse configuration: 我尝试在Eclipse配置中添加一些内容:
eclipse {
jdt {
sourceCompatibility = 1.6 // tried with and without this
}
classpath {
// tried various ways to remove the old entry, among them:
file.beforeMerged { p -> p.entries.clear() }
// and then I add the "correct" one
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45'
}
}
Doing things like this I end up with two JRE containers in .classpath
: 做这样的事情我最终在
.classpath
有两个 JRE容器:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45" exported="true"/>
</classpath>
How do I tell gradle that I only want a JRE 1.6 container, and not the default one too? 我怎么告诉gradle我只想要一个JRE 1.6容器,而不是默认容器?
Some constraints on what I'm after: 对我所追求的一些限制:
sourceConfiguration
- I'm OK with throwing an error if no such JRE is installed in Eclipse. sourceConfiguration
匹配的版本sourceConfiguration
- 如果在Eclipse中没有安装这样的JRE,我可以抛出错误。 I've ended up solving this in a slightly more manual way than I wanted - but at least it works. 我最终以一种比我想要的手动方式解决这个问题 - 但至少它有效。
In order to separate the settings from the implementation, each developer has a gradle.properties
file which is not checked into version control. 为了将设置与实现分开,每个开发人员都有一个
gradle.properties
文件,该文件未检入版本控制。 This file contains the following information (on my workstation): 该文件包含以下信息(在我的工作站上):
javaVersion=1.6
javaPath=C:/Program/Java/jdk1.6.0_45
jdkName=jdk1.6.0_45
In the build script, i then do the following to get all the configuration correct: 在构建脚本中,我然后执行以下操作以使所有配置正确:
// Set sourceCompatibility
if (project.hasProperty('javaVersion')) {
project.sourceCompatibility = project.javaVersion
}
// Set bootClasspath - but wait until after evaluation, to have all tasks defined
project.afterEvaluate {
if (project.hasProperty('javaPath')) {
project.tasks.withType(AbstractCompile, {
it.options.bootClasspath = "${project.javaPath}/jre/lib/rt.jar"
})
}
}
// Configure Eclipse .classpath
project.eclipse.classpath.file.whenMerged { Classpath cp ->
if (project.hasProperty('jdkName') {
cp.entries.findAll { it.path.contains('JRE_CONTAINER') }.each {
it.path += "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/$project.jdkName"
}
}
}
So far I've used it in a couple of projects and it's worked, so I guess it's at least quite portable - but it might be necessary to make slight modifications to make it work for others. 到目前为止,我已经在几个项目中使用它并且它有效,所以我认为它至少非常便携 - 但可能需要稍作修改才能使其适用于其他项目。
I found that the proposed solution causes duplicate entries on subsequent 'gradle eclipse' executions. 我发现建议的解决方案会导致后续“gradle eclipse”执行时出现重复条目。 Borrowing some code from Specifiy JRE Container with gradle eclipse plugin , I came up with the following which seems to work:
借用gradle eclipse插件从Provideiy JRE Container中借用一些代码,我想出了以下似乎有效的方法:
project.afterEvaluate {
// use jre lib matching version used by project, not the workspace default
if (project.sourceCompatibility != null) {
def target = project.sourceCompatibility.toString()
def containerPrefix = "org.eclipse.jdt.launching.JRE_CONTAINER"
def containerSuffix
if (target =~ /1.[4-5]/) {
containerSuffix = '/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-' + target
} else if (target =~ /1.[6-8]/) {
containerSuffix = '/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-' + target
}
if (containerSuffix != null) {
project.eclipse.classpath {
containers.removeAll { it.startsWith(containerPrefix) }
containers.add(containerPrefix + containerSuffix)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.