Skip to content

Commit 70cfedf

Browse files
authored
Refactor global build info plugin to leverage JavaInstallationRegistry (#54026)
This commit removes the configuration time vs execution time distinction with regards to certain BuildParms properties. Because of the cost of determining Java versions for configuration JDK locations we deferred this until execution time. This had two main downsides. First, we had to implement all this build logic in tasks, which required a bunch of additional plumbing and complexity. Second, because some information wasn't known during configuration time, we had to nest any build logic that depended on this in awkward callbacks. We now defer to the JavaInstallationRegistry recently added in Gradle. This utility uses a much more efficient method for probing Java installations vs our jrunscript implementation. This, combined with some optimizations to avoid probing the current JVM as well as deferring some evaluation via Providers when probing installations for BWC builds we can maintain effectively the same configuration time performance while removing a bunch of complexity and runtime cost (snapshotting inputs for the GenerateGlobalBuildInfoTask was very expensive). The end result should be a much more responsive build execution in almost all scenarios. (cherry picked from commit ecdbd37)
1 parent c97ee4e commit 70cfedf

File tree

34 files changed

+646
-1024
lines changed

34 files changed

+646
-1024
lines changed

CONTRIBUTING.md

-3
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,6 @@ be used to test against other JDKs as well, this is not only limited to JDK 8.
103103
> Note: It is also required to have `JAVA8_HOME`, `JAVA9_HOME`, `JAVA10_HOME`
104104
and `JAVA11_HOME`, and `JAVA12_HOME` available so that the tests can pass.
105105

106-
> Warning: do not use `sdkman` for Java installations which do not have proper
107-
`jrunscript` for jdk distributions.
108-
109106
Elasticsearch uses the Gradle wrapper for its build. You can execute Gradle
110107
using the wrapper via the `gradlew` script on Unix systems or `gradlew.bat`
111108
script on Windows in the root of the repository. The examples below show the

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import org.gradle.plugins.ide.eclipse.model.SourceFolder
3131
import org.gradle.util.DistributionLocator
3232
import org.gradle.util.GradleVersion
3333

34-
import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
34+
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure
3535

3636
plugins {
3737
id 'lifecycle-base'

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

+35-49
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@ import groovy.transform.CompileStatic
2626
import org.apache.commons.io.IOUtils
2727
import org.elasticsearch.gradle.info.BuildParams
2828
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin
29-
import org.elasticsearch.gradle.info.GlobalInfoExtension
3029
import org.elasticsearch.gradle.info.JavaHome
3130
import org.elasticsearch.gradle.precommit.DependencyLicensesTask
3231
import org.elasticsearch.gradle.precommit.PrecommitTasks
3332
import org.elasticsearch.gradle.test.ErrorReportingTestListener
3433
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster
3534
import org.elasticsearch.gradle.testclusters.TestClustersPlugin
36-
import org.gradle.api.Action
3735
import org.elasticsearch.gradle.testclusters.TestDistribution
38-
import org.elasticsearch.gradle.tool.Boilerplate
36+
import org.elasticsearch.gradle.util.GradleUtils
37+
import org.gradle.api.Action
3938
import org.gradle.api.GradleException
4039
import org.gradle.api.InvalidUserDataException
4140
import org.gradle.api.JavaVersion
@@ -84,7 +83,7 @@ import org.gradle.util.GradleVersion
8483
import java.nio.charset.StandardCharsets
8584
import java.nio.file.Files
8685

87-
import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
86+
import static org.elasticsearch.gradle.util.GradleUtils.maybeConfigure
8887

8988
/**
9089
* Encapsulates build configuration for elasticsearch projects.
@@ -146,7 +145,7 @@ class BuildPlugin implements Plugin<Project> {
146145
// Common config when running with a FIPS-140 runtime JVM
147146
if (inFipsJvm()) {
148147
// This configuration can be removed once system modules are available
149-
Boilerplate.maybeCreate(project.configurations, 'extraJars') {
148+
GradleUtils.maybeCreate(project.configurations, 'extraJars') {
150149
project.dependencies.add('extraJars', "org.bouncycastle:bc-fips:1.0.1")
151150
project.dependencies.add('extraJars', "org.bouncycastle:bctls-fips:1.0.9")
152151
}
@@ -156,7 +155,6 @@ class BuildPlugin implements Plugin<Project> {
156155
File securityPolicy = buildResources.copy("fips_java.policy")
157156
File security8Policy = buildResources.copy("fips_java8.policy")
158157
File bcfksKeystore = buildResources.copy("cacerts.bcfks")
159-
GlobalInfoExtension globalInfo = project.rootProject.extensions.getByType(GlobalInfoExtension)
160158
project.pluginManager.withPlugin("elasticsearch.testclusters") {
161159
NamedDomainObjectContainer<ElasticsearchCluster> testClusters = project.extensions.findByName(TestClustersPlugin.EXTENSION_NAME) as NamedDomainObjectContainer<ElasticsearchCluster>
162160
if (testClusters != null) {
@@ -165,14 +163,12 @@ class BuildPlugin implements Plugin<Project> {
165163
for (File dep : project.getConfigurations().getByName("extraJars").getFiles()) {
166164
cluster.extraJarFile(dep)
167165
}
168-
globalInfo.ready {
169-
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
170-
cluster.extraConfigFile("fips_java.security", securityProperties)
171-
cluster.extraConfigFile("fips_java.policy", securityPolicy)
172-
} else {
173-
cluster.extraConfigFile("fips_java.security", security8Properties)
174-
cluster.extraConfigFile("fips_java.policy", security8Policy)
175-
}
166+
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
167+
cluster.extraConfigFile("fips_java.security", securityProperties)
168+
cluster.extraConfigFile("fips_java.policy", securityPolicy)
169+
} else {
170+
cluster.extraConfigFile("fips_java.security", security8Properties)
171+
cluster.extraConfigFile("fips_java.policy", security8Policy)
176172
}
177173
cluster.extraConfigFile("cacerts.bcfks", bcfksKeystore)
178174
cluster.systemProperty('java.security.properties', '=${ES_PATH_CONF}/fips_java.security')
@@ -188,16 +184,14 @@ class BuildPlugin implements Plugin<Project> {
188184
}
189185
project.tasks.withType(Test).configureEach { Test task ->
190186
task.dependsOn(buildResources)
191-
globalInfo.ready {
192-
// Using the key==value format to override default JVM security settings and policy
193-
// see also: https://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html
194-
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
195-
task.systemProperty('java.security.properties', String.format(Locale.ROOT, "=%s", securityProperties.toString()))
196-
task.systemProperty('java.security.policy', String.format(Locale.ROOT, "=%s", securityPolicy.toString()))
197-
} else {
198-
task.systemProperty('java.security.properties', String.format(Locale.ROOT, "=%s", security8Properties.toString()))
199-
task.systemProperty('java.security.policy', String.format(Locale.ROOT, "=%s", security8Policy.toString()))
200-
}
187+
// Using the key==value format to override default JVM security settings and policy
188+
// see also: https://docs.oracle.com/javase/8/docs/technotes/guides/security/PolicyFiles.html
189+
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_1_8) {
190+
task.systemProperty('java.security.properties', String.format(Locale.ROOT, "=%s", securityProperties.toString()))
191+
task.systemProperty('java.security.policy', String.format(Locale.ROOT, "=%s", securityPolicy.toString()))
192+
} else {
193+
task.systemProperty('java.security.properties', String.format(Locale.ROOT, "=%s", security8Properties.toString()))
194+
task.systemProperty('java.security.policy', String.format(Locale.ROOT, "=%s", security8Policy.toString()))
201195
}
202196
task.systemProperty('javax.net.ssl.trustStorePassword', 'password')
203197
task.systemProperty('javax.net.ssl.keyStorePassword', 'password')
@@ -258,7 +252,7 @@ class BuildPlugin implements Plugin<Project> {
258252
static String getJavaHome(final Task task, final int version) {
259253
requireJavaHome(task, version)
260254
JavaHome java = BuildParams.javaVersions.find { it.version == version }
261-
return java == null ? null : java.javaHome.absolutePath
255+
return java == null ? null : java.javaHome.get().absolutePath
262256
}
263257

264258
/**
@@ -407,7 +401,7 @@ class BuildPlugin implements Plugin<Project> {
407401
dependencyNode.appendNode('groupId', dependency.group)
408402
dependencyNode.appendNode('artifactId', dependency.getDependencyProject().convention.getPlugin(BasePluginConvention).archivesBaseName)
409403
dependencyNode.appendNode('version', dependency.version)
410-
dependencyNode.appendNode('scope', 'runtime')
404+
dependencyNode.appendNode('scope', 'compile')
411405
}
412406
}
413407
}
@@ -426,13 +420,10 @@ class BuildPlugin implements Plugin<Project> {
426420
/** Adds compiler settings to the project */
427421
static void configureCompile(Project project) {
428422
ExtraPropertiesExtension ext = project.extensions.getByType(ExtraPropertiesExtension)
429-
GlobalInfoExtension globalBuildInfo = project.rootProject.extensions.getByType(GlobalInfoExtension)
430-
globalBuildInfo.ready {
431-
if (BuildParams.compilerJavaVersion < JavaVersion.VERSION_1_10) {
432-
ext.set('compactProfile', 'compact3')
433-
} else {
434-
ext.set('compactProfile', 'full')
435-
}
423+
if (BuildParams.compilerJavaVersion < JavaVersion.VERSION_1_10) {
424+
ext.set('compactProfile', 'compact3')
425+
} else {
426+
ext.set('compactProfile', 'full')
436427
}
437428
ext.set('compactProfile', 'full')
438429

@@ -450,12 +441,10 @@ class BuildPlugin implements Plugin<Project> {
450441
compileTask.options.forkOptions.javaHome = BuildParams.compilerJavaHome
451442
}
452443
if (targetCompatibilityVersion == JavaVersion.VERSION_1_8) {
453-
globalBuildInfo.ready {
454-
// compile with compact 3 profile by default
455-
// NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
456-
if (ext.get('compactProfile') != 'full') {
457-
compileTask.options.compilerArgs << '-profile' << ext.get('compactProfile').toString()
458-
}
444+
// compile with compact 3 profile by default
445+
// NOTE: this is just a compile time check: does not replace testing with a compact3 JRE
446+
if (ext.get('compactProfile') != 'full') {
447+
compileTask.options.compilerArgs << '-profile' << ext.get('compactProfile').toString()
459448
}
460449
}
461450
/*
@@ -657,16 +646,13 @@ class BuildPlugin implements Plugin<Project> {
657646
project.mkdir(heapdumpDir)
658647
project.mkdir(test.workingDir)
659648
project.mkdir(test.workingDir.toPath().resolve('temp'))
660-
661-
if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_1_9) {
662-
test.jvmArgs '--illegal-access=warn'
663-
}
664-
//TODO remove once jvm.options are added to test system properties
665-
if (BuildParams.runtimeJavaVersion == JavaVersion.VERSION_1_8) {
666-
test.systemProperty ('java.locale.providers','SPI,JRE')
667-
} else if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_1_9) {
668-
test.systemProperty ('java.locale.providers','SPI,COMPAT')
669-
}
649+
}
650+
//TODO remove once jvm.options are added to test system properties
651+
if (BuildParams.runtimeJavaVersion == JavaVersion.VERSION_1_8) {
652+
test.systemProperty ('java.locale.providers','SPI,JRE')
653+
} else if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_1_9) {
654+
test.systemProperty ('java.locale.providers','SPI,COMPAT')
655+
test.jvmArgs '--illegal-access=warn'
670656
}
671657
if (inFipsJvm()) {
672658
project.dependencies.add('testRuntimeOnly', "org.bouncycastle:bc-fips:1.0.1")

buildSrc/src/main/groovy/org/elasticsearch/gradle/precommit/PrecommitTasks.groovy

+7-10
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,13 @@ class PrecommitTasks {
143143
ExportElasticsearchBuildResourcesTask buildResources = project.tasks.getByName('buildResources')
144144
project.tasks.withType(CheckForbiddenApis).configureEach {
145145
dependsOn(buildResources)
146-
doFirst {
147-
// we need to defer this configuration since we don't know the runtime java version until execution time
148-
targetCompatibility = BuildParams.runtimeJavaVersion.majorVersion
149-
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_13) {
150-
project.logger.warn(
151-
"Forbidden APIs does not support Java versions past 13. Will use the signatures from 13 for {}.",
152-
BuildParams.runtimeJavaVersion
153-
)
154-
targetCompatibility = JavaVersion.VERSION_13.majorVersion
155-
}
146+
targetCompatibility = BuildParams.runtimeJavaVersion.majorVersion
147+
if (BuildParams.runtimeJavaVersion > JavaVersion.VERSION_13) {
148+
project.logger.warn(
149+
"Forbidden APIs does not support Java versions past 13. Will use the signatures from 13 for {}.",
150+
BuildParams.runtimeJavaVersion
151+
)
152+
targetCompatibility = JavaVersion.VERSION_13.majorVersion
156153
}
157154
bundledSignatures = [
158155
"jdk-unsafe", "jdk-deprecated", "jdk-non-portable", "jdk-system-out"

buildSrc/src/main/java/org/elasticsearch/gradle/DistributionDownloadPlugin.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.elasticsearch.gradle.docker.DockerSupportService;
2727
import org.elasticsearch.gradle.info.BuildParams;
2828
import org.elasticsearch.gradle.info.GlobalBuildInfoPlugin;
29-
import org.elasticsearch.gradle.tool.Boilerplate;
29+
import org.elasticsearch.gradle.util.GradleUtils;
3030
import org.gradle.api.GradleException;
3131
import org.gradle.api.NamedDomainObjectContainer;
3232
import org.gradle.api.Plugin;
@@ -53,7 +53,7 @@
5353
import java.util.concurrent.Callable;
5454
import java.util.function.Supplier;
5555

56-
import static org.elasticsearch.gradle.Util.capitalize;
56+
import static org.elasticsearch.gradle.util.Util.capitalize;
5757

5858
/**
5959
* A plugin to manage getting and extracting distributions of Elasticsearch.
@@ -78,7 +78,7 @@ public void apply(Project project) {
7878
project.getRootProject().getPluginManager().apply(GlobalBuildInfoPlugin.class);
7979
project.getRootProject().getPluginManager().apply(DockerSupportPlugin.class);
8080

81-
Provider<DockerSupportService> dockerSupport = Boilerplate.getBuildService(
81+
Provider<DockerSupportService> dockerSupport = GradleUtils.getBuildService(
8282
project.getGradle().getSharedServices(),
8383
DockerSupportPlugin.DOCKER_SUPPORT_SERVICE_NAME
8484
);

buildSrc/src/main/java/org/elasticsearch/gradle/JdkDownloadPlugin.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
import java.util.concurrent.Callable;
4848
import java.util.stream.StreamSupport;
4949

50-
import static org.elasticsearch.gradle.tool.Boilerplate.findByName;
51-
import static org.elasticsearch.gradle.tool.Boilerplate.maybeCreate;
50+
import static org.elasticsearch.gradle.util.GradleUtils.findByName;
51+
import static org.elasticsearch.gradle.util.GradleUtils.maybeCreate;
5252

5353
public class JdkDownloadPlugin implements Plugin<Project> {
5454

0 commit comments

Comments
 (0)