如何以独立于操作系统的方式设置Gradle`options.bootClasspath`? [英] How to set Gradle `options.bootClasspath` in an os independent manner?
问题描述
因为我的Java源代码和目标必须兼容JRE 1.6,所以我需要将 options.bootClasspath
设置为包含1.6版本 rt.jar
和 jce.jar
。它必须建立在Windows和Unix(Linux / Solaris)上。什么是正确的方法来做到这一点?我现在在我的顶级 build.gradle
中使用以下方法,它可以工作,但它看起来远非优雅,特别是依赖于操作系统的分隔符 :或
;
:
import org .apache.tools.ant.taskdefs.condition.Os
子项目{
apply插件:'java'
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
def java6_home = System.getenv(JAVA_HOME_6)
def java6_lib =C:/ localdata / Program Files(x86)/Java/jdk1.6.0_45/jre/lib /
if(java6_home!= null){
java6_lib = java6_home +/ jre / lib /
}
def sep =' :'
if(Os.isFamily(Os.FAMILY_WINDOWS)){
sep =';'
}
options.bootClasspath = java6_lib +rt.jar+ sep + java6_lib +jce.jar
}
}
我使用下面的代码(假设JDK6_HOME p对于JDK 1.6安装的根目录):
pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b if(sourceCompatibility =='1.6'&& System.env.JDK6_HOME!= null){
options.fork = true
options.bootClasspath =$ System.env.JDK6_HOME / jre / lib / rt.jar
options.bootClasspath + =$ File.pathSeparator $ System.env.JDK6_HOME / jre / lib / jsse.jar
//以上面的行为例添加jce.jar
//和其他特定的JDK jars
}
}
}
这种方法会自动检测环境变量的存在并为所有声明 sourceCompatibility
的模块自动设置 bootClasspath
为1.6。
当使用 bootClasspath
时需要 options.fork = true
。
Because my Java sources and targets must be JRE 1.6 compatible, I need to set options.bootClasspath
to a path that contains the 1.6 versions of rt.jar
and jce.jar
. It must build on both Windows and Unix (Linux/Solaris). What is the proper way to do this? I now use the following approach in my top-level build.gradle
, it works, but it seems far from elegant, especially the os-dependent separator :
or ;
:
import org.apache.tools.ant.taskdefs.condition.Os
subprojects {
apply plugin: 'java'
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
def java6_home = System.getenv("JAVA_HOME_6")
def java6_lib = "C:/localdata/Program Files (x86)/Java/jdk1.6.0_45/jre/lib/"
if (java6_home != null) {
java6_lib = java6_home + "/jre/lib/"
}
def sep = ':'
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
sep = ';'
}
options.bootClasspath = java6_lib + "rt.jar" + sep + java6_lib + "jce.jar"
}
}
I am using the following code (assuming the JDK6_HOME points to the root of the JDK 1.6 installation):
tasks.withType(JavaCompile) {
doFirst {
if (sourceCompatibility == '1.6' && System.env.JDK6_HOME != null) {
options.fork = true
options.bootClasspath = "$System.env.JDK6_HOME/jre/lib/rt.jar"
options.bootClasspath += "$File.pathSeparator$System.env.JDK6_HOME/jre/lib/jsse.jar"
// use the line above as an example to add jce.jar
// and other specific JDK jars
}
}
}
This approach automatically detects the presence of the environment variable and automatically sets the bootClasspath
for all modules that declare sourceCompatibility
as 1.6.
The options.fork = true
is required when you use bootClasspath
.
这篇关于如何以独立于操作系统的方式设置Gradle`options.bootClasspath`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!