Skip to content

Commit a72760e

Browse files
authored
Replace immediate task creations by using task avoidance api (#60071)
- Replace immediate task creations by using task avoidance api - One step closer to #56610 - Still many tasks are created during configuration phase. Tackled in separate steps
1 parent e27f8fb commit a72760e

File tree

57 files changed

+544
-510
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+544
-510
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/doc/DocsTestPlugin.groovy

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.elasticsearch.gradle.VersionProperties
2424
import org.gradle.api.Plugin
2525
import org.gradle.api.Project
2626
import org.gradle.api.Task
27+
import org.gradle.api.tasks.TaskProvider
2728

2829
/**
2930
* Sets up tests for documentation.
@@ -42,7 +43,7 @@ class DocsTestPlugin implements Plugin<Project> {
4243
project.testClusters.integTest.nameCustomization = { it.replace("integTest", "node") }
4344
// Docs are published separately so no need to assemble
4445
project.tasks.assemble.enabled = false
45-
Map<String, String> defaultSubstitutions = [
46+
Map<String, String> commonDefaultSubstitutions = [
4647
/* These match up with the asciidoc syntax for substitutions but
4748
* the values may differ. In particular {version} needs to resolve
4849
* to the version being built for testing but needs to resolve to
@@ -53,26 +54,26 @@ class DocsTestPlugin implements Plugin<Project> {
5354
'\\{build_flavor\\}' : distribution,
5455
'\\{build_type\\}' : OS.conditionalString().onWindows({"zip"}).onUnix({"tar"}).supply(),
5556
]
56-
Task listSnippets = project.tasks.create('listSnippets', SnippetsTask)
57-
listSnippets.group 'Docs'
58-
listSnippets.description 'List each snippet'
59-
listSnippets.defaultSubstitutions = defaultSubstitutions
60-
listSnippets.perSnippet { println(it.toString()) }
61-
62-
Task listConsoleCandidates = project.tasks.create(
63-
'listConsoleCandidates', SnippetsTask)
64-
listConsoleCandidates.group 'Docs'
65-
listConsoleCandidates.description
66-
'List snippets that probably should be marked // CONSOLE'
67-
listConsoleCandidates.defaultSubstitutions = defaultSubstitutions
68-
listConsoleCandidates.perSnippet {
69-
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
70-
println(it.toString())
57+
project.tasks.register('listSnippets', SnippetsTask) {
58+
group 'Docs'
59+
description 'List each snippet'
60+
defaultSubstitutions = commonDefaultSubstitutions
61+
perSnippet { println(it.toString()) }
62+
}
63+
project.tasks.register('listConsoleCandidates', SnippetsTask) {
64+
group 'Docs'
65+
description
66+
'List snippets that probably should be marked // CONSOLE'
67+
defaultSubstitutions = commonDefaultSubstitutions
68+
perSnippet {
69+
if (RestTestsFromSnippetsTask.isConsoleCandidate(it)) {
70+
println(it.toString())
71+
}
7172
}
7273
}
7374

74-
Task buildRestTests = project.tasks.create(
75-
'buildRestTests', RestTestsFromSnippetsTask)
76-
buildRestTests.defaultSubstitutions = defaultSubstitutions
75+
project.tasks.register('buildRestTests', RestTestsFromSnippetsTask) {
76+
defaultSubstitutions = commonDefaultSubstitutions
77+
}
7778
}
7879
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.gradle.api.tasks.bundling.Zip
4343

4444
import java.util.regex.Matcher
4545
import java.util.regex.Pattern
46+
4647
/**
4748
* Encapsulates build configuration for an Elasticsearch plugin.
4849
*/
@@ -64,7 +65,9 @@ class PluginBuildPlugin implements Plugin<Project> {
6465

6566
createIntegTestTask(project)
6667
createBundleTasks(project, extension)
67-
project.tasks.integTest.dependsOn(project.tasks.bundlePlugin)
68+
project.tasks.named("integTest").configure {
69+
it.dependsOn(project.tasks.named("bundlePlugin"))
70+
}
6871
if (isModule) {
6972
project.testClusters.integTest.module(project.tasks.bundlePlugin.archiveFile)
7073
} else {
@@ -77,7 +80,7 @@ class PluginBuildPlugin implements Plugin<Project> {
7780
if (project.findProject(":modules:${pluginName}") != null) {
7881
project.integTest.dependsOn(project.project(":modules:${pluginName}").tasks.bundlePlugin)
7982
project.testClusters.integTest.module(
80-
project.project(":modules:${pluginName}").tasks.bundlePlugin.archiveFile
83+
project.project(":modules:${pluginName}").tasks.bundlePlugin.archiveFile
8184
)
8285
}
8386
}
@@ -98,15 +101,15 @@ class PluginBuildPlugin implements Plugin<Project> {
98101
}
99102

100103
Map<String, String> properties = [
101-
'name' : extension1.name,
102-
'description' : extension1.description,
103-
'version' : extension1.version,
104-
'elasticsearchVersion': Version.fromString(VersionProperties.elasticsearch).toString(),
105-
'javaVersion' : project.targetCompatibility as String,
106-
'classname' : extension1.classname,
107-
'extendedPlugins' : extension1.extendedPlugins.join(','),
108-
'hasNativeController' : extension1.hasNativeController,
109-
'requiresKeystore' : extension1.requiresKeystore
104+
'name' : extension1.name,
105+
'description' : extension1.description,
106+
'version' : extension1.version,
107+
'elasticsearchVersion': Version.fromString(VersionProperties.elasticsearch).toString(),
108+
'javaVersion' : project.targetCompatibility as String,
109+
'classname' : extension1.classname,
110+
'extendedPlugins' : extension1.extendedPlugins.join(','),
111+
'hasNativeController' : extension1.hasNativeController,
112+
'requiresKeystore' : extension1.requiresKeystore
110113
]
111114
project.tasks.named('pluginProperties').configure {
112115
expand(properties)
@@ -139,7 +142,7 @@ class PluginBuildPlugin implements Plugin<Project> {
139142
}
140143
}
141144
project.configurations.getByName('default')
142-
.extendsFrom(project.configurations.getByName('runtimeClasspath'))
145+
.extendsFrom(project.configurations.getByName('runtimeClasspath'))
143146
// allow running ES with this plugin in the foreground of a build
144147
project.tasks.register('run', RunTask) {
145148
dependsOn(project.tasks.bundlePlugin)
@@ -219,7 +222,7 @@ class PluginBuildPlugin implements Plugin<Project> {
219222
*/
220223
from { project.plugins.hasPlugin(ShadowPlugin) ? project.shadowJar : project.jar }
221224
from project.configurations.runtimeClasspath - project.configurations.getByName(
222-
CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME
225+
CompileOnlyResolvePlugin.RESOLVEABLE_COMPILE_ONLY_CONFIGURATION_NAME
223226
)
224227
// extra files for the plugin to go into the zip
225228
from('src/main/packaging') // TODO: move all config/bin/_size/etc into packaging
@@ -229,7 +232,7 @@ class PluginBuildPlugin implements Plugin<Project> {
229232
}
230233
}
231234
project.tasks.named(BasePlugin.ASSEMBLE_TASK_NAME).configure {
232-
dependsOn(bundle)
235+
dependsOn(bundle)
233236
}
234237

235238
// also make the zip available as a configuration (used when depending on this project)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class StandaloneRestTestPlugin implements Plugin<Project> {
6262
project.pluginManager.apply(TestClustersPlugin)
6363
project.pluginManager.apply(RepositoriesSetupPlugin)
6464

65-
project.getTasks().create("buildResources", ExportElasticsearchBuildResourcesTask)
65+
project.getTasks().register("buildResources", ExportElasticsearchBuildResourcesTask)
6666
ElasticsearchJavaPlugin.configureTestTasks(project)
6767
ElasticsearchJavaPlugin.configureInputNormalization(project)
6868
ElasticsearchJavaPlugin.configureCompile(project)

buildSrc/src/main/java/org/elasticsearch/gradle/testclusters/TestClustersPlugin.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@ private NamedDomainObjectContainer<ElasticsearchCluster> createTestClustersConta
9696
}
9797

9898
private void createListClustersTask(Project project, NamedDomainObjectContainer<ElasticsearchCluster> container) {
99-
Task listTask = project.getTasks().create(LIST_TASK_NAME);
100-
listTask.setGroup("ES cluster formation");
101-
listTask.setDescription("Lists all ES clusters configured for this project");
102-
listTask.doLast(
103-
(Task task) -> container.forEach(cluster -> logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getNumberOfNodes()))
104-
);
99+
// Task is never up to date so we can pass an lambda for the task action
100+
project.getTasks().register(LIST_TASK_NAME, task -> {
101+
task.setGroup("ES cluster formation");
102+
task.setDescription("Lists all ES clusters configured for this project");
103+
task.doLast(
104+
(Task t) -> container.forEach(cluster -> logger.lifecycle(" * {}: {}", cluster.getName(), cluster.getNumberOfNodes()))
105+
);
106+
});
107+
105108
}
106109

107110
static class TestClustersHookPlugin implements Plugin<Project> {

distribution/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ tasks.register("generateDependenciesReport", ConcatFilesTask) {
5454
*****************************************************************************/
5555

5656
// integ test zip only uses server, so a different notice file is needed there
57-
task buildServerNotice(type: NoticeTask)
57+
tasks.register("buildServerNotice", NoticeTask)
5858

5959
// other distributions include notices from modules as well, which are added below later
60-
task buildDefaultNotice(type: NoticeTask) {
60+
tasks.register("buildDefaultNotice", NoticeTask).configure {
6161
licensesDir new File(project(':distribution').projectDir, 'licenses')
6262
}
6363

64-
task buildOssNotice(type: NoticeTask) {
64+
tasks.register("buildOssNotice", NoticeTask).configure {
6565
licensesDir new File(project(':distribution').projectDir, 'licenses')
6666
}
6767

68-
task buildDefaultNoJdkNotice(type: NoticeTask)
68+
tasks.register("buildDefaultNoJdkNotice", NoticeTask)
6969

70-
task buildOssNoJdkNotice(type: NoticeTask)
70+
tasks.register("buildOssNoJdkNotice", NoticeTask)
7171

7272
// The :server and :libs projects belong to all distributions
73-
tasks.withType(NoticeTask) {
73+
tasks.withType(NoticeTask).configureEach {
7474
licensesDir project(':server').file('licenses')
7575
source project(':server').file('src/main/java')
7676
project(':libs').subprojects.each { Project lib ->

distribution/bwc/build.gradle

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
4242
String bwcBranch = unreleasedVersion.branch
4343
apply plugin: 'distribution'
4444
// Not published so no need to assemble
45-
assemble.enabled = false
46-
45+
tasks.named("assemble").configure {
46+
enabled = false
47+
}
4748
File checkoutDir = file("${buildDir}/bwc/checkout-${bwcBranch}")
4849

4950
final String remote = System.getProperty("bwc.remote", "elastic")
@@ -58,13 +59,13 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
5859
throw new GradleException("tests.bwc.git_fetch_latest must be [true] or [false] but was [" + gitFetchLatestProperty + "]")
5960
}
6061

61-
task createClone(type: LoggedExec) {
62+
tasks.register("createClone", LoggedExec) {
6263
onlyIf { checkoutDir.exists() == false }
6364
commandLine = ['git', 'clone', rootDir, checkoutDir]
6465
}
6566

66-
task findRemote(type: LoggedExec) {
67-
dependsOn createClone
67+
tasks.register("findRemote", LoggedExec) {
68+
dependsOn "createClone"
6869
workingDir = checkoutDir
6970
commandLine = ['git', 'remote', '-v']
7071
ByteArrayOutputStream output = new ByteArrayOutputStream()
@@ -79,16 +80,16 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
7980
}
8081
}
8182

82-
task addRemote(type: LoggedExec) {
83+
tasks.register("addRemote", LoggedExec) {
8384
dependsOn findRemote
8485
onlyIf { project.ext.remoteExists == false }
8586
workingDir = checkoutDir
8687
commandLine = ['git', 'remote', 'add', "${remote}", "https://github.com/${remote}/elasticsearch.git"]
8788
}
8889

89-
task fetchLatest(type: LoggedExec) {
90+
tasks.register("fetchLatest", LoggedExec) {
9091
onlyIf { project.gradle.startParameter.isOffline() == false && gitFetchLatest }
91-
dependsOn addRemote
92+
dependsOn("addRemote")
9293
workingDir = checkoutDir
9394
commandLine = ['git', 'fetch', '--all']
9495
}
@@ -104,8 +105,8 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
104105
return os.toString().trim()
105106
}
106107
}
107-
task checkoutBwcBranch() {
108-
dependsOn fetchLatest
108+
tasks.register("checkoutBwcBranch") {
109+
dependsOn("fetchLatest")
109110
doLast {
110111
def refspec = System.getProperty("bwc.refspec." + bwcBranch) ?: System.getProperty("tests.bwc.refspec." + bwcBranch) ?: "${remote}/${bwcBranch}"
111112
if (System.getProperty("bwc.checkout.align") != null) {
@@ -156,9 +157,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
156157
}
157158

158159

159-
Closure createRunBwcGradleTask = { name, extraConfig ->
160+
Closure<TaskProvider> createRunBwcGradleTask = { name, extraConfig ->
160161
return tasks.register("$name", LoggedExec) {
161-
dependsOn checkoutBwcBranch
162+
dependsOn "checkoutBwcBranch"
162163
spoolOutput = true
163164
workingDir = checkoutDir
164165
doFirst {
@@ -304,7 +305,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
304305
Version currentVersion = Version.fromString(version)
305306
if (currentVersion.getMinor() == 0 && currentVersion.getRevision() == 0) {
306307
// We only want to resolve dependencies for live versions of master, without cascading this to older versions
307-
resolveAllDependencies.dependsOn resolveAllBwcDependencies
308+
tasks.named("resolveAllDependencies").configure {
309+
dependsOn("resolveAllBwcDependencies")
310+
}
308311
}
309312

310313
for (e in artifactFiles) {
@@ -326,7 +329,9 @@ BuildParams.bwcVersions.forPreviousUnreleased { BwcVersions.UnreleasedVersionInf
326329
}
327330
}
328331
// make sure no dependencies were added to assemble; we want it to be a no-op
329-
assemble.dependsOn = []
332+
tasks.named("assemble").configure {
333+
dependsOn = []
334+
}
330335
}
331336
}
332337

0 commit comments

Comments
 (0)