Skip to content

Commit 351eabe

Browse files
committed
Merge branch 'compile-with-jdk-9' into mrjar
* compile-with-jdk-9: Runtime Java home Fix reindex from remote tests Update docs Require JDK 9 for compilation
2 parents 2522787 + 95475f9 commit 351eabe

File tree

15 files changed

+102
-107
lines changed

15 files changed

+102
-107
lines changed

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ Contributing to the Elasticsearch codebase
9292

9393
**Repository:** [https://github.com/elastic/elasticsearch](https://github.com/elastic/elasticsearch)
9494

95+
JDK 9 is required to build Elasticsearch. You must have a JDK 9 installation
96+
with the environment variable `JAVA_HOME` referencing the path to Java home for
97+
your JDK 9 installation. By default, tests use the same runtime as `JAVA_HOME`.
98+
However, since Elasticsearch, supports JDK 8 the build supports compiling with
99+
JDK 9 and testing on a JDK 8 runtime; to do this, set `RUNTIME_JAVA_HOME`
100+
pointing to the Java home of a JDK 8 installation. Note that this mechanism can
101+
be used to test against other JDKs as well, this is not only limited to JDK 8.
102+
95103
Elasticsearch uses the Gradle wrapper for its build. You can execute Gradle
96104
using the wrapper via the `gradlew` script in the root of the repository.
97105

buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ import java.time.ZonedDateTime
5555
*/
5656
class 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'

buildSrc/src/main/groovy/org/elasticsearch/gradle/plugin/PluginBuildPlugin.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public class PluginBuildPlugin extends BuildPlugin {
169169
Files.copy(jarFile.resolveSibling(sourcesFileName), jarFile.resolveSibling(clientSourcesFileName),
170170
StandardCopyOption.REPLACE_EXISTING)
171171

172-
if (project.javaVersion < JavaVersion.VERSION_1_10) {
172+
if (project.compilerJavaVersion < JavaVersion.VERSION_1_10) {
173173
String javadocFileName = jarFile.fileName.toString().replace('.jar', '-javadoc.jar')
174174
String clientJavadocFileName = clientFileName.replace('.jar', '-javadoc.jar')
175175
Files.copy(jarFile.resolveSibling(javadocFileName), jarFile.resolveSibling(clientJavadocFileName),

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/JarHellTask.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class JarHellTask extends LoggedExec {
4242
inputs.files(classpath)
4343
dependsOn(classpath)
4444
description = "Runs CheckJarHell on ${classpath}"
45-
executable = new File(project.javaHome, 'bin/java')
45+
executable = new File(project.runtimeJavaHome, 'bin/java')
4646
doFirst({
4747
/* JarHell doesn't like getting directories that don't exist but
4848
gradle isn't especially careful about that. So we have to do it

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/LoggerUsageTask.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class LoggerUsageTask extends LoggedExec {
4444
project.afterEvaluate {
4545
dependsOn(classpath)
4646
description = "Runs LoggerUsageCheck on ${classDirectories}"
47-
executable = new File(project.javaHome, 'bin/java')
47+
executable = new File(project.runtimeJavaHome, 'bin/java')
4848
if (classDirectories == null) {
4949
// Default to main and test class files
5050
List files = []

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/NamingConventionsTask.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public class NamingConventionsTask extends LoggedExec {
8080
FileCollection classpath = project.sourceSets.test.runtimeClasspath
8181
inputs.files(classpath)
8282
description = "Tests that test classes aren't misnamed or misplaced"
83-
executable = new File(project.javaHome, 'bin/java')
83+
executable = new File(project.runtimeJavaHome, 'bin/java')
8484
if (false == checkForTestsInMain) {
8585
/* This task is created by default for all subprojects with this
8686
* setting and there is no point in running it if the files don't

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ class ClusterFormationTasks {
655655
String pid = node.pidFile.getText('UTF-8')
656656
ByteArrayOutputStream output = new ByteArrayOutputStream()
657657
project.exec {
658-
commandLine = ["${project.javaHome}/bin/jstack", pid]
658+
commandLine = ["${project.runtimeJavaHome}/bin/jstack", pid]
659659
standardOutput = output
660660
}
661661
output.toString('UTF-8').eachLine { line -> logger.error("| ${line}") }
@@ -699,7 +699,7 @@ class ClusterFormationTasks {
699699
}
700700

701701
private static File getJpsExecutableByName(Project project, String jpsExecutableName) {
702-
return Paths.get(project.javaHome.toString(), "bin/" + jpsExecutableName).toFile()
702+
return Paths.get(project.runtimeJavaHome.toString(), "bin/" + jpsExecutableName).toFile()
703703
}
704704

705705
/** Adds a task to kill an elasticsearch node with the given pidfile */

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/NodeInfo.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class NodeInfo {
162162
args.add("${esScript}")
163163
}
164164

165-
env = ['JAVA_HOME': project.javaHome]
165+
env = ['JAVA_HOME': project.runtimeJavaHome]
166166
args.addAll("-E", "node.portsfile=true")
167167
String collectedSystemProperties = config.systemProperties.collect { key, value -> "-D${key}=${value}" }.join(" ")
168168
String esJavaOpts = config.jvmArgs.isEmpty() ? collectedSystemProperties : collectedSystemProperties + " " + config.jvmArgs

plugins/discovery-azure-classic/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ task createKey(type: LoggedExec) {
6767
project.delete(keystore.parentFile)
6868
keystore.parentFile.mkdirs()
6969
}
70-
executable = new File(project.javaHome, 'bin/keytool')
70+
executable = new File(project.runtimeJavaHome, 'bin/keytool')
7171
standardInput = new ByteArrayInputStream('FirstName LastName\nUnit\nOrganization\nCity\nState\nNL\nyes\n\n'.getBytes('UTF-8'))
7272
args '-genkey',
7373
'-alias', 'test-node',

plugins/discovery-ec2/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ thirdPartyAudit.excludes = [
7575
'software.amazon.ion.system.IonSystemBuilder',
7676
'software.amazon.ion.system.IonTextWriterBuilder',
7777
'software.amazon.ion.system.IonWriterBuilder',
78+
'javax.xml.bind.DatatypeConverter',
79+
'javax.xml.bind.JAXBContext',
7880
'javax.servlet.ServletContextEvent',
7981
'javax.servlet.ServletContextListener',
8082
'org.apache.avalon.framework.logger.Logger',
8183
'org.apache.log.Hierarchy',
8284
'org.apache.log.Logger',
8385
]
84-
85-
if (JavaVersion.current() > JavaVersion.VERSION_1_8) {
86-
thirdPartyAudit.excludes += ['javax.xml.bind.DatatypeConverter']
87-
}

0 commit comments

Comments
 (0)