Simplify java home verification (#55635)

* Simplify java home verification

At one time, all uses of java home were found through the getJavaHome
utility method on BuildPlugin. However, that was changed many
refactorings ago, but the complex support for registering a java home
version needed that fails at configuration time still exists. The only
remaining use of grabbing java home is within bwc tests, and must be at
runtime since that is when we have the checkout and know what version is
needed.

This commit consolidates the java home finding method into a utility
unassociated with BuildPlugin.

* fix checkstyle

* address feedback
This commit is contained in:
Ryan Ernst 2020-04-27 12:42:50 -07:00 committed by Ryan Ernst
parent 6ba5148ead
commit 70b499b7aa
No known key found for this signature in database
GPG Key ID: 5F7EA39E15F54DCE
4 changed files with 40 additions and 59 deletions

View File

@ -199,60 +199,6 @@ class BuildPlugin implements Plugin<Project> {
}
}
/** 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) {
// use root project for global accounting
Project rootProject = task.project.rootProject
ExtraPropertiesExtension extraProperties = rootProject.extensions.extraProperties
// 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 {
// 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<String> messages = []
Map<Integer, List<Task>> requiredJavaVersions = (Map<Integer, List<Task>>) extraProperties.get('requiredJavaVersions')
for (Map.Entry<Integer, List<Task>> entry : requiredJavaVersions) {
if (BuildParams.javaVersions.any { it.version == entry.key }) {
continue
}
List<String> 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<Integer, List<Task>> requiredJavaVersions = (Map<Integer, List<Task>>) 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)
JavaHome java = BuildParams.javaVersions.find { it.version == version }
return java == null ? null : java.javaHome.get().absolutePath
}
/**
* Makes dependencies non-transitive.
*

View File

@ -0,0 +1,37 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.gradle.util;
import org.elasticsearch.gradle.info.BuildParams;
import org.elasticsearch.gradle.info.JavaHome;
import org.gradle.api.GradleException;
import java.util.List;
import java.util.Optional;
public class JavaUtil {
/** 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 int version) {
List<JavaHome> javaHomes = BuildParams.getJavaVersions();
Optional<JavaHome> java = javaHomes.stream().filter(j -> j.getVersion() == version).findFirst();
return java.orElseThrow(() -> new GradleException("JAVA" + version + "_HOME required")).getJavaHome().get().getAbsolutePath();
}
}

View File

@ -27,7 +27,7 @@ import org.gradle.util.GradleVersion
import java.nio.charset.StandardCharsets
import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
import static org.elasticsearch.gradle.util.JavaUtil.getJavaHome
/**
* We want to be able to do BWC tests for unreleased versions without relying on and waiting for snapshots.
@ -166,7 +166,7 @@ bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInfo unreleased
List<String> lines = file("${checkoutDir}/.ci/java-versions.properties").readLines()
environment(
'JAVA_HOME',
getJavaHome(it, Integer.parseInt(
getJavaHome(Integer.parseInt(
lines
.findAll({ it.startsWith("ES_BUILD_JAVA=") })
.collect({ it.replace("ES_BUILD_JAVA=java", "").trim() })
@ -176,7 +176,7 @@ bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInfo unreleased
)
environment(
'RUNTIME_JAVA_HOME',
getJavaHome(it, Integer.parseInt(
getJavaHome(Integer.parseInt(
lines
.findAll({ it.startsWith("ES_RUNTIME_JAVA=java") })
.collect({ it.replace("ES_RUNTIME_JAVA=java", "").trim() })

View File

@ -22,8 +22,6 @@ import org.elasticsearch.gradle.Architecture
import org.elasticsearch.gradle.OS
import org.elasticsearch.gradle.info.BuildParams
import static org.elasticsearch.gradle.BuildPlugin.getJavaHome
apply plugin: 'elasticsearch.test-with-dependencies'
apply plugin: 'elasticsearch.jdk-download'