Tom*_*han 5 java eclipse gradle
我正在使用Gradle和Eclipse插件为我的项目生成项目文件,但我无法让它将正确的JRE版本放入其中.classpath.我可以添加一个JRE容器,但我无法弄清楚如何删除默认容器 - 并且由于这个项目是在开发人员之间共享的,他们可能在Eclipse中设置了不同的默认值,我想手动控制它.
我觉得这应该有用的方式是这样的:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.6
Run Code Online (Sandbox Code Playgroud)
既然targetCompatibility是相同的sourceCompatibility,我希望这个设置转到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'
}
}
Run Code Online (Sandbox Code Playgroud)
做这样的事情我最终得到两个 JRE容器.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>
Run Code Online (Sandbox Code Playgroud)
我怎么告诉gradle我只想要一个JRE 1.6容器,而不是默认容器?
对我所追求的一些限制:
sourceConfiguration与之匹配的版本更好- 如果在Eclipse中没有安装这样的JRE,我可以抛出错误.我最终以比我想要的稍微手动的方式解决了这个问题 - 但至少它有效。
为了将设置与实现分开,每个开发人员都有一个gradle.properties未签入版本控制的文件。该文件包含以下信息(在我的工作站上):
javaVersion=1.6
javaPath=C:/Program/Java/jdk1.6.0_45
jdkName=jdk1.6.0_45
Run Code Online (Sandbox Code Playgroud)
在构建脚本中,我执行以下操作以使所有配置正确:
// 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"
}
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经在几个项目中使用了它并且它起作用了,所以我想它至少相当便携 - 但可能需要进行一些细微的修改才能使其适用于其他项目。
| 归档时间: |
|
| 查看次数: |
6791 次 |
| 最近记录: |