diff --git a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy index a43d60b9983..09a3d5dd7fa 100644 --- a/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy +++ b/buildSrc/src/main/groovy/org/elasticsearch/gradle/BuildPlugin.groovy @@ -216,41 +216,55 @@ class BuildPlugin implements Plugin { static void requireJavaHome(Task task, int version) { // use root project for global accounting Project rootProject = task.project.rootProject - ExtraPropertiesExtension ext = rootProject.extensions.getByType(ExtraPropertiesExtension) + ExtraPropertiesExtension extraProperties = rootProject.extensions.extraProperties - if (rootProject.hasProperty('requiredJavaVersions') == false) { - ext.set('requiredJavaVersions', [:]) - rootProject.gradle.taskGraph.whenReady({ TaskExecutionGraph taskGraph -> - List messages = [] - Map> requiredJavaVersions = (Map>) ext.get('requiredJavaVersions') - for (Map.Entry> entry : requiredJavaVersions) { - if (BuildParams.javaVersions.find { it.version == entry.key } != null) { - continue - } - List tasks = entry.value.findAll { taskGraph.hasTask(it) }.collect { " ${it.path}".toString() } - if (tasks.isEmpty() == false) { - messages.add("JAVA${entry.key}_HOME required to run tasks:\n${tasks.join('\n')}".toString()) - } - } - if (messages.isEmpty() == false) { - throw new GradleException(messages.join('\n')) - } - ext.set('requiredJavaVersions', null) // reset to null to indicate the pre-execution checks have executed - }) - } else if (ext.has('requiredJavaVersions') == false || ext.get('requiredJavaVersions') == null) { + // hacky way (but the only way) to find if the task graph has already been populated + boolean taskGraphReady + try { + rootProject.gradle.taskGraph.getAllTasks() + taskGraphReady = true + } catch (IllegalStateException) { + taskGraphReady = false + } + + if (taskGraphReady) { // check directly if the version is present since we are already executing if (BuildParams.javaVersions.find { it.version == version } == null) { throw new GradleException("JAVA${version}_HOME required to run task:\n${task}") } } else { - (ext.get('requiredJavaVersions') as Map>).getOrDefault(version, []).add(task) + // setup list of java versions we will check at the end of configuration time + if (extraProperties.has('requiredJavaVersions') == false) { + extraProperties.set('requiredJavaVersions', [:]) + rootProject.gradle.taskGraph.whenReady { TaskExecutionGraph taskGraph -> + List messages = [] + Map> requiredJavaVersions = (Map>) extraProperties.get('requiredJavaVersions') + task.logger.warn(requiredJavaVersions.toString()) + for (Map.Entry> entry : requiredJavaVersions) { + if (BuildParams.javaVersions.any { it.version == entry.key }) { + continue + } + List tasks = entry.value.findAll { taskGraph.hasTask(it) }.collect { " ${it.path}".toString() } + if (tasks.isEmpty() == false) { + messages.add("JAVA${entry.key}_HOME required to run tasks:\n${tasks.join('\n')}".toString()) + } + } + if (messages.isEmpty() == false) { + throw new GradleException(messages.join('\n')) + } + } + } + Map> requiredJavaVersions = (Map>) extraProperties.get('requiredJavaVersions') + requiredJavaVersions.putIfAbsent(version, []) + 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 BuildParams.javaVersions.find { it.version == version }.javaHome.absolutePath + JavaHome java = BuildParams.javaVersions.find { it.version == version } + return java == null ? null : java.javaHome.absolutePath } /** diff --git a/modules/reindex/build.gradle b/modules/reindex/build.gradle index 8ff83698e8d..2f952b431ce 100644 --- a/modules/reindex/build.gradle +++ b/modules/reindex/build.gradle @@ -118,7 +118,7 @@ if (Os.isFamily(Os.FAMILY_WINDOWS)) { dependsOn unzip executable = "${BuildParams.runtimeJavaHome}/bin/java" env 'CLASSPATH', "${-> project.configurations.oldesFixture.asPath}" - env 'JAVA_HOME', "${-> getJavaHome(it, 8)}" + env 'JAVA_HOME', getJavaHome(it, 8) args 'oldes.OldElasticsearch', baseDir, unzip.temporaryDir,