@@ -55,7 +55,8 @@ import java.time.ZonedDateTime
5555 */
5656class BuildPlugin implements Plugin<Project > {
5757
58- static final JavaVersion minimumJava = JavaVersion . VERSION_1_8
58+ static final JavaVersion minimumRuntimeVersion = JavaVersion . VERSION_1_8
59+ static final JavaVersion minimumCompilerVersion = JavaVersion . VERSION_1_9
5960
6061 @Override
6162 void apply (Project project ) {
@@ -92,20 +93,26 @@ class BuildPlugin implements Plugin<Project> {
9293 /* * Performs checks on the build environment and prints information about the build environment. */
9394 static void globalBuildInfo (Project project ) {
9495 if (project. rootProject. ext. has(' buildChecksDone' ) == false ) {
95- String javaHome = findJavaHome()
96+ String compilerJavaHome = findCompilerJavaHome()
97+ String runtimeJavaHome = findRuntimeJavaHome(compilerJavaHome)
9698 File gradleJavaHome = Jvm . current(). javaHome
9799 String javaVendor = System . getProperty(' java.vendor' )
98100 String javaVersion = System . getProperty(' java.version' )
99101 String gradleJavaVersionDetails = " ${ javaVendor} ${ javaVersion} " +
100- " [${ System.getProperty('java.vm.name')} ${ System.getProperty('java.vm.version')} ]"
101-
102- String javaVersionDetails = gradleJavaVersionDetails
103- JavaVersion javaVersionEnum = JavaVersion . current()
104- if (new File (javaHome). canonicalPath != gradleJavaHome. canonicalPath) {
105- javaVersionDetails = findJavaVersionDetails(project, javaHome)
106- javaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, javaHome))
107- javaVendor = findJavaVendor(project, javaHome)
108- javaVersion = findJavaVersion(project, javaHome)
102+ " [${ System.getProperty('java.vm.name')} ${ System.getProperty('java.vm.version')} ]"
103+
104+ String compilerJavaVersionDetails = gradleJavaVersionDetails
105+ JavaVersion compilerJavaVersionEnum = JavaVersion . current()
106+ if (new File (compilerJavaHome). canonicalPath != gradleJavaHome. canonicalPath) {
107+ compilerJavaVersionDetails = findJavaVersionDetails(project, compilerJavaHome)
108+ compilerJavaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, compilerJavaHome))
109+ }
110+
111+ String runtimeJavaVersionDetails = gradleJavaVersionDetails
112+ JavaVersion runtimeJavaVersionEnum = JavaVersion . current()
113+ if (new File (runtimeJavaHome). canonicalPath != gradleJavaHome. canonicalPath) {
114+ runtimeJavaVersionDetails = findJavaVersionDetails(project, runtimeJavaHome)
115+ runtimeJavaVersionEnum = JavaVersion . toVersion(findJavaSpecificationVersion(project, runtimeJavaHome))
109116 }
110117
111118 // Build debugging info
@@ -114,11 +121,13 @@ class BuildPlugin implements Plugin<Project> {
114121 println ' ======================================='
115122 println " Gradle Version : ${ project.gradle.gradleVersion} "
116123 println " OS Info : ${ System.getProperty('os.name')} ${ System.getProperty('os.version')} (${ System.getProperty('os.arch')} )"
117- if (gradleJavaVersionDetails != javaVersionDetails ) {
124+ if (gradleJavaVersionDetails != compilerJavaVersionDetails || gradleJavaVersionDetails != runtimeJavaVersionDetails ) {
118125 println " JDK Version (gradle) : ${ gradleJavaVersionDetails} "
119126 println " JAVA_HOME (gradle) : ${ gradleJavaHome} "
120- println " JDK Version (compile) : ${ javaVersionDetails} "
121- println " JAVA_HOME (compile) : ${ javaHome} "
127+ println " JDK Version (compile) : ${ compilerJavaVersionDetails} "
128+ println " JAVA_HOME (compile) : ${ compilerJavaHome} "
129+ println " JDK Version (runtime) : ${ runtimeJavaVersionDetails} "
130+ println " JAVA_HOME (runtime) : ${ runtimeJavaHome} "
122131 } else {
123132 println " JDK Version : ${ gradleJavaVersionDetails} "
124133 println " JAVA_HOME : ${ gradleJavaHome} "
@@ -134,48 +143,48 @@ class BuildPlugin implements Plugin<Project> {
134143 }
135144
136145 // enforce Java version
137- if (javaVersionEnum < minimumJava ) {
138- throw new GradleException (" Java ${ minimumJava } or above is required to build Elasticsearch" )
146+ if (compilerJavaVersionEnum < minimumCompilerVersion ) {
147+ throw new GradleException (" Java ${ minimumCompilerVersion } or above is required to build Elasticsearch" )
139148 }
140149
141- // this block of code detecting buggy JDK 8 compiler versions can be removed when minimum Java version is incremented
142- assert minimumJava == JavaVersion . VERSION_1_8 : " Remove JDK compiler bug detection only applicable to JDK 8"
143- if (javaVersionEnum == JavaVersion . VERSION_1_8 ) {
144- if (Objects . equals(" Oracle Corporation" , javaVendor)) {
145- def matcher = javaVersion =~ / 1\. 8\. 0(?:_(\d +))?/
146- if (matcher. matches()) {
147- int update;
148- if (matcher. group(1 ) == null ) {
149- update = 0
150- } else {
151- update = matcher. group(1 ). toInteger()
152- }
153- if (update < 40 ) {
154- throw new GradleException (" JDK ${ javaVendor} ${ javaVersion} has compiler bug JDK-8052388, update your JDK to at least 8u40" )
155- }
156- }
157- }
150+ if (runtimeJavaVersionEnum < minimumRuntimeVersion) {
151+ throw new GradleException (" Java ${ minimumRuntimeVersion} or above is required to run Elasticsearch" )
158152 }
159153
160- project. rootProject. ext. javaHome = javaHome
161- project. rootProject. ext. javaVersion = javaVersionEnum
154+ project. rootProject. ext. compilerJavaHome = compilerJavaHome
155+ project. rootProject. ext. runtimeJavaHome = runtimeJavaHome
156+ project. rootProject. ext. compilerJavaVersion = compilerJavaVersionEnum
157+ project. rootProject. ext. runtimeJavaVersion = runtimeJavaVersionEnum
162158 project. rootProject. ext. buildChecksDone = true
163159 }
164160
161+ project. targetCompatibility = minimumRuntimeVersion
162+ project. sourceCompatibility = minimumRuntimeVersion
163+
165164 // set java home for each project, so they dont have to find it in the root project
166- project. ext. javaHome = project. rootProject. ext. javaHome
167- project. ext. javaVersion = project. rootProject. ext. javaVersion
165+ project. ext. compilerJavaHome = project. rootProject. ext. compilerJavaHome
166+ project. ext. runtimeJavaHome = project. rootProject. ext. runtimeJavaHome
167+ project. ext. compilerJavaVersion = project. rootProject. ext. compilerJavaVersion
168+ project. ext. runtimeJavaVersion = project. rootProject. ext. runtimeJavaVersion
168169 }
169170
170171 /* * Finds and enforces JAVA_HOME is set */
171- private static String findJavaHome () {
172- String javaHome = System . getenv(' JAVA_HOME' )
172+ private static String findCompilerJavaHome () {
173+ return findJavaHome(System . getenv(' JAVA_HOME' ), null )
174+ }
175+
176+ private static String findRuntimeJavaHome (final String compilerJavaHome ) {
177+ return findJavaHome(System . getenv(' RUNTIME_JAVA_HOME' ), compilerJavaHome)
178+ }
179+
180+ private static String findJavaHome (String maybeJavaHome , String defaultJavaHome ) {
181+ final String javaHome = maybeJavaHome ?: defaultJavaHome
173182 if (javaHome == null ) {
174183 if (System . getProperty(" idea.active" ) != null || System . getProperty(" eclipse.launcher" ) != null ) {
175- // intellij doesn't set JAVA_HOME, so we use the jdk gradle was run with
184+ // IntelliJ does not set JAVA_HOME, so we use the JDK that Gradle was run with
176185 javaHome = Jvm . current(). javaHome
177186 } else {
178- throw new GradleException ( ' JAVA_HOME must be set to build Elasticsearch ' )
187+ assert false
179188 }
180189 }
181190 return javaHome
@@ -409,25 +418,19 @@ class BuildPlugin implements Plugin<Project> {
409418
410419 /* * Adds compiler settings to the project */
411420 static void configureCompile (Project project ) {
421+ if (project. compilerJavaVersion < JavaVersion . VERSION_1_10 ) {
422+ project. ext. compactProfile = ' compact3'
423+ } else {
424+ project. ext. compactProfile = ' full'
425+ }
412426 project. afterEvaluate {
413427 project. tasks. withType(JavaCompile ) {
414- final JavaVersion sourceCompatibilityJavaVersion = JavaVersion . toVersion(it. sourceCompatibility)
415- final File gradleJavaHome
416- if (sourceCompatibilityJavaVersion > minimumJava) {
417- final String javaHomeSourceCompatibility = System . getenv(" JAVA_${ sourceCompatibilityJavaVersion.majorVersion} _HOME" )
418- if (javaHomeSourceCompatibility == null ) {
419- throw new GradleException (" JAVA_${ sourceCompatibilityJavaVersion.majorVersion} _HOME must be set to build Elasticsearch" )
420- }
421- gradleJavaHome = new File (javaHomeSourceCompatibility)
422- } else {
423- gradleJavaHome = Jvm . current(). javaHome
424- }
425-
428+ final JavaVersion targetCompatibilityVersion = JavaVersion . toVersion(it. targetCompatibility)
426429 // we fork because compiling lots of different classes in a shared jvm can eventually trigger GC overhead limitations
427430 options. fork = true
428- options. forkOptions. javaHome = gradleJavaHome
431+ options. forkOptions. javaHome = new File (project . compilerJavaHome)
429432 options. forkOptions. memoryMaximumSize = " 1g"
430- if (sourceCompatibilityJavaVersion == JavaVersion . VERSION_1_8 ) {
433+ if (targetCompatibilityVersion == JavaVersion . VERSION_1_8 ) {
431434 // compile with compact 3 profile by default
432435 // NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
433436 options. compilerArgs << ' -profile' << ' compact3'
@@ -448,22 +451,17 @@ class BuildPlugin implements Plugin<Project> {
448451
449452 options. encoding = ' UTF-8'
450453 options. incremental = true
451-
452- if (sourceCompatibilityJavaVersion > JavaVersion . VERSION_1_8 ) {
453- // hack until gradle supports java 9's new "--release" arg
454- assert minimumJava == JavaVersion . VERSION_1_8
455- options. compilerArgs << ' --release' << it. sourceCompatibility
456- }
454+ options. compilerArgs << ' --release' << targetCompatibilityVersion. majorVersion
457455 }
458456 }
459457 }
460458
461459 static void configureJavadoc (Project project ) {
462460 project. tasks. withType(Javadoc ) {
463- executable = new File (project. javaHome , ' bin/javadoc' )
461+ executable = new File (project. compilerJavaHome , ' bin/javadoc' )
464462 }
465463 configureJavadocJar(project)
466- if (project. javaVersion == JavaVersion . VERSION_1_10 ) {
464+ if (project. compilerJavaVersion == JavaVersion . VERSION_1_10 ) {
467465 project. tasks. withType(Javadoc ) { it. enabled = false }
468466 project. tasks. getByName(' javadocJar' ). each { it. enabled = false }
469467 }
@@ -509,7 +507,7 @@ class BuildPlugin implements Plugin<Project> {
509507 ' X-Compile-Lucene-Version' : VersionProperties . lucene,
510508 ' X-Compile-Elasticsearch-Snapshot' : isSnapshot,
511509 ' Build-Date' : ZonedDateTime . now(ZoneOffset . UTC ),
512- ' Build-Java-Version' : project. javaVersion )
510+ ' Build-Java-Version' : project. compilerJavaVersion )
513511 if (jarTask. manifest. attributes. containsKey(' Change' ) == false ) {
514512 logger. warn(' Building without git revision id.' )
515513 jarTask. manifest. attributes(' Change' : ' Unknown' )
@@ -546,7 +544,7 @@ class BuildPlugin implements Plugin<Project> {
546544 /* * Returns a closure of common configuration shared by unit and integration tests. */
547545 static Closure commonTestConfig (Project project ) {
548546 return {
549- jvm " ${ project.javaHome } /bin/java"
547+ jvm " ${ project.runtimeJavaHome } /bin/java"
550548 parallelism System . getProperty(' tests.jvms' , ' auto' )
551549 ifNoTests ' fail'
552550 onNonEmptyWorkDirectory ' wipe'
0 commit comments