mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-25 21:22:58 +08:00
Polish "Add support for Gradle's tasksWithType"
Closes gh-890 Co-authored-by: Stephane Nicoll <snicoll@pivotal.io>
This commit is contained in:
committed by
Stephane Nicoll
parent
0eeb85697e
commit
0f645c7143
@@ -40,8 +40,8 @@ class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
|
||||
public void customize(GradleBuild build) {
|
||||
build.addPlugin("org.jetbrains.kotlin.jvm", this.settings.getVersion());
|
||||
build.addPlugin("org.jetbrains.kotlin.plugin.spring", this.settings.getVersion());
|
||||
build.addImportedType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
build.customizeTasksWithType("KotlinCompile", this::customizeKotlinOptions);
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
this::customizeKotlinOptions);
|
||||
}
|
||||
|
||||
private void customizeKotlinOptions(TaskCustomization compile) {
|
||||
|
||||
@@ -31,6 +31,9 @@ import java.util.function.Consumer;
|
||||
import io.spring.initializr.generator.buildsystem.Build;
|
||||
import io.spring.initializr.generator.buildsystem.BuildItemResolver;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Gradle build configuration for a project.
|
||||
*
|
||||
@@ -127,17 +130,24 @@ public class GradleBuild extends Build {
|
||||
return Collections.unmodifiableMap(this.configurationCustomizations);
|
||||
}
|
||||
|
||||
public void addImportedType(String type) {
|
||||
this.importedTypes.add(type);
|
||||
}
|
||||
|
||||
public Set<String> getImportedTypes() {
|
||||
return Collections.unmodifiableSet(this.importedTypes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize tasks matching a given type.
|
||||
* @param typeName the name of type. Can use the short form for well-known types such
|
||||
* as {@code JavaCompile}, use a fully qualified name if an import is required
|
||||
* @param customizer a callback to customize tasks matching that type
|
||||
*/
|
||||
public void customizeTasksWithType(String typeName,
|
||||
Consumer<TaskCustomization> customizer) {
|
||||
customizer.accept(this.tasksWithTypeCustomizations.computeIfAbsent(typeName,
|
||||
String packageName = ClassUtils.getPackageName(typeName);
|
||||
if (!StringUtils.isEmpty(packageName)) {
|
||||
this.importedTypes.add(typeName);
|
||||
}
|
||||
String shortName = ClassUtils.getShortName(typeName);
|
||||
customizer.accept(this.tasksWithTypeCustomizations.computeIfAbsent(shortName,
|
||||
(name) -> new TaskCustomization()));
|
||||
}
|
||||
|
||||
|
||||
@@ -291,7 +291,6 @@ public class GradleBuildWriter {
|
||||
GradleBuild build) {
|
||||
Map<String, GradleBuild.TaskCustomization> tasksWithTypeCustomizations = build
|
||||
.getTasksWithTypeCustomizations();
|
||||
|
||||
tasksWithTypeCustomizations.forEach((typeName, customization) -> {
|
||||
writer.println();
|
||||
writer.println("tasks.withType(" + typeName + ") {");
|
||||
|
||||
@@ -34,24 +34,10 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GradleBuildWriterTests {
|
||||
|
||||
@Test
|
||||
void gradleBuildWithImports() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addImportedType(
|
||||
"org.springframework.boot.gradle.tasks.buildinfo.BuildInfo");
|
||||
build.addImportedType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
// same import added twice on purpose
|
||||
build.addImportedType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines.subList(0, 3)).containsExactly(
|
||||
"import org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
"import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCoordinates() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
@@ -159,16 +145,30 @@ class GradleBuildWriterTests {
|
||||
void gradleBuildWithTaskWithTypesCustomizedWithNestedAssignments()
|
||||
throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("KotlinCompile", (task) -> {
|
||||
task.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
kotlinOptions.set("freeCompilerArgs", "['-Xjsr305=strict']");
|
||||
kotlinOptions.set("jvmTarget", "'1.8'");
|
||||
});
|
||||
});
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
(task) -> task.nested("kotlinOptions", (kotlinOptions) -> kotlinOptions
|
||||
.set("freeCompilerArgs", "['-Xjsr305=strict']")));
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
(task) -> task.nested("kotlinOptions",
|
||||
(kotlinOptions) -> kotlinOptions.set("jvmTarget", "'1.8'")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.withType(KotlinCompile) {",
|
||||
" kotlinOptions {", " freeCompilerArgs = ['-Xjsr305=strict']",
|
||||
" jvmTarget = '1.8'", " }", "}");
|
||||
assertThat(lines)
|
||||
.containsOnlyOnce(
|
||||
"import org.jetbrains.kotlin.gradle.tasks.KotlinCompile")
|
||||
.containsSequence("tasks.withType(KotlinCompile) {",
|
||||
" kotlinOptions {",
|
||||
" freeCompilerArgs = ['-Xjsr305=strict']",
|
||||
" jvmTarget = '1.8'", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesAndShortTypes() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("JavaCompile",
|
||||
(javaCompile) -> javaCompile.set("options.fork", "true"));
|
||||
assertThat(generateBuild(build)).doesNotContain("import JavaCompile")
|
||||
.containsSequence("tasks.withType(JavaCompile) {",
|
||||
" options.fork = true", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user