This repository was archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathjava-compatibility.gradle
76 lines (71 loc) · 2.91 KB
/
java-compatibility.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* Enable Java compatibility support.
*
* Simply set sourceCompatibility (or targetCompatibility) to automatically fork the correct JDK.
* In tests, set the executable to the desired version, e.g. "1.9", and it will be pointed to the
* correct executable. Executable lookup is done lazily when the task runs, so users lacking the
* right JDK can still run any other task, and (with a warning) any compile task using an older
* JDK.
*/
import org.gradle.internal.jvm.JavaInfo
import org.gradle.internal.jvm.Jvm
// These JDKs must be installed
// For other versions, we will fall back to the closest required JDK if unavailable
TreeSet<JavaVersion> requiredJdks = [JavaVersion.toVersion("8"), JavaVersion.toVersion("11")]
// Map Java version to JVM information, using $JAVA_x_HOME environment variables
// and falling back to gradle properties as an extra option for devs.
Map<Integer, Jvm> jvms = [:].withDefault { String versionString ->
JavaVersion version = JavaVersion.toVersion(versionString)
JavaVersion fallback = requiredJdks.higher(version);
String propertyName = "java${version.majorVersion}Home"
String envName = "JAVA_${version.majorVersion}_HOME"
JavaInfo javaInfo
if (project.hasProperty(propertyName)) {
javaInfo = Jvm.forHome(file(project.getProperty(propertyName)))
} else if (System.env.containsKey(envName)) {
javaInfo = Jvm.forHome(file(System.env[envName]))
} else if (!requiredJdks.contains(version) && fallback != null) {
logger.info "Java ${versionString} not found; falling back to ${fallback}";
return jvms[fallback.majorVersion];
} else {
throw new RuntimeException("Set the property $propertyName in your"
+ "$HOME/.gradle/gradle.properties, pointing to a Java $version installation")
}
logger.info "Java ${versionString} found at ${javaInfo.javaHome}"
return javaInfo
}
jvms[(JavaVersion.current().name)] = Jvm.current()
project.ext.jvms = jvms
// Fork the correct JVM version if installed.
// Runs lazily so users lacking some JVMs can still run most tasks.
tasks.withType(JavaCompile) {
doFirst{
if (targetCompatibility != JavaVersion.current().name) {
options.with {
try {
fork javaHome: jvms[targetCompatibility].javaHome
} catch (RuntimeException e) {
if (JavaVersion.toVersion(targetCompatibility).compareTo(JavaVersion.current()) < 0) {
logger.warn "Java ${targetCompatibility} not available, using ${JavaVersion.current()}"
} else {
throw e
}
}
}
}
}
}
// Sets the correct JVM executable path on tests.
// Runs lazily so users lacking the correct version can run all other tasks.
afterEvaluate {
tasks.withType(Test) {
if (executable =~ /^\d+(\.\d+)?$/) {
def version = executable
executable = new Object() {
@Override public String toString() {
return jvms[version].javaExecutable.toString()
}
}
}
}
}