@@ -56,7 +56,8 @@ import java.time.ZonedDateTime
5656 */
5757class BuildPlugin implements Plugin<Project > {
5858
59- static final JavaVersion minimumJava = JavaVersion . VERSION_1_8
59+ static final JavaVersion minimumRuntimeVersion = JavaVersion . VERSION_1_8
60+ static final JavaVersion minimumCompilerVersion = JavaVersion . VERSION_1_9
6061
6162 @Override
6263 void apply (Project project ) {
@@ -93,20 +94,26 @@ class BuildPlugin implements Plugin<Project> {
9394 /* * Performs checks on the build environment and prints information about the build environment. */
9495 static void globalBuildInfo (Project project ) {
9596 if (project. rootProject. ext. has(' buildChecksDone' ) == false ) {
96- String javaHome = findJavaHome()
97+ String compilerJavaHome = findCompilerJavaHome()
98+ String runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome)
9799 File gradleJavaHome = Jvm . current(). javaHome
98100 String javaVendor = System . getProperty(' java.vendor' )
99101 String javaVersion = System . getProperty(' java.version' )
100102 String gradleJavaVersionDetails = " ${ javaVendor} ${ javaVersion} " +
101103 " [${ System.getProperty('java.vm.name')} ${ System.getProperty('java.vm.version')} ]"
102104
103- String javaVersionDetails = gradleJavaVersionDetails
104- JavaVersion javaVersionEnum = JavaVersion . current()
105- if (new File (javaHome). canonicalPath != gradleJavaHome. canonicalPath) {
106- javaVersionDetails = findJavaVersionDetails(project, javaHome)
107- javaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, javaHome))
108- javaVendor = findJavaVendor(project, javaHome)
109- javaVersion = findJavaVersion(project, javaHome)
105+ String compilerJavaVersionDetails = gradleJavaVersionDetails
106+ JavaVersion compilerJavaVersionEnum = JavaVersion . current()
107+ if (new File (compilerJavaHome). canonicalPath != gradleJavaHome. canonicalPath) {
108+ compilerJavaVersionDetails = findJavaVersionDetails(project, compilerJavaHome)
109+ compilerJavaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, compilerJavaHome))
110+ }
111+
112+ String runtimeJavaVersionDetails = gradleJavaVersionDetails
113+ JavaVersion runtimeJavaVersionEnum = JavaVersion . current()
114+ if (new File (runtimeJavaHome). canonicalPath != gradleJavaHome. canonicalPath) {
115+ runtimeJavaVersionDetails = findJavaVersionDetails(project, runtimeJavaHome)
116+ runtimeJavaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, runtimeJavaHome))
110117 }
111118
112119 // Build debugging info
@@ -115,11 +122,13 @@ class BuildPlugin implements Plugin<Project> {
115122 println ' ======================================='
116123 println " Gradle Version : ${ project.gradle.gradleVersion} "
117124 println " OS Info : ${ System.getProperty('os.name')} ${ System.getProperty('os.version')} (${ System.getProperty('os.arch')} )"
118- if (gradleJavaVersionDetails != javaVersionDetails ) {
125+ if (gradleJavaVersionDetails != compilerJavaVersionDetails || gradleJavaVersionDetails != runtimeJavaVersionDetails ) {
119126 println " JDK Version (gradle) : ${ gradleJavaVersionDetails} "
120127 println " JAVA_HOME (gradle) : ${ gradleJavaHome} "
121- println " JDK Version (compile) : ${ javaVersionDetails} "
122- println " JAVA_HOME (compile) : ${ javaHome} "
128+ println " JDK Version (compile) : ${ compilerJavaVersionDetails} "
129+ println " JAVA_HOME (compile) : ${ compilerJavaHome} "
130+ println " JDK Version (runtime) : ${ runtimeJavaVersionDetails} "
131+ println " JAVA_HOME (runtime) : ${ runtimeJavaHome} "
123132 } else {
124133 println " JDK Version : ${ gradleJavaVersionDetails} "
125134 println " JAVA_HOME : ${ gradleJavaHome} "
@@ -135,54 +144,49 @@ class BuildPlugin implements Plugin<Project> {
135144 }
136145
137146 // enforce Java version
138- if (javaVersionEnum < minimumJava ) {
139- throw new GradleException (" Java ${ minimumJava } or above is required to build Elasticsearch" )
147+ if (compilerJavaVersionEnum < minimumCompilerVersion ) {
148+ throw new GradleException (" Java ${ minimumCompilerVersion } or above is required to build Elasticsearch" )
140149 }
141150
142- // this block of code detecting buggy JDK 8 compiler versions can be removed when minimum Java version is incremented
143- assert minimumJava == JavaVersion . VERSION_1_8 : " Remove JDK compiler bug detection only applicable to JDK 8"
144- if (javaVersionEnum == JavaVersion . VERSION_1_8 ) {
145- if (Objects . equals(" Oracle Corporation" , javaVendor)) {
146- def matcher = javaVersion =~ / 1\. 8\. 0(?:_(\d +))?/
147- if (matcher. matches()) {
148- int update;
149- if (matcher. group(1 ) == null ) {
150- update = 0
151- } else {
152- update = matcher. group(1 ). toInteger()
153- }
154- if (update < 40 ) {
155- throw new GradleException (" JDK ${ javaVendor} ${ javaVersion} has compiler bug JDK-8052388, update your JDK to at least 8u40" )
156- }
157- }
158- }
151+ if (runtimeJavaVersionEnum < minimumRuntimeVersion) {
152+ throw new GradleException (" Java ${ minimumRuntimeVersion} or above is required to run Elasticsearch" )
159153 }
160154
161- project. rootProject. ext. javaHome = javaHome
162- project. rootProject. ext. javaVersion = javaVersionEnum
155+ project. rootProject. ext. compilerJavaHome = compilerJavaHome
156+ project. rootProject. ext. runtimeJavaHome = runtimeJavaHome
157+ project. rootProject. ext. compilerJavaVersion = compilerJavaVersionEnum
158+ project. rootProject. ext. runtimeJavaVersion = runtimeJavaVersionEnum
163159 project. rootProject. ext. buildChecksDone = true
164160 }
165- project. targetCompatibility = minimumJava
166- project. sourceCompatibility = minimumJava
161+
162+ project. targetCompatibility = minimumRuntimeVersion
163+ project. sourceCompatibility = minimumRuntimeVersion
164+
167165 // set java home for each project, so they dont have to find it in the root project
168- project. ext. javaHome = project. rootProject. ext. javaHome
169- project. ext. javaVersion = project. rootProject. ext. javaVersion
166+ project. ext. compilerJavaHome = project. rootProject. ext. compilerJavaHome
167+ project. ext. runtimeJavaHome = project. rootProject. ext. runtimeJavaHome
168+ project. ext. compilerJavaVersion = project. rootProject. ext. compilerJavaVersion
169+ project. ext. runtimeJavaVersion = project. rootProject. ext. runtimeJavaVersion
170170 }
171171
172- /* * Finds and enforces JAVA_HOME is set */
173- private static String findJavaHome () {
174- String javaHome = System . getenv(' JAVA_HOME' )
172+ private static String findCompilerJavaHome () {
173+ final String javaHome = System . getenv(' JAVA_HOME' )
175174 if (javaHome == null ) {
176175 if (System . getProperty(" idea.active" ) != null || System . getProperty(" eclipse.launcher" ) != null ) {
177- // intellij doesn't set JAVA_HOME, so we use the jdk gradle was run with
178- javaHome = Jvm . current(). javaHome
176+ // IntelliJ does not set JAVA_HOME, so we use the JDK that Gradle was run with
177+ return Jvm . current(). javaHome
179178 } else {
180- throw new GradleException (' JAVA_HOME must be set to build Elasticsearch' )
179+ throw new GradleException (" JAVA_HOME must be set to build Elasticsearch" )
181180 }
182181 }
183182 return javaHome
184183 }
185184
185+ private static String findRuntimeJavaHome (final String compilerJavaHome ) {
186+ assert compilerJavaHome != null
187+ return System . getenv(' RUNTIME_JAVA_HOME' ) ?: compilerJavaHome
188+ }
189+
186190 /* * Finds printable java version of the given JAVA_HOME */
187191 private static String findJavaVersionDetails (Project project , String javaHome ) {
188192 String versionInfoScript = ' print(' +
@@ -412,19 +416,19 @@ class BuildPlugin implements Plugin<Project> {
412416
413417 /* * Adds compiler settings to the project */
414418 static void configureCompile (Project project ) {
415- if (project. javaVersion < JavaVersion . VERSION_1_10 ) {
419+ if (project. compilerJavaVersion < JavaVersion . VERSION_1_10 ) {
416420 project. ext. compactProfile = ' compact3'
417421 } else {
418422 project. ext. compactProfile = ' full'
419423 }
420424 project. afterEvaluate {
421425 project. tasks. withType(JavaCompile ) {
422- File gradleJavaHome = Jvm . current() . javaHome
426+ final JavaVersion targetCompatibilityVersion = JavaVersion . toVersion(it . targetCompatibility)
423427 // we fork because compiling lots of different classes in a shared jvm can eventually trigger GC overhead limitations
424428 options. fork = true
425- options. forkOptions. executable = new File (project. javaHome, ' bin/javac ' )
429+ options. forkOptions. javaHome = new File (project. compilerJavaHome )
426430 options. forkOptions. memoryMaximumSize = " 1g"
427- if (project . targetCompatibility > = JavaVersion . VERSION_1_8 ) {
431+ if (targetCompatibilityVersion = = JavaVersion . VERSION_1_8 ) {
428432 // compile with compact 3 profile by default
429433 // NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
430434 if (project. compactProfile != ' full' ) {
@@ -448,21 +452,18 @@ class BuildPlugin implements Plugin<Project> {
448452 options. encoding = ' UTF-8'
449453 options. incremental = true
450454
451- if (project. javaVersion == JavaVersion . VERSION_1_9 ) {
452- // hack until gradle supports java 9's new "--release" arg
453- assert minimumJava == JavaVersion . VERSION_1_8
454- options. compilerArgs << ' --release' << ' 8'
455- }
455+ // TODO: use native Gradle support for --release when available (cf. https://github.com/gradle/gradle/issues/2510)
456+ options. compilerArgs << ' --release' << targetCompatibilityVersion. majorVersion
456457 }
457458 }
458459 }
459460
460461 static void configureJavadoc (Project project ) {
461462 project. tasks. withType(Javadoc ) {
462- executable = new File (project. javaHome , ' bin/javadoc' )
463+ executable = new File (project. compilerJavaHome , ' bin/javadoc' )
463464 }
464465 configureJavadocJar(project)
465- if (project. javaVersion == JavaVersion . VERSION_1_10 ) {
466+ if (project. compilerJavaVersion == JavaVersion . VERSION_1_10 ) {
466467 project. tasks. withType(Javadoc ) { it. enabled = false }
467468 project. tasks. getByName(' javadocJar' ). each { it. enabled = false }
468469 }
@@ -508,7 +509,7 @@ class BuildPlugin implements Plugin<Project> {
508509 ' X-Compile-Lucene-Version' : VersionProperties . lucene,
509510 ' X-Compile-Elasticsearch-Snapshot' : isSnapshot,
510511 ' Build-Date' : ZonedDateTime . now(ZoneOffset . UTC ),
511- ' Build-Java-Version' : project. javaVersion )
512+ ' Build-Java-Version' : project. compilerJavaVersion )
512513 if (jarTask. manifest. attributes. containsKey(' Change' ) == false ) {
513514 logger. warn(' Building without git revision id.' )
514515 jarTask. manifest. attributes(' Change' : ' Unknown' )
@@ -545,7 +546,7 @@ class BuildPlugin implements Plugin<Project> {
545546 /* * Returns a closure of common configuration shared by unit and integration tests. */
546547 static Closure commonTestConfig (Project project ) {
547548 return {
548- jvm " ${ project.javaHome } /bin/java"
549+ jvm " ${ project.runtimeJavaHome } /bin/java"
549550 parallelism System . getProperty(' tests.jvms' , ' auto' )
550551 ifNoTests ' fail'
551552 onNonEmptyWorkDirectory ' wipe'
0 commit comments