Skip to content

Commit cb990b9

Browse files
committed
Lazy configure build tasks that require older JDKs (#29519)
Some build tasks require older JDKs. For example, the BWC build tasks for older versions of Elasticsearch require older JDKs. It is onerous to require these be configured when merely compiling Elasticsearch, the requirement that they be strictly set to appropriate values should only be enforced if these tasks are going to be executed. To address this, we lazy configure these tasks.
1 parent e7c310f commit cb990b9

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@ class BuildPlugin implements Plugin<Project> {
221221
return System.getenv('JAVA' + version + '_HOME')
222222
}
223223

224+
/**
225+
* Get Java home for the project for the specified version. If the specified version is not configured, an exception with the specified
226+
* message is thrown.
227+
*
228+
* @param project the project
229+
* @param version the version of Java home to obtain
230+
* @param message the exception message if Java home for the specified version is not configured
231+
* @return Java home for the specified version
232+
* @throws GradleException if Java home for the specified version is not configured
233+
*/
234+
static String getJavaHome(final Project project, final int version, final String message) {
235+
if (project.javaVersions.get(version) == null) {
236+
throw new GradleException(message)
237+
}
238+
return project.javaVersions.get(version)
239+
}
240+
224241
private static String findRuntimeJavaHome(final String compilerJavaHome) {
225242
assert compilerJavaHome != null
226243
return System.getenv('RUNTIME_JAVA_HOME') ?: compilerJavaHome

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
1920
package org.elasticsearch.gradle.test
2021

2122
import com.sun.jna.Native
2223
import com.sun.jna.WString
2324
import org.apache.tools.ant.taskdefs.condition.Os
2425
import org.elasticsearch.gradle.Version
25-
import org.gradle.api.GradleException
2626
import org.gradle.api.InvalidUserDataException
27-
import org.gradle.api.JavaVersion
2827
import org.gradle.api.Project
2928

3029
import java.nio.file.Files
3130
import java.nio.file.Path
3231
import java.nio.file.Paths
3332

33+
import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
34+
3435
/**
3536
* A container for the files and configuration associated with a single node in a test cluster.
3637
*/
@@ -165,24 +166,14 @@ class NodeInfo {
165166
}
166167

167168
final String javaHome
168-
final Map<Integer, JavaVersion> javaVersions = project.javaVersions
169169
if (nodeVersion.before("6.2.0")) {
170-
final String java8Home = javaVersions.get(8)
171-
if (java8Home == null) {
172-
throw new GradleException("JAVA8_HOME must be set to run BWC tests against [" + nodeVersion + "]")
173-
}
174-
javaHome = java8Home
170+
env = ['JAVA_HOME':"${-> getJavaHome(project, 8, "JAVA8_HOME must be set to run BWC tests against [" + nodeVersion + "]")}"]
175171
} else if (nodeVersion.onOrAfter("6.2.0") && nodeVersion.before("6.3.0")) {
176-
final String java9Home = javaVersions.get(9)
177-
if (java9Home == null) {
178-
throw new GradleException("JAVA9_HOME must be set to run BWC tests against [" + nodeVersion + "]")
179-
}
180-
javaHome = java9Home
172+
env = ['JAVA_HOME':"${-> getJavaHome(project, 9, "JAVA9_HOME must be set to run BWC tests against [" + nodeVersion + "]")}"]
181173
} else {
182-
javaHome = project.compilerJavaHome
174+
env = ['JAVA_HOME':project.runtimeJavaHome]
183175
}
184176

185-
env = ['JAVA_HOME':javaHome]
186177
args.addAll("-E", "node.portsfile=true")
187178
String collectedSystemProperties = config.systemProperties.collect { key, value -> "-D${key}=${value}" }.join(" ")
188179
String esJavaOpts = config.jvmArgs.isEmpty() ? collectedSystemProperties : collectedSystemProperties + " " + config.jvmArgs

distribution/bwc/build.gradle

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
* under the License.
1818
*/
1919

20+
2021
import org.apache.tools.ant.taskdefs.condition.Os
2122
import org.elasticsearch.gradle.LoggedExec
2223
import org.elasticsearch.gradle.Version
2324

24-
import java.util.regex.Matcher
25+
import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
2526

2627
/**
2728
* This is a dummy project which does a local checkout of the previous
@@ -146,15 +147,9 @@ subprojects {
146147
workingDir = checkoutDir
147148
if (["5.6", "6.0", "6.1"].contains(bwcBranch)) {
148149
// we are building branches that are officially built with JDK 8, push JAVA8_HOME to JAVA_HOME for these builds
149-
if (project.javaVersions.get(8) == null) {
150-
throw new GradleException("JAVA8_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")
151-
}
152-
environment('JAVA_HOME', project.javaVersions.get(8))
150+
environment('JAVA_HOME', "${-> getJavaHome(project, 8, "JAVA8_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")}")
153151
} else if ("6.2".equals(bwcBranch)) {
154-
if (project.javaVersions.get(9) == null) {
155-
throw new GradleException("JAVA9_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")
156-
}
157-
environment('JAVA_HOME', project.javaVersions.get(9))
152+
environment('JAVA_HOME', "${-> getJavaHome(project, 9, "JAVA9_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")}")
158153
} else {
159154
environment('JAVA_HOME', project.compilerJavaHome)
160155
}

qa/reindex-from-old/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ should be able to use the standard launching mechanism which
2424
is more flexible and reliable.
2525
"""
2626

27+
2728
import org.apache.tools.ant.taskdefs.condition.Os
2829

30+
import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
31+
2932
apply plugin: 'elasticsearch.standalone-rest-test'
3033
apply plugin: 'elasticsearch.rest-test'
3134

@@ -55,9 +58,6 @@ if (Os.isFamily(Os.FAMILY_WINDOWS)) {
5558
// we can't get the pid files in windows so we skip that
5659
integTest.enabled = false
5760
} else {
58-
if (project.javaVersions.get(7) == null) {
59-
throw new GradleException("JAVA7_HOME must be set to run reindex-from-old")
60-
}
6161
/* Set up tasks to unzip and run the old versions of ES before running the
6262
* integration tests. */
6363
for (String version : ['2', '1', '090']) {
@@ -77,7 +77,7 @@ if (Os.isFamily(Os.FAMILY_WINDOWS)) {
7777
dependsOn unzip
7878
executable = new File(project.runtimeJavaHome, 'bin/java')
7979
env 'CLASSPATH', "${ -> project.configurations.oldesFixture.asPath }"
80-
env 'JAVA_HOME', project.javaVersions.get(7)
80+
env 'JAVA_HOME', "${-> getJavaHome(project, 7, "JAVA7_HOME must be set to run reindex-from-old")}"
8181
args 'oldes.OldElasticsearch',
8282
baseDir,
8383
unzip.temporaryDir,

0 commit comments

Comments
 (0)