【问题标题】:Choosing the correct JRE version in Gradle with Eclipse使用 Eclipse 在 Gradle 中选择正确的 JRE 版本
【发布时间】:2013-06-11 05:19:28
【问题描述】:

我正在使用带有 Eclipse 插件的 Gradle 来为我的项目生成项目文件,但我无法将正确的 JRE 版本放入 .classpath。我可以添加一个 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,我可以抛出错误。

【问题讨论】:

  • 另外:正确配置引导类路径以避免warning: [options] bootstrap class path not set in conjunction with -source 1.6 的方式与用户无关(即以某种智能方式查找 JRE 的路径)的奖励积分。

标签: java eclipse gradle


【解决方案1】:

我发现建议的解决方案会导致后续“gradle eclipse”执行出现重复条目​​。从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)
      }
    }
  }
}

【讨论】:

    【解决方案2】:

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

    为了将设置与实现分开,每个开发人员都有一个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"
            }
        }
    }
    

    到目前为止,我已经在几个项目中使用了它并且它工作正常,所以我想它至少非常便携 - 但可能需要稍作修改才能使其适用于其他项目。

    【讨论】:

      猜你喜欢
      • 2015-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多