mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 09:44:47 +08:00
Merge pull request #890 from jnizet
* pr/890: Polish "Add support for Gradle's tasksWithType" Add support for Gradle's tasksWithType
This commit is contained in:
@@ -26,6 +26,7 @@ import io.spring.initializr.generator.spring.build.BuildCustomizer;
|
||||
* {@link BuildCustomizer} for Kotlin projects build with Gradle.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
|
||||
|
||||
@@ -39,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.customizeTask("compileKotlin", this::customizeKotlinOptions);
|
||||
build.customizeTask("compileTestKotlin", this::customizeKotlinOptions);
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
this::customizeKotlinOptions);
|
||||
}
|
||||
|
||||
private void customizeKotlinOptions(TaskCustomization compile) {
|
||||
|
@@ -48,11 +48,11 @@ class KotlinGradleBuildCustomizerTests {
|
||||
GradleBuild build = new GradleBuild();
|
||||
new KotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70"))
|
||||
.customize(build);
|
||||
assertThat(build.getTaskCustomizations()).hasSize(2);
|
||||
assertThat(build.getTaskCustomizations()).containsKeys("compileKotlin",
|
||||
"compileTestKotlin");
|
||||
assertKotlinOptions(build.getTaskCustomizations().get("compileKotlin"));
|
||||
assertKotlinOptions(build.getTaskCustomizations().get("compileTestKotlin"));
|
||||
assertThat(build.getImportedTypes())
|
||||
.contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
assertThat(build.getTasksWithTypeCustomizations()).hasSize(1);
|
||||
assertThat(build.getTasksWithTypeCustomizations()).containsKeys("KotlinCompile");
|
||||
assertKotlinOptions(build.getTasksWithTypeCustomizations().get("KotlinCompile"));
|
||||
}
|
||||
|
||||
private void assertKotlinOptions(TaskCustomization compileTask) {
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.1.1.RELEASE'
|
||||
id 'org.jetbrains.kotlin.jvm' version '1.1.1'
|
||||
@@ -21,14 +23,7 @@ dependencies {
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@@ -29,14 +31,7 @@ dependencies {
|
||||
testCompile 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.1.1.RELEASE'
|
||||
id 'org.jetbrains.kotlin.jvm' version '1.1.1'
|
||||
@@ -21,14 +23,7 @@ dependencies {
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
|
@@ -1,3 +1,5 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.1.1.RELEASE'
|
||||
id 'war'
|
||||
@@ -23,14 +25,7 @@ dependencies {
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
jvmTarget = '1.8'
|
||||
|
@@ -19,6 +19,7 @@ package io.spring.initializr.generator.buildsystem.gradle;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -30,10 +31,14 @@ 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.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GradleBuild extends Build {
|
||||
|
||||
@@ -49,6 +54,10 @@ public class GradleBuild extends Build {
|
||||
|
||||
private final Map<String, TaskCustomization> taskCustomizations = new LinkedHashMap<>();
|
||||
|
||||
private final Set<String> importedTypes = new HashSet<>();
|
||||
|
||||
private final Map<String, TaskCustomization> tasksWithTypeCustomizations = new LinkedHashMap<>();
|
||||
|
||||
private final Buildscript buildscript = new Buildscript();
|
||||
|
||||
public GradleBuild(BuildItemResolver buildItemResolver) {
|
||||
@@ -121,6 +130,31 @@ public class GradleBuild extends Build {
|
||||
return Collections.unmodifiableMap(this.configurationCustomizations);
|
||||
}
|
||||
|
||||
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) {
|
||||
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()));
|
||||
}
|
||||
|
||||
public Map<String, TaskCustomization> getTasksWithTypeCustomizations() {
|
||||
return Collections.unmodifiableMap(this.tasksWithTypeCustomizations);
|
||||
}
|
||||
|
||||
public void customizeTask(String taskName, Consumer<TaskCustomization> customizer) {
|
||||
customizer.accept(this.taskCustomizations.computeIfAbsent(taskName,
|
||||
(name) -> new TaskCustomization()));
|
||||
|
@@ -47,10 +47,12 @@ import io.spring.initializr.generator.version.VersionReference;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GradleBuildWriter {
|
||||
|
||||
public void writeTo(IndentingWriter writer, GradleBuild build) throws IOException {
|
||||
writeImports(writer, build);
|
||||
boolean buildScriptWritten = writeBuildscript(writer, build);
|
||||
writePlugins(writer, build, buildScriptWritten);
|
||||
writeProperty(writer, "group", build.getGroup());
|
||||
@@ -61,9 +63,18 @@ public class GradleBuildWriter {
|
||||
writeProperties(writer, build);
|
||||
writeDependencies(writer, build);
|
||||
writeBoms(writer, build);
|
||||
writeTasksWithTypeCustomizations(writer, build);
|
||||
writeTaskCustomizations(writer, build);
|
||||
}
|
||||
|
||||
private void writeImports(IndentingWriter writer, GradleBuild build) {
|
||||
build.getImportedTypes().stream().sorted().forEachOrdered(
|
||||
(importedType) -> writer.println("import " + importedType));
|
||||
if (!build.getImportedTypes().isEmpty()) {
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean writeBuildscript(IndentingWriter writer, GradleBuild build) {
|
||||
List<String> dependencies = build.getBuildscript().getDependencies();
|
||||
Map<String, String> ext = build.getBuildscript().getExt();
|
||||
@@ -276,6 +287,18 @@ public class GradleBuildWriter {
|
||||
return null;
|
||||
}
|
||||
|
||||
private void writeTasksWithTypeCustomizations(IndentingWriter writer,
|
||||
GradleBuild build) {
|
||||
Map<String, GradleBuild.TaskCustomization> tasksWithTypeCustomizations = build
|
||||
.getTasksWithTypeCustomizations();
|
||||
tasksWithTypeCustomizations.forEach((typeName, customization) -> {
|
||||
writer.println();
|
||||
writer.println("tasks.withType(" + typeName + ") {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
private void writeTaskCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, TaskCustomization> taskCustomizations = build.getTaskCustomizations();
|
||||
taskCustomizations.forEach((name, customization) -> {
|
||||
|
@@ -33,6 +33,8 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link GradleBuildWriter}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GradleBuildWriterTests {
|
||||
|
||||
@@ -139,6 +141,36 @@ class GradleBuildWriterTests {
|
||||
assertThat(lines).doesNotContain("repositories {");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesCustomizedWithNestedAssignments()
|
||||
throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
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)
|
||||
.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
|
||||
void gradleBuildWithTaskCustomizedWithInvocations() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
|
Reference in New Issue
Block a user