Skip to content

Commit

Permalink
Build: Move java home checks to pre-execution phase (#29548)
Browse files Browse the repository at this point in the history
This commit moves the checks on JAVAX_HOME (where X is the java version
number) existing to the end of gradle's configuration phase, and based
on whether the tasks needing the java home are configured to execute.

relates #29519
  • Loading branch information
rjernst committed Apr 19, 2018
1 parent ee403aa commit e8d8142
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.dsl.RepositoryHandler
import org.gradle.api.execution.TaskExecutionGraph
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
Expand Down Expand Up @@ -221,21 +222,34 @@ class BuildPlugin implements Plugin<Project> {
return System.getenv('JAVA' + version + '_HOME')
}

/**
* Get Java home for the project for the specified version. If the specified version is not configured, an exception with the specified
* message is thrown.
*
* @param project the project
* @param version the version of Java home to obtain
* @param message the exception message if Java home for the specified version is not configured
* @return Java home for the specified version
* @throws GradleException if Java home for the specified version is not configured
*/
static String getJavaHome(final Project project, final int version, final String message) {
if (project.javaVersions.get(version) == null) {
throw new GradleException(message)
/** Add a check before gradle execution phase which ensures java home for the given java version is set. */
static void requireJavaHome(Task task, int version) {
Project rootProject = task.project.rootProject // use root project for global accounting
if (rootProject.hasProperty('requiredJavaVersions') == false) {
rootProject.rootProject.ext.requiredJavaVersions = [:].withDefault{key -> return []}
rootProject.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph ->
List<String> messages = []
for (entry in rootProject.requiredJavaVersions) {
if (rootProject.javaVersions.get(entry.key) != null) {
continue
}
List<String> tasks = entry.value.findAll { taskGraph.hasTask(it) }.collect { " ${it.path}" }
if (tasks.isEmpty() == false) {
messages.add("JAVA${entry.key}_HOME required to run tasks:\n${tasks.join('\n')}")
}
}
if (messages.isEmpty() == false) {
throw new GradleException(messages.join('\n'))
}
}
}
return project.javaVersions.get(version)
rootProject.requiredJavaVersions.get(version).add(task)
}

/** A convenience method for getting java home for a version of java and requiring that version for the given task to execute */
static String getJavaHome(final Task task, final int version) {
requireJavaHome(task, version)
return task.project.javaVersions.get(version)
}

private static String findRuntimeJavaHome(final String compilerJavaHome) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package org.elasticsearch.gradle.test

import org.apache.tools.ant.DefaultLogger
import org.apache.tools.ant.taskdefs.condition.Os
import org.elasticsearch.gradle.BuildPlugin
import org.elasticsearch.gradle.LoggedExec
import org.elasticsearch.gradle.Version
import org.elasticsearch.gradle.VersionProperties
Expand Down Expand Up @@ -611,6 +612,9 @@ class ClusterFormationTasks {
}

Task start = project.tasks.create(name: name, type: DefaultTask, dependsOn: setup)
if (node.javaVersion != null) {
BuildPlugin.requireJavaHome(start, node.javaVersion)
}
start.doLast(elasticsearchRunner)
return start
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
* A container for the files and configuration associated with a single node in a test cluster.
*/
class NodeInfo {
/** Gradle project this node is part of */
Project project

/** common configuration for all nodes, including this one */
ClusterConfiguration config

Expand Down Expand Up @@ -84,6 +87,9 @@ class NodeInfo {
/** directory to install plugins from */
File pluginsTmpDir

/** Major version of java this node runs with, or {@code null} if using the runtime java version */
Integer javaVersion

/** environment variables to start the node with */
Map<String, String> env

Expand All @@ -109,6 +115,7 @@ class NodeInfo {
NodeInfo(ClusterConfiguration config, int nodeNum, Project project, String prefix, Version nodeVersion, File sharedDir) {
this.config = config
this.nodeNum = nodeNum
this.project = project
this.sharedDir = sharedDir
if (config.clusterName != null) {
clusterName = config.clusterName
Expand Down Expand Up @@ -165,12 +172,11 @@ class NodeInfo {
args.add("${esScript}")
}


if (nodeVersion.before("6.2.0")) {
env = ['JAVA_HOME': "${-> getJavaHome(project, 8, "JAVA8_HOME must be set to run BWC tests against [" + nodeVersion + "]")}"]
javaVersion = 8
} else if (nodeVersion.onOrAfter("6.2.0") && nodeVersion.before("6.3.0")) {
env = ['JAVA_HOME': "${-> getJavaHome(project, 9, "JAVA9_HOME must be set to run BWC tests against [" + nodeVersion + "]")}"]
} else {
env = ['JAVA_HOME': (String) project.runtimeJavaHome]
javaVersion = 9
}

args.addAll("-E", "node.portsfile=true")
Expand All @@ -182,7 +188,7 @@ class NodeInfo {
// in the cluster-specific options
esJavaOpts = String.join(" ", "-ea", "-esa", esJavaOpts)
}
env.put('ES_JAVA_OPTS', esJavaOpts)
env = ['ES_JAVA_OPTS': esJavaOpts]
for (Map.Entry<String, String> property : System.properties.entrySet()) {
if (property.key.startsWith('tests.es.')) {
args.add("-E")
Expand Down Expand Up @@ -253,13 +259,19 @@ class NodeInfo {
return Native.toString(shortPath).substring(4)
}

/** Return the java home used by this node. */
String getJavaHome() {
return javaVersion == null ? project.runtimeJavaHome : project.javaVersions.get(javaVersion)
}

/** Returns debug string for the command that started this node. */
String getCommandString() {
String esCommandString = "\nNode ${nodeNum} configuration:\n"
esCommandString += "|-----------------------------------------\n"
esCommandString += "| cwd: ${cwd}\n"
esCommandString += "| command: ${executable} ${args.join(' ')}\n"
esCommandString += '| environment:\n'
esCommandString += "| JAVA_HOME: ${javaHome}\n"
env.each { k, v -> esCommandString += "| ${k}: ${v}\n" }
if (config.daemonize) {
esCommandString += "|\n| [${wrapperScript.name}]\n"
Expand Down
4 changes: 2 additions & 2 deletions distribution/bwc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ subprojects {
workingDir = checkoutDir
if (["5.6", "6.0", "6.1"].contains(bwcBranch)) {
// we are building branches that are officially built with JDK 8, push JAVA8_HOME to JAVA_HOME for these builds
environment('JAVA_HOME', "${-> getJavaHome(project, 8, "JAVA8_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")}")
environment('JAVA_HOME', getJavaHome(it, 8))
} else if ("6.2".equals(bwcBranch)) {
environment('JAVA_HOME', "${-> getJavaHome(project, 9, "JAVA9_HOME is required to build BWC versions for BWC branch [" + bwcBranch + "]")}")
environment('JAVA_HOME', getJavaHome(it, 9))
} else {
environment('JAVA_HOME', project.compilerJavaHome)
}
Expand Down
2 changes: 1 addition & 1 deletion qa/reindex-from-old/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ if (Os.isFamily(Os.FAMILY_WINDOWS)) {
dependsOn unzip
executable = new File(project.runtimeJavaHome, 'bin/java')
env 'CLASSPATH', "${ -> project.configurations.oldesFixture.asPath }"
env 'JAVA_HOME', "${-> getJavaHome(project, 7, "JAVA7_HOME must be set to run reindex-from-old")}"
env 'JAVA_HOME', getJavaHome(it, 7)
args 'oldes.OldElasticsearch',
baseDir,
unzip.temporaryDir,
Expand Down

0 comments on commit e8d8142

Please sign in to comment.