Skip to content

Commit c181635

Browse files
authored
[Backport] Improve build configuration time (#42674)
1 parent d14799f commit c181635

File tree

39 files changed

+1335
-949
lines changed

39 files changed

+1335
-949
lines changed

build.gradle

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.gradle.plugins.ide.eclipse.model.SourceFolder
3131
plugins {
3232
id 'com.gradle.build-scan' version '2.2.1'
3333
id 'base'
34+
id 'elasticsearch.global-build-info'
3435
}
3536
if (properties.get("org.elasticsearch.acceptScanTOS", "false") == "true") {
3637
buildScan {
@@ -263,7 +264,7 @@ allprojects {
263264
}
264265

265266
project.afterEvaluate {
266-
configurations.all {
267+
configurations.matching { it.canBeResolved }.all {
267268
resolutionStrategy.dependencySubstitution { DependencySubstitutions subs ->
268269
projectSubstitutions.each { k,v ->
269270
subs.substitute(subs.module(k)).with(subs.project(v))
@@ -337,7 +338,7 @@ gradle.projectsEvaluated {
337338
if (tasks.findByPath('test') != null && tasks.findByPath('integTest') != null) {
338339
integTest.mustRunAfter test
339340
}
340-
configurations.all { Configuration configuration ->
341+
configurations.matching { it.canBeResolved }.all { Configuration configuration ->
341342
dependencies.all { Dependency dep ->
342343
Project upstreamProject = dependencyToProject(dep)
343344
if (upstreamProject != null) {
@@ -593,7 +594,3 @@ allprojects {
593594
}
594595
}
595596
}
596-
597-
598-
599-

buildSrc/build.gradle

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -65,39 +65,10 @@ processResources {
6565
if (JavaVersion.current() < JavaVersion.VERSION_11) {
6666
throw new GradleException('At least Java 11 is required to build elasticsearch gradle tools')
6767
}
68-
// Gradle 4.10 does not support setting this to 11 yet
69-
targetCompatibility = "10"
70-
sourceCompatibility = "10"
71-
72-
// We have a few classes that need to be compiled for older java versions because these are used to run checks against
73-
// those
74-
sourceSets {
75-
minimumRuntime {
76-
// We only want Java here, but the Groovy doesn't configure javadoc correctly if we don't define this as groovy
77-
groovy {
78-
srcDirs = ['src/main/minimumRuntime']
79-
}
80-
}
81-
}
82-
compileMinimumRuntimeGroovy {
83-
// We can't use BuildPlugin here, so read from file
84-
String minimumRuntimeVersion = file('src/main/resources/minimumRuntimeVersion').text.trim()
85-
targetCompatibility = minimumRuntimeVersion
86-
sourceCompatibility = minimumRuntimeVersion
87-
}
88-
dependencies {
89-
if (project.ext.has("isEclipse") == false || project.ext.isEclipse == false) {
90-
// eclipse is confused if this is set explicitly
91-
compile sourceSets.minimumRuntime.output
92-
}
93-
minimumRuntimeCompile "junit:junit:${props.getProperty('junit')}"
94-
minimumRuntimeCompile localGroovy()
95-
minimumRuntimeCompile gradleApi()
96-
}
97-
jar {
98-
from sourceSets.minimumRuntime.output
99-
}
10068

69+
// Keep compatibility with Java 8 for external users of build-tools that haven't migrated to Java 11
70+
targetCompatibility = '8'
71+
sourceCompatibility = '8'
10172

10273
/*****************************************************************************
10374
* Dependencies used by the entire build *
@@ -117,7 +88,7 @@ dependencies {
11788
compile 'com.perforce:p4java:2012.3.551082' // THIS IS SUPPOSED TO BE OPTIONAL IN THE FUTURE....
11889
compile 'org.apache.rat:apache-rat:0.11'
11990
compile "org.elasticsearch:jna:4.5.1"
120-
compile 'com.github.jengelman.gradle.plugins:shadow:2.0.4'
91+
compile 'com.github.jengelman.gradle.plugins:shadow:4.0.3'
12192
compile 'de.thetaphi:forbiddenapis:2.6'
12293
compile 'com.avast.gradle:gradle-docker-compose-plugin:0.8.12'
12394
testCompile "junit:junit:${props.getProperty('junit')}"
@@ -162,7 +133,6 @@ if (project != rootProject) {
162133
dependenciesInfo.enabled = false
163134
forbiddenApisMain.enabled = false
164135
forbiddenApisTest.enabled = false
165-
forbiddenApisMinimumRuntime.enabled = false
166136
jarHell.enabled = false
167137
thirdPartyAudit.enabled = false
168138

@@ -184,16 +154,7 @@ if (project != rootProject) {
184154
from configurations.distribution
185155
into localDownloads
186156
}
187-
188-
test {
189-
// The test task is configured to runtimeJava version, but build-tools doesn't support all of them, so test
190-
// with compiler instead on the ones that are too old.
191-
if (project.runtimeJavaVersion <= JavaVersion.VERSION_1_10) {
192-
executable = "${project.compilerJavaHome}/bin/java"
193-
}
194-
}
195-
196-
// This can't be an RandomizedTestingTask because we can't yet reference it
157+
197158
task integTest(type: Test) {
198159
// integration test requires the local testing repo for example plugin builds
199160
dependsOn project.rootProject.allprojects.collect {

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

Lines changed: 285 additions & 472 deletions
Large diffs are not rendered by default.

buildSrc/src/main/groovy/org/elasticsearch/gradle/plugin/PluginBuildPlugin.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import org.elasticsearch.gradle.test.RestIntegTestTask
2828
import org.elasticsearch.gradle.test.RunTask
2929
import org.elasticsearch.gradle.testclusters.TestClustersPlugin
3030
import org.gradle.api.InvalidUserDataException
31+
import org.gradle.api.Plugin
3132
import org.gradle.api.Project
3233
import org.gradle.api.Task
3334
import org.gradle.api.publish.maven.MavenPublication
@@ -43,13 +44,13 @@ import java.util.regex.Pattern
4344
/**
4445
* Encapsulates build configuration for an Elasticsearch plugin.
4546
*/
46-
class PluginBuildPlugin extends BuildPlugin {
47+
class PluginBuildPlugin implements Plugin<Project> {
4748

4849
public static final String PLUGIN_EXTENSION_NAME = 'esplugin'
4950

5051
@Override
5152
void apply(Project project) {
52-
super.apply(project)
53+
project.pluginManager.apply(BuildPlugin)
5354

5455
PluginPropertiesExtension extension = project.extensions.create(PLUGIN_EXTENSION_NAME, PluginPropertiesExtension, project)
5556
configureDependencies(project)

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,30 @@ class PrecommitTasks {
116116
}
117117

118118
private static Task configureThirdPartyAudit(Project project) {
119-
ThirdPartyAuditTask thirdPartyAuditTask = project.tasks.create('thirdPartyAudit', ThirdPartyAuditTask.class)
120119
ExportElasticsearchBuildResourcesTask buildResources = project.tasks.getByName('buildResources')
121-
thirdPartyAuditTask.configure {
122-
dependsOn(buildResources)
123-
signatureFile = buildResources.copy("forbidden/third-party-audit.txt")
124-
javaHome = project.runtimeJavaHome
125-
targetCompatibility = project.runtimeJavaVersion
120+
return project.tasks.create('thirdPartyAudit', ThirdPartyAuditTask.class) { task ->
121+
task.dependsOn(buildResources)
122+
task.signatureFile = buildResources.copy("forbidden/third-party-audit.txt")
123+
task.javaHome = project.runtimeJavaHome
124+
task.targetCompatibility.set(project.provider({ project.runtimeJavaVersion }))
126125
}
127-
return thirdPartyAuditTask
128126
}
129127

130128
private static Task configureForbiddenApisCli(Project project) {
131129
project.pluginManager.apply(ForbiddenApisPlugin)
132130
ExportElasticsearchBuildResourcesTask buildResources = project.tasks.getByName('buildResources')
133131
project.tasks.withType(CheckForbiddenApis) {
134132
dependsOn(buildResources)
135-
targetCompatibility = project.runtimeJavaVersion >= JavaVersion.VERSION_1_9 ?
136-
project.runtimeJavaVersion.getMajorVersion() : project.runtimeJavaVersion
137-
if (project.runtimeJavaVersion > JavaVersion.VERSION_11) {
138-
doLast {
133+
doFirst {
134+
// we need to defer this configuration since we don't know the runtime java version until execution time
135+
targetCompatibility = project.runtimeJavaVersion.getMajorVersion()
136+
if (project.runtimeJavaVersion > JavaVersion.VERSION_11) {
139137
project.logger.info(
140138
"Forbidden APIs does not support java version past 11. Will use the signatures from 11 for ",
141139
project.runtimeJavaVersion
142140
)
141+
targetCompatibility = JavaVersion.VERSION_11.getMajorVersion()
143142
}
144-
targetCompatibility = JavaVersion.VERSION_11.getMajorVersion()
145143
}
146144
bundledSignatures = [
147145
"jdk-unsafe", "jdk-deprecated", "jdk-non-portable", "jdk-system-out"

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/ClusterFormationTasks.groovy

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,6 @@ class ClusterFormationTasks {
317317
// its run after plugins have been installed, as the extra config files may belong to plugins
318318
setup = configureExtraConfigFilesTask(taskName(prefix, node, 'extraConfig'), project, setup, node)
319319

320-
// If the node runs in a FIPS 140-2 JVM, the BCFKS default keystore will be password protected
321-
if (project.inFipsJvm){
322-
node.config.systemProperties.put('javax.net.ssl.trustStorePassword', 'password')
323-
node.config.systemProperties.put('javax.net.ssl.keyStorePassword', 'password')
324-
}
325-
326320
// extra setup commands
327321
for (Map.Entry<String, Object[]> command : node.config.setupCommands.entrySet()) {
328322
// the first argument is the actual script name, relative to home
@@ -430,16 +424,17 @@ class ClusterFormationTasks {
430424
if (node.nodeVersion.major >= 7) {
431425
esConfig['indices.breaker.total.use_real_memory'] = false
432426
}
433-
for (Map.Entry<String, Object> setting : node.config.settings) {
434-
if (setting.value == null) {
435-
esConfig.remove(setting.key)
436-
} else {
437-
esConfig.put(setting.key, setting.value)
438-
}
439-
}
440427

441428
Task writeConfig = project.tasks.create(name: name, type: DefaultTask, dependsOn: setup)
442429
writeConfig.doFirst {
430+
for (Map.Entry<String, Object> setting : node.config.settings) {
431+
if (setting.value == null) {
432+
esConfig.remove(setting.key)
433+
} else {
434+
esConfig.put(setting.key, setting.value)
435+
}
436+
}
437+
443438
esConfig = configFilter.call(esConfig)
444439
File configFile = new File(node.pathConf, 'elasticsearch.yml')
445440
logger.info("Configuring ${configFile}")
@@ -760,6 +755,12 @@ class ClusterFormationTasks {
760755
}
761756
start.doLast(elasticsearchRunner)
762757
start.doFirst {
758+
// If the node runs in a FIPS 140-2 JVM, the BCFKS default keystore will be password protected
759+
if (project.inFipsJvm){
760+
node.config.systemProperties.put('javax.net.ssl.trustStorePassword', 'password')
761+
node.config.systemProperties.put('javax.net.ssl.keyStorePassword', 'password')
762+
}
763+
763764
// Configure ES JAVA OPTS - adds system properties, assertion flags, remote debug etc
764765
List<String> esJavaOpts = [node.env.get('ES_JAVA_OPTS', '')]
765766
String collectedSystemProperties = node.config.systemProperties.collect { key, value -> "-D${key}=${value}" }.join(" ")

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/RestIntegTestTask.groovy

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -86,50 +86,23 @@ class RestIntegTestTask extends DefaultTask {
8686
runner.include('**/*IT.class')
8787
runner.systemProperty('tests.rest.load_packaged', 'false')
8888

89-
/*
90-
* We use lazy-evaluated strings in order to configure system properties whose value will not be known until
91-
* execution time (e.g. cluster port numbers). Adding these via the normal DSL doesn't work as these get treated
92-
* as task inputs and therefore Gradle attempts to snapshot them before/after task execution. This fails due
93-
* to the GStrings containing references to non-serializable objects.
94-
*
95-
* We bypass this by instead passing this system properties vi a CommandLineArgumentProvider. This has the added
96-
* side-effect that these properties are NOT treated as inputs, therefore they don't influence things like the
97-
* build cache key or up to date checking.
98-
*/
99-
def nonInputProperties = new CommandLineArgumentProvider() {
100-
private final Map<String, Object> systemProperties = [:]
101-
102-
void systemProperty(String key, Object value) {
103-
systemProperties.put(key, value)
104-
}
105-
106-
@Override
107-
Iterable<String> asArguments() {
108-
return systemProperties.collect { key, value ->
109-
"-D${key}=${value.toString()}".toString()
110-
}
111-
}
112-
}
113-
runner.jvmArgumentProviders.add(nonInputProperties)
114-
runner.ext.nonInputProperties = nonInputProperties
115-
11689
if (System.getProperty("tests.rest.cluster") == null) {
11790
if (System.getProperty("tests.cluster") != null) {
11891
throw new IllegalArgumentException("tests.rest.cluster and tests.cluster must both be null or non-null")
11992
}
12093
if (usesTestclusters == true) {
12194
ElasticsearchCluster cluster = project.testClusters."${name}"
122-
nonInputProperties.systemProperty('tests.rest.cluster', "${-> cluster.allHttpSocketURI.join(",") }")
123-
nonInputProperties.systemProperty('tests.cluster', "${-> cluster.transportPortURI }")
95+
runner.nonInputProperties.systemProperty('tests.rest.cluster', "${-> cluster.allHttpSocketURI.join(",") }")
96+
runner.nonInputProperties.systemProperty('tests.cluster', "${-> cluster.transportPortURI }")
12497
} else {
12598
// we pass all nodes to the rest cluster to allow the clients to round-robin between them
12699
// this is more realistic than just talking to a single node
127-
nonInputProperties.systemProperty('tests.rest.cluster', "${-> nodes.collect { it.httpUri() }.join(",")}")
128-
nonInputProperties.systemProperty('tests.config.dir', "${-> nodes[0].pathConf}")
100+
runner.nonInputProperties.systemProperty('tests.rest.cluster', "${-> nodes.collect { it.httpUri() }.join(",")}")
101+
runner.nonInputProperties.systemProperty('tests.config.dir', "${-> nodes[0].pathConf}")
129102
// TODO: our "client" qa tests currently use the rest-test plugin. instead they should have their own plugin
130103
// that sets up the test cluster and passes this transport uri instead of http uri. Until then, we pass
131104
// both as separate sysprops
132-
nonInputProperties.systemProperty('tests.cluster', "${-> nodes[0].transportUri()}")
105+
runner.nonInputProperties.systemProperty('tests.cluster', "${-> nodes[0].transportUri()}")
133106

134107
// dump errors and warnings from cluster log on failure
135108
TaskExecutionAdapter logDumpListener = new TaskExecutionAdapter() {

buildSrc/src/main/groovy/org/elasticsearch/gradle/test/StandaloneRestTestPlugin.groovy

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@ import org.elasticsearch.gradle.ExportElasticsearchBuildResourcesTask
2727
import org.elasticsearch.gradle.VersionProperties
2828
import org.elasticsearch.gradle.precommit.PrecommitTasks
2929
import org.gradle.api.InvalidUserDataException
30+
import org.gradle.api.JavaVersion
3031
import org.gradle.api.Plugin
3132
import org.gradle.api.Project
3233
import org.gradle.api.artifacts.Configuration
34+
import org.gradle.api.plugins.ExtraPropertiesExtension
3335
import org.gradle.api.plugins.JavaBasePlugin
3436
import org.gradle.api.plugins.JavaPlugin
37+
import org.gradle.api.plugins.JavaPluginExtension
3538
import org.gradle.api.tasks.SourceSet
3639
import org.gradle.api.tasks.SourceSetContainer
3740
import org.gradle.api.tasks.compile.JavaCompile
@@ -57,11 +60,14 @@ class StandaloneRestTestPlugin implements Plugin<Project> {
5760
project.pluginManager.apply(JavaBasePlugin)
5861

5962
project.getTasks().create("buildResources", ExportElasticsearchBuildResourcesTask)
60-
BuildPlugin.globalBuildInfo(project)
6163
BuildPlugin.configureRepositories(project)
6264
BuildPlugin.configureTestTasks(project)
6365
BuildPlugin.configureInputNormalization(project)
6466

67+
ExtraPropertiesExtension ext = project.extensions.getByType(ExtraPropertiesExtension)
68+
project.extensions.getByType(JavaPluginExtension).sourceCompatibility = ext.get('minimumRuntimeVersion') as JavaVersion
69+
project.extensions.getByType(JavaPluginExtension).targetCompatibility = ext.get('minimumRuntimeVersion') as JavaVersion
70+
6571
// only setup tests to build
6672
SourceSetContainer sourceSets = project.extensions.getByType(SourceSetContainer)
6773
SourceSet testSourceSet = sourceSets.create('test')

0 commit comments

Comments
 (0)