Fix java home validation usage by tasks (#49204)
Tasks intending to use a particular java home provided by JAVA<N>_HOME use the getJavaHome method, which verifies the given java home is available, or will be if the task will run. However, the verification logic was broken, in addition to unnecessarily delaying retrieving the java home until runtime. This commit fixes the verification logic to run at either config time, delaying verification, or at runtime which immediately checks if java home is available. closes #49153
This commit is contained in:
parent
14d2e795ae
commit
c6a8913c38
|
@ -216,41 +216,55 @@ class BuildPlugin implements Plugin<Project> {
|
|||
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<String> messages = []
|
||||
Map<Integer, List<Task>> requiredJavaVersions = (Map<Integer, List<Task>>) ext.get('requiredJavaVersions')
|
||||
for (Map.Entry<Integer, List<Task>> entry : requiredJavaVersions) {
|
||||
if (BuildParams.javaVersions.find { it.version == entry.key } != null) {
|
||||
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'))
|
||||
}
|
||||
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<Integer, List<Task>>).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<String> messages = []
|
||||
Map<Integer, List<Task>> requiredJavaVersions = (Map<Integer, List<Task>>) extraProperties.get('requiredJavaVersions')
|
||||
task.logger.warn(requiredJavaVersions.toString())
|
||||
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)
|
||||
return BuildParams.javaVersions.find { it.version == version }.javaHome.absolutePath
|
||||
JavaHome java = BuildParams.javaVersions.find { it.version == version }
|
||||
return java == null ? null : java.javaHome.absolutePath
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue