Skip to content

Commit 0ae103c

Browse files
authored
Avoid unnecessary eager creation of Gradle tasks (#45098) (#45310)
1 parent b716b84 commit 0ae103c

File tree

11 files changed

+276
-190
lines changed

11 files changed

+276
-190
lines changed

build.gradle

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ import org.elasticsearch.gradle.Version
2424
import org.elasticsearch.gradle.BwcVersions
2525
import org.elasticsearch.gradle.VersionProperties
2626
import org.elasticsearch.gradle.plugin.PluginBuildPlugin
27+
import org.elasticsearch.gradle.tool.Boilerplate
2728
import org.gradle.util.GradleVersion
2829
import org.gradle.util.DistributionLocator
2930
import org.gradle.plugins.ide.eclipse.model.SourceFolder
3031

32+
import static org.elasticsearch.gradle.tool.Boilerplate.maybeConfigure
33+
3134
plugins {
3235
id 'com.gradle.build-scan' version '2.3'
3336
id 'base'
@@ -212,7 +215,7 @@ task branchConsistency {
212215

213216
allprojects {
214217
// ignore missing javadocs
215-
tasks.withType(Javadoc) { Javadoc javadoc ->
218+
tasks.withType(Javadoc).configureEach { Javadoc javadoc ->
216219
// the -quiet here is because of a bug in gradle, in that adding a string option
217220
// by itself is not added to the options. By adding quiet, both this option and
218221
// the "value" -quiet is added, separated by a space. This is ok since the javadoc
@@ -329,13 +332,9 @@ allprojects {
329332
}
330333
}
331334

332-
task cleanIdeaBuildDir(type: Delete) {
333-
delete 'build-idea'
335+
tasks.named('cleanIdea') {
336+
delete 'build-idea'
334337
}
335-
cleanIdeaBuildDir.setGroup("ide")
336-
cleanIdeaBuildDir.setDescription("Deletes the IDEA build directory.")
337-
338-
tasks.cleanIdea.dependsOn(cleanIdeaBuildDir)
339338
}
340339

341340
idea {
@@ -390,29 +389,20 @@ allprojects {
390389

391390
String lineSeparator = Os.isFamily(Os.FAMILY_WINDOWS) ? '\\\\r\\\\n' : '\\\\n'
392391
String licenseHeader = licenseHeaderFile.getText('UTF-8').replace(System.lineSeparator(), lineSeparator)
393-
task copyEclipseSettings(type: Copy) {
392+
tasks.register('copyEclipseSettings', Copy) {
393+
mustRunAfter 'wipeEclipseSettings'
394394
// TODO: "package this up" for external builds
395395
from new File(project.rootDir, 'buildSrc/src/main/resources/eclipse.settings')
396396
into '.settings'
397397
filter{ it.replaceAll('@@LICENSE_HEADER_TEXT@@', licenseHeader)}
398398
}
399399
// otherwise .settings is not nuked entirely
400-
task wipeEclipseSettings(type: Delete) {
400+
tasks.register('wipeEclipseSettings', Delete) {
401401
delete '.settings'
402402
}
403-
tasks.cleanEclipse.dependsOn(wipeEclipseSettings)
403+
tasks.named('cleanEclipse') { dependsOn 'wipeEclipseSettings' }
404404
// otherwise the eclipse merging is *super confusing*
405-
tasks.eclipse.dependsOn(cleanEclipse, copyEclipseSettings)
406-
407-
// work arround https://github.com/gradle/gradle/issues/6582
408-
tasks.eclipseProject.mustRunAfter tasks.cleanEclipseProject
409-
tasks.matching { it.name == 'eclipseClasspath' }.all {
410-
it.mustRunAfter { tasks.cleanEclipseClasspath }
411-
}
412-
tasks.matching { it.name == 'eclipseJdt' }.all {
413-
it.mustRunAfter { tasks.cleanEclipseJdt }
414-
}
415-
tasks.copyEclipseSettings.mustRunAfter tasks.wipeEclipseSettings
405+
tasks.named('eclipse') { dependsOn 'cleanEclipse', 'copyEclipseSettings' }
416406
}
417407

418408
allprojects {
@@ -477,13 +467,11 @@ gradle.projectsEvaluated {
477467
* need to publish artifacts for them.
478468
*/
479469
if (project.name.equals('qa') || project.path.contains(':qa:')) {
480-
Task assemble = project.tasks.findByName('assemble')
481-
if (assemble) {
482-
assemble.enabled = false
470+
maybeConfigure(project.tasks, 'assemble') {
471+
it.enabled = false
483472
}
484-
Task dependenciesInfo = project.tasks.findByName('dependenciesInfo')
485-
if (dependenciesInfo) {
486-
dependenciesInfo.enabled = false
473+
maybeConfigure(project.tasks, 'dependenciesInfo') {
474+
it.enabled = false
487475
}
488476
}
489477
}
@@ -505,7 +493,7 @@ gradle.projectsEvaluated {
505493
}
506494

507495
allprojects {
508-
task resolveAllDependencies {
496+
tasks.register('resolveAllDependencies') {
509497
dependsOn tasks.matching { it.name == "pullFixture"}
510498
doLast {
511499
configurations.findAll { it.isCanBeResolved() }.each { it.resolve() }
@@ -535,13 +523,13 @@ allprojects {
535523
}
536524
}
537525

538-
task checkPart1
539-
task checkPart2
540-
tasks.matching { it.name == "check" }.all { check ->
541-
if (check.path.startsWith(":x-pack:")) {
542-
checkPart2.dependsOn check
543-
} else {
544-
checkPart1.dependsOn check
545-
}
546-
}
526+
def checkPart1 = tasks.register('checkPart1')
527+
def checkPart2 = tasks.register('checkPart2')
528+
plugins.withId('lifecycle-base') {
529+
if (project.path.startsWith(":x-pack:")) {
530+
checkPart1.configure { dependsOn 'check' }
531+
} else {
532+
checkPart2.configure { dependsOn 'check' }
533+
}
534+
}
547535
}

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

Lines changed: 79 additions & 53 deletions
Large diffs are not rendered by default.

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.gradle.api.artifacts.Configuration
2525
import org.gradle.api.artifacts.Dependency
2626
import org.gradle.api.artifacts.DependencyResolutionListener
2727
import org.gradle.api.artifacts.DependencySet
28+
import org.gradle.api.internal.ConventionTask
2829
import org.gradle.api.tasks.Input
2930
import org.gradle.api.tasks.InputDirectory
3031
import org.gradle.api.tasks.OutputFile
@@ -45,7 +46,7 @@ import java.util.regex.Pattern
4546
* </ul>
4647
*
4748
*/
48-
public class DependenciesInfoTask extends DefaultTask {
49+
public class DependenciesInfoTask extends ConventionTask {
4950

5051
/** Dependencies to gather information from. */
5152
@Input
@@ -55,8 +56,7 @@ public class DependenciesInfoTask extends DefaultTask {
5556
@Input
5657
public Configuration compileOnlyConfiguration
5758

58-
@Input
59-
public LinkedHashMap<String, String> mappings
59+
private LinkedHashMap<String, String> mappings
6060

6161
/** Directory to read license files */
6262
@InputDirectory
@@ -93,7 +93,7 @@ public class DependenciesInfoTask extends DefaultTask {
9393
}
9494

9595
final String url = createURL(dependency.group, dependency.name, dependency.version)
96-
final String dependencyName = DependencyLicensesTask.getDependencyName(mappings, dependency.name)
96+
final String dependencyName = DependencyLicensesTask.getDependencyName(getMappings(), dependency.name)
9797
logger.info("mapped dependency ${dependency.group}:${dependency.name} to ${dependencyName} for license info")
9898

9999
final String licenseType = getLicenseType(dependency.group, dependencyName)
@@ -103,7 +103,15 @@ public class DependenciesInfoTask extends DefaultTask {
103103
outputFile.setText(output.toString(), 'UTF-8')
104104
}
105105

106-
/**
106+
@Input
107+
LinkedHashMap<String, String> getMappings() {
108+
return mappings
109+
}
110+
111+
void setMappings(LinkedHashMap<String, String> mappings) {
112+
this.mappings = mappings
113+
}
114+
/**
107115
* Create an URL on <a href="https://repo1.maven.org/maven2/">Maven Central</a>
108116
* based on dependency coordinates.
109117
*/

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import org.gradle.api.InvalidUserDataException
3232
import org.gradle.api.Plugin
3333
import org.gradle.api.Project
3434
import org.gradle.api.Task
35+
import org.gradle.api.plugins.BasePlugin
3536
import org.gradle.api.publish.maven.MavenPublication
3637
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin
3738
import org.gradle.api.publish.maven.tasks.GenerateMavenPom
@@ -112,7 +113,7 @@ class PluginBuildPlugin implements Plugin<Project> {
112113
addNoticeGeneration(project, extension)
113114
}
114115
}
115-
project.testingConventions {
116+
project.tasks.named('testingConventions').configure {
116117
naming.clear()
117118
naming {
118119
Tests {
@@ -175,7 +176,7 @@ class PluginBuildPlugin implements Plugin<Project> {
175176
/** Adds an integTest task which runs rest tests */
176177
private static void createIntegTestTask(Project project) {
177178
RestIntegTestTask integTest = project.tasks.create('integTest', RestIntegTestTask.class)
178-
integTest.mustRunAfter(project.precommit, project.test)
179+
integTest.mustRunAfter('precommit', 'test')
179180
if (project.plugins.hasPlugin(TestClustersPlugin.class) == false) {
180181
// only if not using test clusters
181182
project.integTestCluster.distribution = System.getProperty('tests.distribution', 'integ-test-zip')
@@ -259,7 +260,9 @@ class PluginBuildPlugin implements Plugin<Project> {
259260
include 'bin/**'
260261
}
261262
}
262-
project.assemble.dependsOn(bundle)
263+
project.tasks.named(BasePlugin.ASSEMBLE_TASK_NAME).configure {
264+
dependsOn(bundle)
265+
}
263266

264267
// also make the zip available as a configuration (used when depending on this project)
265268
project.configurations.create('zip')

0 commit comments

Comments
 (0)