回答自我,并感谢@JBNizet提供初始解决方案...
解决方案确实是使用JavaVersion
,并且碰巧两者都接受a作为参数...sourceCompatibility
targetCompatibility
JavaVersion
因此,构建文件已变为:
def javaVersion = JavaVersion.VERSION_1_7;
sourceCompatibility = javaVersion;
targetCompatibility = javaVersion; // defaults to sourceCompatibility
然后是任务:
task enforceVersion << {
def foundVersion = JavaVersion.current();
if (foundVersion != javaVersion)
throw new IllegalStateException("Wrong Java version; required is "
+ javaVersion + ", but found " + foundVersion);
}
compileJava.dependsOn(enforceVersion);
它的工作原理是:
$ ./gradlew clean compileJava
:clean UP-TO-DATE
:enforceVersion FAILED
FAILURE: Build failed with an exception.
* Where:
Build file '/home/fge/src/perso/grappa-tracer-backport/build.gradle' line: 55
* What went wrong:
Execution failed for task ':enforceVersion'.
> Wrong Java version; required is 1.7, but found 1.8