Skip to content

Commit 731b282

Browse files
authored
Improvement usage of gradle task avoidance api (#56627)
Use gradle task avoidance api wherever it is possible as a drop in replacement in the es build
1 parent bea2341 commit 731b282

File tree

22 files changed

+109
-92
lines changed

22 files changed

+109
-92
lines changed

build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ subprojects {
108108
}
109109
}
110110

111-
task updateCIBwcVersions() {
111+
tasks.register("updateCIBwcVersions") {
112112
doLast {
113113
File yml = file(".ci/bwcVersions")
114114
yml.text = ""
@@ -142,7 +142,7 @@ allprojects {
142142
}
143143
}
144144

145-
task verifyVersions {
145+
tasks.register("verifyVersions") {
146146
doLast {
147147
if (gradle.startParameter.isOffline()) {
148148
throw new GradleException("Must run in online mode to verify versions")
@@ -194,18 +194,18 @@ subprojects {
194194
ext.bwc_tests_enabled = bwc_tests_enabled
195195
}
196196

197-
task verifyBwcTestsEnabled {
197+
tasks.register("verifyBwcTestsEnabled") {
198198
doLast {
199199
if (bwc_tests_enabled == false) {
200200
throw new GradleException('Bwc tests are disabled. They must be re-enabled after completing backcompat behavior backporting.')
201201
}
202202
}
203203
}
204204

205-
task branchConsistency {
205+
tasks.register("branchConsistency") {
206206
description 'Ensures this branch is internally consistent. For example, that versions constants match released versions.'
207207
group 'Verification'
208-
dependsOn verifyVersions, verifyBwcTestsEnabled
208+
dependsOn ":verifyVersions", ":verifyBwcTestsEnabled"
209209
}
210210

211211
allprojects {
@@ -406,7 +406,7 @@ class Run extends DefaultTask {
406406
}
407407
}
408408

409-
task run(type: Run) {
409+
tasks.register("run", Run) {
410410
dependsOn ':distribution:run'
411411
description = 'Runs elasticsearch in the foreground'
412412
group = 'Verification'
@@ -485,7 +485,7 @@ allprojects {
485485
if (realTask == null) {
486486
return
487487
}
488-
project.tasks.create(taskName) {
488+
project.tasks.register(taskName) {
489489
doLast {
490490
println("${realTask.path} dependencies:")
491491
for (Task dep : realTask.getTaskDependencies().getDependencies(realTask)) {

buildSrc/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ if (project == rootProject) {
4848
Properties props = VersionPropertiesLoader.loadBuildSrcVersion(project.file('version.properties'))
4949
version = props.getProperty("elasticsearch")
5050

51-
task generateVersionProperties(type: WriteProperties) {
51+
def generateVersionProperties = tasks.register("generateVersionProperties", WriteProperties) {
5252
outputFile = "${buildDir}/version.properties"
5353
comment = 'Generated version properties'
5454
properties(props)
@@ -234,13 +234,13 @@ if (project != rootProject) {
234234
}
235235
}
236236

237-
task integTest(type: Test) {
237+
tasks.register("integTest", Test) {
238238
inputs.dir(file("src/testKit")).withPropertyName("testkit dir").withPathSensitivity(PathSensitivity.RELATIVE)
239239
systemProperty 'test.version_under_test', version
240240
onlyIf { org.elasticsearch.gradle.info.BuildParams.inFipsJvm == false }
241241
maxParallelForks = System.getProperty('tests.jvms', org.elasticsearch.gradle.info.BuildParams.defaultParallel.toString()) as Integer
242242
}
243-
check.dependsOn(integTest)
243+
check.dependsOn("integTest")
244244

245245
// for now we hardcode the tests for our build to use the gradle jvm.
246246
tasks.withType(Test).configureEach {

buildSrc/reaper/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
jar {
2-
archiveName = "${project.name}.jar"
2+
archiveFileName = "${project.name}.jar"
33
manifest {
44
attributes 'Main-Class': 'org.elasticsearch.gradle.reaper.Reaper'
55
}

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import org.gradle.api.GradleException
2626
import org.gradle.api.Task
2727
import org.gradle.api.tasks.Exec
2828
import org.gradle.api.tasks.Internal
29+
import org.gradle.api.tasks.TaskProvider
30+
2931
/**
3032
* A fixture for integration tests which runs in a separate process launched by Ant.
3133
*/
@@ -79,7 +81,7 @@ class AntFixture extends AntTask implements Fixture {
7981
return tmpFile.exists()
8082
}
8183

82-
private final Task stopTask
84+
private final TaskProvider stopTask
8385

8486
AntFixture() {
8587
stopTask = createStopTask()
@@ -88,7 +90,7 @@ class AntFixture extends AntTask implements Fixture {
8890

8991
@Override
9092
@Internal
91-
Task getStopTask() {
93+
TaskProvider getStopTask() {
9294
return stopTask
9395
}
9496

@@ -222,24 +224,29 @@ class AntFixture extends AntTask implements Fixture {
222224
}
223225

224226
/** Adds a task to kill an elasticsearch node with the given pidfile */
225-
private Task createStopTask() {
227+
private TaskProvider createStopTask() {
226228
final AntFixture fixture = this
227229
final Object pid = "${ -> fixture.pid }"
228-
Exec stop = project.tasks.create(name: "${name}#stop", type: LoggedExec)
229-
stop.onlyIf { fixture.pidFile.exists() }
230-
stop.doFirst {
231-
logger.info("Shutting down ${fixture.name} with pid ${pid}")
232-
}
233-
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
234-
stop.executable = 'Taskkill'
235-
stop.args('/PID', pid, '/F')
236-
} else {
237-
stop.executable = 'kill'
238-
stop.args('-9', pid)
239-
}
240-
stop.doLast {
241-
project.delete(fixture.pidFile)
230+
TaskProvider<Exec> stop = project.tasks.register("${name}#stop", LoggedExec)
231+
stop.configure {
232+
onlyIf { fixture.pidFile.exists() }
233+
doFirst {
234+
logger.info("Shutting down ${fixture.name} with pid ${pid}")
235+
}
236+
237+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
238+
executable = 'Taskkill'
239+
args('/PID', pid, '/F')
240+
} else {
241+
executable = 'kill'
242+
args('-9', pid)
243+
}
244+
doLast {
245+
project.delete(fixture.pidFile)
246+
}
247+
242248
}
249+
243250
return stop
244251
}
245252

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ class TestWithDependenciesPlugin implements Plugin<Project> {
5858
String outputDir = "${project.buildDir}/generated-resources/${pluginProject.name}"
5959
String camelName = pluginProject.name.replaceAll(/-(\w)/) { _, c -> c.toUpperCase(Locale.ROOT) }
6060
String taskName = "copy" + camelName[0].toUpperCase(Locale.ROOT) + camelName.substring(1) + "Metadata"
61-
Copy copyPluginMetadata = project.tasks.create(taskName, Copy.class)
62-
copyPluginMetadata.into(outputDir)
63-
copyPluginMetadata.from(pluginProject.tasks.pluginProperties)
64-
copyPluginMetadata.from(pluginProject.file('src/main/plugin-metadata'))
61+
project.tasks.register(taskName, Copy.class) {
62+
into(outputDir)
63+
from(pluginProject.tasks.pluginProperties)
64+
from(pluginProject.file('src/main/plugin-metadata'))
65+
}
66+
6567
project.sourceSets.test.output.dir(outputDir, builtBy: taskName)
6668
}
6769
}

buildSrc/src/test/java/org/elasticsearch/gradle/ExportElasticsearchBuildResourcesTaskIT.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ public void testUpToDateWithSourcesConfigured() {
3232
BuildResult result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build();
3333
assertTaskSuccessful(result, ":buildResources");
3434
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml");
35-
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
35+
36+
// using task avoidance api means the task configuration of the sample task is never triggered
37+
assertBuildFileDoesNotExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
3638

3739
result = getGradleRunner(PROJECT_NAME).withArguments("buildResources", "-s", "-i").build();
3840
assertTaskUpToDate(result, ":buildResources");
3941
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle.xml");
40-
assertBuildFileExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
42+
43+
// using task avoidance api means the task configuration of the sample task is never triggered
44+
assertBuildFileDoesNotExists(result, PROJECT_NAME, "build-tools-exported/checkstyle_suppressions.xml");
4145
}
4246

4347
public void testImplicitTaskDependencyCopy() {
@@ -46,8 +50,10 @@ public void testImplicitTaskDependencyCopy() {
4650
assertTaskSuccessful(result, ":buildResources");
4751
assertTaskSuccessful(result, ":sampleCopyAll");
4852
assertBuildFileExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle.xml");
49-
// This is a side effect of compile time reference
50-
assertBuildFileExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle_suppressions.xml");
53+
54+
// using task avoidance api means the task configuration of the sample task is never triggered
55+
// which means buildResource is not configured to copy this file
56+
assertBuildFileDoesNotExists(result, PROJECT_NAME, "sampleCopyAll/checkstyle_suppressions.xml");
5157
}
5258

5359
public void testImplicitTaskDependencyInputFileOfOther() {

buildSrc/src/test/java/org/elasticsearch/gradle/precommit/ThirdPartyAuditTaskIT.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class ThirdPartyAuditTaskIT extends GradleIntegrationTestCase {
2727
@Before
2828
public void setUp() throws Exception {
2929
// Build the sample jars
30-
getGradleRunner("thirdPartyAudit").withArguments("build", "-s").build();
30+
getGradleRunner("thirdPartyAudit").withArguments(":sample_jars:build", "-s").build();
3131
}
3232

3333
public void testElasticsearchIgnored() {
3434
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
35-
"clean",
36-
"empty",
35+
":clean",
36+
":empty",
3737
"-s",
3838
"-PcompileOnlyGroup=elasticsearch.gradle:broken-log4j",
3939
"-PcompileOnlyVersion=0.0.1",
@@ -44,9 +44,9 @@ public void testElasticsearchIgnored() {
4444
}
4545

4646
public void testWithEmptyRules() {
47-
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
48-
"clean",
49-
"empty",
47+
getGradleRunner("thirdPartyAudit").withArguments(
48+
":clean",
49+
":empty",
5050
"-s",
5151
"-PcompileOnlyGroup=other.gradle:broken-log4j",
5252
"-PcompileOnlyVersion=0.0.1",
@@ -57,8 +57,8 @@ public void testWithEmptyRules() {
5757

5858
public void testViolationFoundAndCompileOnlyIgnored() {
5959
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
60-
"clean",
61-
"absurd",
60+
":clean",
61+
":absurd",
6262
"-s",
6363
"-PcompileOnlyGroup=other.gradle:broken-log4j",
6464
"-PcompileOnlyVersion=0.0.1",
@@ -73,8 +73,8 @@ public void testViolationFoundAndCompileOnlyIgnored() {
7373

7474
public void testClassNotFoundAndCompileOnlyIgnored() {
7575
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
76-
"clean",
77-
"absurd",
76+
":clean",
77+
":absurd",
7878
"-s",
7979
"-PcompileGroup=other.gradle:broken-log4j",
8080
"-PcompileVersion=0.0.1",
@@ -94,8 +94,8 @@ public void testClassNotFoundAndCompileOnlyIgnored() {
9494

9595
public void testJarHellWithJDK() {
9696
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
97-
"clean",
98-
"absurd",
97+
":clean",
98+
":absurd",
9999
"-s",
100100
"-PcompileGroup=other.gradle:jarhellJdk",
101101
"-PcompileVersion=0.0.1",
@@ -115,8 +115,8 @@ public void testJarHellWithJDK() {
115115

116116
public void testElasticsearchIgnoredWithViolations() {
117117
BuildResult result = getGradleRunner("thirdPartyAudit").withArguments(
118-
"clean",
119-
"absurd",
118+
":clean",
119+
":absurd",
120120
"-s",
121121
"-PcompileOnlyGroup=elasticsearch.gradle:broken-log4j",
122122
"-PcompileOnlyVersion=0.0.1",

buildSrc/src/testKit/distribution-download/distribution/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ if (distroConfig != null) {
33
// setup the test distribution as an artifact of this project
44
String distroType = System.getProperty('tests.distro.type')
55

6-
Task buildDistro
6+
def buildDistro
77
File buildFile
88
if (['rpm', 'deb'].contains(distroType)) {
9-
buildDistro = project.tasks.create("build" + distroType.capitalize(), Copy) {
9+
buildDistro = project.tasks.register("build" + distroType.capitalize(), Copy) {
1010
from 'files'
1111
into 'build/files'
1212
include 'fake_elasticsearch.tar.gz'
@@ -21,7 +21,7 @@ if (distroConfig != null) {
2121
extension = "zip"
2222
}
2323
// copy file as is
24-
buildDistro = project.tasks.create("buildArchive", Copy) {
24+
buildDistro = project.tasks.register("buildArchive", Copy) {
2525
from 'files'
2626
include "fake_elasticsearch.${extension}"
2727
into 'build/files'

buildSrc/src/testKit/distribution-download/subproj/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ elasticsearch_distributions {
3636
}
3737
}
3838

39-
task assertDistroFile {
39+
tasks.register("assertDistroFile") {
4040
dependsOn elasticsearch_distributions.test_distro
4141
doLast {
4242
File distroFile = new File(elasticsearch_distributions.test_distro.toString())
@@ -50,7 +50,7 @@ task assertDistroFile {
5050
}
5151

5252
if (['rpm', 'deb'].contains(distroType) == false) {
53-
task assertDistroExtracted {
53+
tasks.register("assertDistroExtracted") {
5454
dependsOn elasticsearch_distributions.test_distro.extracted, assertDistroFile
5555
doLast {
5656
File distroExtracted = new File(elasticsearch_distributions.test_distro.extracted.toString())

buildSrc/src/testKit/elasticsearch-build-resources/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ buildResources {
99
copy 'checkstyle.xml'
1010
}
1111

12-
task sampleCopyAll(type: Sync) {
12+
tasks.register("sampleCopyAll", Sync) {
1313
/** Note: no explicit dependency. This works with tasks that use the Provider API a.k.a "Lazy Configuration" **/
1414
from buildResources
1515
into "$buildDir/sampleCopyAll"
1616
}
1717

18-
task sample {
18+
tasks.register("sample") {
1919
// This does not work, task dependencies can't be providers
2020
// dependsOn buildResources.resource('minimumRuntimeVersion')
2121
// Nor does this, despite https://github.com/gradle/gradle/issues/3811
@@ -29,7 +29,7 @@ task sample {
2929
}
3030
}
3131

32-
task noConfigAfterExecution {
32+
tasks.register("noConfigAfterExecution") {
3333
dependsOn buildResources
3434
doLast {
3535
println "This should cause an error because we are refferencing " +

buildSrc/src/testKit/elasticsearch.build/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ loggerUsageCheck.enabled = false
2929
// TODO: shouldn't be part of BuildPlugin, should be tested separately
3030
validateNebulaPom.enabled = false
3131

32-
task hello {
32+
tasks.register("hello") {
3333
doFirst {
3434
println "build plugin can be applied"
3535
}

buildSrc/src/testKit/jdk-download/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ project.gradle.projectsEvaluated {
99
repository.setUrl(fakeJdkRepo)
1010
}
1111

12-
task numConfigurations {
12+
tasks.register("numConfigurations") {
1313
doLast {
1414
println "NUM CONFIGS: ${project.configurations.size()}"
1515
}

buildSrc/src/testKit/jdk-download/subproj/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ jdks {
2626
}
2727
}
2828

29-
task getLinuxJdk {
29+
tasks.register("getLinuxJdk") {
3030
dependsOn jdks.linux
3131
doLast {
3232
println "JDK HOME: " + jdks.linux
3333
}
3434
}
3535

36-
task getDarwinJdk {
36+
tasks.register("getDarwinJdk") {
3737
dependsOn jdks.darwin
3838
doLast {
3939
println "JDK HOME: " + jdks.darwin
4040
}
4141
}
4242

43-
task getWindowsJdk {
43+
tasks.register("getWindowsJdk") {
4444
dependsOn jdks.windows
4545
doLast {
4646
println "JDK HOME: " + jdks.windows

0 commit comments

Comments
 (0)