使用Eclipse在Gradle中选择正确的JRE版本

时间:2013-06-14 14:11:48

标签: java eclipse gradle

我正在使用Gradle和Eclipse插件为我的项目生成项目文件,但我无法在.classpath中使用正确的JRE版本。我可以添加一个JRE容器,但我无法弄清楚如何删除默认容器 - 并且由于这个项目是在开发人员之间共享的,他们可能在Eclipse中设置了不同的默认值,我想控制这是手动。

我认为这个应该工作的方式是这样的:

apply plugin: 'java'
apply plugin: 'eclipse'

sourceCompatibility = 1.6

由于targetCompatibilitysourceCompatibility相同,我希望此设置转到Eclipse设置,找到与源版本匹配的JRE(是的,我的机器上有一个 - 两者都有一个JRE安装和一个单独的JDK安装)并继续使用它。

相反,它选择了默认值,在我的机器上恰好是Java 7。

我尝试在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'
    }
}

做这样的事情我最终在.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>

如何告诉gradle我想要一个JRE 1.6容器,而也是默认容器?


对我所追求的一些限制:

  • Eclipse中的默认设置应该是无关紧要的
  • 我希望脚本能够查找容器 - 在上面的脚本中,定义要添加的容器的字符串是依赖于用户的。我希望在“已安装的JRE”中查找与sourceConfiguration匹配的版本更好 - 如果在Eclipse中没有安装此类JRE,我可以抛出错误。

2 个答案:

答案 0 :(得分:4)

我最终以一种比我想要的更手动的方式来解决这个问题 - 但至少它有效。

为了将设置与实现分开,每个开发人员都有一个gradle.properties文件,该文件未检入版本控制。该文件包含以下信息(在我的工作站上):

javaVersion=1.6
javaPath=C:/Program/Java/jdk1.6.0_45
jdkName=jdk1.6.0_45

在构建脚本中,我会执行以下操作以使所有配置正确无误:

// 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"
        }
    }
}

到目前为止,我已经在一些项目中使用它并且它有效,所以我认为它至少非常便携 - 但可能需要稍作修改以使其适用于其他项目。

答案 1 :(得分:2)

我发现建议的解决方案会导致后续“日食”的重复输入。处决。借用Specifiy JRE Container with gradle eclipse plugin中的一些代码,我提出了以下似乎有用的代码:

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)
      }
    }
  }
}