Ensure Gradle tasks are configured lazily

At present, generated Gradle build scripts that use Groovy DSL configure
tasks eagerly. This is both suboptimal and actually not aligned with the
generated build scripts that use Kotlin DSL, which configures tasks
lazily.

This commit updates `GroovyDslGradleBuildWriter` to ensure tasks are
configured lazily.

See gh-1292
This commit is contained in:
Vedran Pavic 2021-12-30 12:59:19 +01:00 committed by Stephane Nicoll
parent 35e41a8f08
commit 27fc9d4067
17 changed files with 24 additions and 22 deletions

View File

@ -129,7 +129,7 @@ class GradleProjectGenerationConfigurationTests {
" testImplementation 'org.springframework.boot:spring-boot-starter-test'",
"}",
"",
"test {",
"tasks.named('test') {",
" useJUnitPlatform()",
"}"); // @formatter:on
}
@ -150,7 +150,8 @@ class GradleProjectGenerationConfigurationTests {
description.setPlatformVersion(Version.parse("2.2.4.RELEASE"));
description.setLanguage(new JavaLanguage());
ProjectStructure project = this.projectTester.generate(description);
assertThat(project).textFile("build.gradle").lines().containsSequence("test {", " useJUnitPlatform()", "}");
assertThat(project).textFile("build.gradle").lines().containsSequence("tasks.named('test') {",
" useJUnitPlatform()", "}");
}
@Test

View File

@ -25,6 +25,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -25,6 +25,6 @@ dependencyManagement {
}
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -27,6 +27,6 @@ dependencyManagement {
}
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -19,6 +19,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -29,6 +29,6 @@ tasks.withType(KotlinCompile) {
}
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -20,6 +20,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -18,6 +18,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -22,6 +22,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -18,6 +18,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -20,6 +20,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -17,6 +17,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -19,6 +19,6 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -29,6 +29,6 @@ tasks.withType(KotlinCompile) {
}
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -31,6 +31,6 @@ tasks.withType(KotlinCompile) {
}
}
test {
tasks.named('test') {
useJUnitPlatform()
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -190,7 +190,7 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
});
tasks.values().filter((candidate) -> candidate.getType() == null).forEach((task) -> {
writer.println();
writer.println(task.getName() + " {");
writer.println("tasks.named('" + task.getName() + "') {");
writer.indented(() -> writeTaskCustomization(writer, task));
writer.println("}");
});

View File

@ -170,7 +170,8 @@ class GroovyDslGradleBuildWriterTests {
task.invoke("dependsOn", "test");
});
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("asciidoctor {", " inputs.dir snippetsDir", " dependsOn test", "}");
assertThat(lines).containsSequence("tasks.named('asciidoctor') {", " inputs.dir snippetsDir",
" dependsOn test", "}");
}
@Test
@ -178,7 +179,7 @@ class GroovyDslGradleBuildWriterTests {
GradleBuild build = new GradleBuild();
build.tasks().customize("test", (task) -> task.invoke("myMethod"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("test {", " myMethod()", "}");
assertThat(lines).containsSequence("tasks.named('test') {", " myMethod()", "}");
}
@Test
@ -189,7 +190,7 @@ class GroovyDslGradleBuildWriterTests {
task.attribute("kotlinOptions.jvmTarget", "'1.8'");
});
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("compileKotlin {",
assertThat(lines).containsSequence("tasks.named('compileKotlin') {",
" kotlinOptions.freeCompilerArgs = ['-Xjsr305=strict']", " kotlinOptions.jvmTarget = '1.8'", "}");
}
@ -202,7 +203,7 @@ class GroovyDslGradleBuildWriterTests {
kotlinOptions.attribute("jvmTarget", "'1.8'");
}));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("compileKotlin {", " kotlinOptions {",
assertThat(lines).containsSequence("tasks.named('compileKotlin') {", " kotlinOptions {",
" freeCompilerArgs = ['-Xjsr305=strict']", " jvmTarget = '1.8'", " }", "}");
}