这个问题告诉我,确实可以使用--target和--source以及更新的--release命令将较新的版本编译为旧版本。Java 10和Java 8在设置上非常不同;10使用模块,而8没有。
我尝试将sourceCompatibility = 1.8添加到我的build.gradle中,并收到一个modules are not supported in -source 8错误。这当然是意料之中的,也是有道理的。
我是否可以做类似的事情,或者准确地这样做,这样我就可以在我的Gradle组装任务中输入一个var来“切换”Java 8和Java 10 code代码。
发布于 2019-02-13 16:43:38
事实证明我可以。我输入了这个问题,然后做了更多的研究。唯一的警告是,您不能使用任何更高级别的Java添加。
使用Gradle,我在run语句中包含了一个变量,类似于这个gradle clean assemble -PjavaVer=8
使用该变量,我基本上可以关闭想要运行的代码Id。见下面的例子。
if(project.hasProperty('javaVer') && javaVer == '8') {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
afterEvaluate {
repositories {
mavenCentral()
jcenter()
flatDir {
dirs 'lib'
}
}
compileJava {
if (project.hasProperty(('javaVer')) && javaVer == '8') {
excludes = ['**/module-info.java']
} else {
doFirst {
options.compilerArgs = [
'--module-path', classpath.asPath,
]
classpath = files()
}
}
}
}这将检测变量是否设置为8,如果是,则执行module-info.java类,并将sourceCompatibility和targetCompatibility设置为1.8。这对我有用。
请注意,如果使用Java 10或9特定代码,即JavaforJava10,您不能这样做。这就是为什么我们必须排除var类,因为它是在Java9中引入的。
希望这能帮到其他人。
发布于 2019-04-07 18:10:50
也许这对您有用--使用最近的Gradle Modules插件(从1.5.0版本开始),您实际上可以编译:
module-info.java除外)转到Java 8,module-info.java,分别转到Java 9。这是通过modularity.mixedJavaRelease方法自动完成的。
下面是如何将插件应用于主build.gradle的示例(假设它是一个多项目构建):
plugins {
// your remaining plugins here
id 'org.javamodularity.moduleplugin' version '1.5.0' apply false
}
subprojects {
// your remaining subproject configuration here
apply plugin: 'org.javamodularity.moduleplugin'
modularity.mixedJavaRelease 8 // sets "--release 8" for main code, and "--release 9" for "module-info.java"
// test.moduleOptions.runOnClasspath = true // optional (if you want your tests to still run on classpath)
}https://stackoverflow.com/questions/54675324
复制相似问题