使用Eclipse在Gradle中选择正确的JRE版本 [英] Choosing the correct JRE version in Gradle with Eclipse
问题描述
我正在使用Gradle与Eclipse插件来为我的项目生成项目文件,但是我无法将其放在 .classpath
中。我可以添加一个JRE容器,但是我不知道如何删除默认的容器,而且由于这个项目是在开发人员之间共享的,谁可能在Eclipse中设置不同的默认值,我想控制这个手动。
我认为这个应该的方式是这样的:
应用插件:'java'
/ pre>
应用插件:'eclipse'
sourceCompatibility = 1.6
由于
targetCompatibility
与sourceCompatibility
,我希望这个设置可以转到Eclipse设置,找到一个与源代码匹配的JRE(是的,我的机器上有一个,包括一个JRE安装和一个单独的JDK安装),然后继续。
但是,它选择默认值,在我的机器上恰好是Java 7。
我尝试添加一些东西到Eclipse配置:
eclipse {
jdt {
sourceCompa tibility = 1.6 //尝试使用和不使用
}
classpath {
//尝试各种方法删除旧条目,其中包括:
file.beforeMerged {p - > ; p.entries.clear()}
//然后我添加正确一个
containers'org.eclipse.jdt.launching.JRE_CONTAINER / org.eclipse.jdt.internal .debug.ui.launcher.StandardVMType / jdk1.6.0_45'
}
}
做这样的事情我最终在
.classpath
中的两个 JRE容器:<?xml version =1.0encoding =UTF-8?>
< classpath>
< classpathentry kind =outputpath =bin/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINERexported =true/>
< classpathentry kind =conpath =org.eclipse.jdt.launching.JRE_CONTAINER / org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType / jdk1.6.0_45exported =true />
< / classpath>
如何告诉毕业生我只需要一个JRE 1.6容器,而不是默认值
我以后:
- Eclipse中的默认设置应该是无关的
- 希望脚本查找容器 - 在上述脚本中,定义要添加的容器的字符串是用户依赖的。我想要更好地查看安装的JRE与
sourceConfiguration
匹配的版本 - 如果在Eclipse中没有安装这样的JRE,我可以抛出错误
我最终以比我想要的更为手工的方式解决这个问题 - 但至少它有效。
为了将设置与实现分开,每个开发人员都有一个 gradle.properties
文件未被检入版本控制。该文件包含以下信息(在我的工作站上):
javaVersion = 1.6
javaPath = C:/ Program / Java / jdk1.6.0_45
jdkName = jdk1.6.0_45
在构建脚本中,我然后执行以下操作来获取所有配置的正确性:
//设置sourceCompatibility
if(project.hasProperty 'javaVersion')){
project.sourceCompatibility = project.javaVersion
}
//设置bootClasspath - 等待评估后,定义所有任务
project.afterEvaluate {
if(project.hasProperty('javaPath')){
project.tasks.withType(AbstractCompile,{
it.options.bootClasspath =$ {project.javaPath} /jre/lib/rt.jar
})
}
}
//配置Eclipse .classpath
project.eclipse.classpath.file 。合并{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
}
}
}
到目前为止,我已经在几个项目中使用它,它的工作,所以我想它至少是可移植的 - 但是可能需要稍微修改让其他人工作。
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
. 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.
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.
Instead, however, it picks the default, which on my machine happens to be Java 7.
I tried adding some stuff to the Eclipse configuration:
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
:
<?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?
Some constraints on what I'm after:
- The default setting in Eclipse should be irrelevant
- Preferrably, I want the script to look up the container - in the above script, the string defining the container to add is user-dependent. I would like it much better to look among "installed JREs" for a version matching that of
sourceConfiguration
- I'm OK with throwing an error if no such JRE is installed in Eclipse.
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. 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.
这篇关于使用Eclipse在Gradle中选择正确的JRE版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!