mirror of
https://gitee.com/dcren/initializr.git
synced 2026-02-25 21:22:58 +08:00
Harmonize Gradle build to use containers
See gh-996
This commit is contained in:
@@ -35,7 +35,7 @@ public class GradleAnnotationProcessorScopeBuildCustomizer implements BuildCusto
|
||||
boolean annotationProcessorUsed = build.dependencies().items()
|
||||
.anyMatch((dependency) -> dependency.getScope() == DependencyScope.ANNOTATION_PROCESSOR);
|
||||
if (annotationProcessorUsed) {
|
||||
build.customizeConfiguration("compileOnly",
|
||||
build.configurations().customize("compileOnly",
|
||||
(configuration) -> configuration.extendsFrom("annotationProcessor"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ public class GradleConfigurationBuildCustomizer implements BuildCustomizer<Gradl
|
||||
.anyMatch((dependency) -> DependencyScope.PROVIDED_RUNTIME.equals(dependency.getScope()));
|
||||
boolean war = build.plugins().values().anyMatch((plugin) -> plugin.getId().equals("war"));
|
||||
if (providedRuntimeUsed && !war) {
|
||||
build.addConfiguration("providedRuntime");
|
||||
build.configurations().add("providedRuntime");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -213,7 +213,7 @@ public class GradleProjectGenerationConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnPlatformVersion("2.2.0.M3")
|
||||
BuildCustomizer<GradleBuild> testTaskContributor() {
|
||||
return (build) -> build.customizeTask("test", (test) -> test.invoke("useJUnitPlatform"));
|
||||
return (build) -> build.tasks().customize("test", (test) -> test.invoke("useJUnitPlatform"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -246,7 +246,7 @@ public class GradleProjectGenerationConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnPlatformVersion("2.2.0.M3")
|
||||
BuildCustomizer<GradleBuild> testTaskContributor() {
|
||||
return (build) -> build.customizeTasksWithType("Test", (test) -> test.invoke("useJUnitPlatform"));
|
||||
return (build) -> build.tasks().customizeWithType("Test", (test) -> test.invoke("useJUnitPlatform"));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -18,7 +18,7 @@ package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import io.spring.initializr.generator.spring.build.BuildCustomizer;
|
||||
|
||||
/**
|
||||
@@ -33,12 +33,12 @@ class GroovyDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, TaskCustomization compile) {
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) {
|
||||
compile.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
String compilerArgs = settings.getCompilerArgs().stream().map((arg) -> "'" + arg + "'")
|
||||
.collect(Collectors.joining(", "));
|
||||
kotlinOptions.set("freeCompilerArgs", "[" + compilerArgs + "]");
|
||||
kotlinOptions.set("jvmTarget", "'" + settings.getJvmTarget() + "'");
|
||||
kotlinOptions.attribute("freeCompilerArgs", "[" + compilerArgs + "]");
|
||||
kotlinOptions.attribute("jvmTarget", "'" + settings.getJvmTarget() + "'");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import io.spring.initializr.generator.spring.build.BuildCustomizer;
|
||||
|
||||
/**
|
||||
@@ -33,12 +33,12 @@ class KotlinDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, TaskCustomization compile) {
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) {
|
||||
compile.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
String compilerArgs = settings.getCompilerArgs().stream().map((arg) -> "\"" + arg + "\"")
|
||||
.collect(Collectors.joining(", "));
|
||||
kotlinOptions.set("freeCompilerArgs", "listOf(" + compilerArgs + ")");
|
||||
kotlinOptions.set("jvmTarget", "\"" + settings.getJvmTarget() + "\"");
|
||||
kotlinOptions.attribute("freeCompilerArgs", "listOf(" + compilerArgs + ")");
|
||||
kotlinOptions.attribute("jvmTarget", "\"" + settings.getJvmTarget() + "\"");
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import io.spring.initializr.generator.spring.build.BuildCustomizer;
|
||||
|
||||
/**
|
||||
@@ -41,10 +41,10 @@ abstract class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuil
|
||||
build.plugins().add("org.jetbrains.kotlin.jvm", (plugin) -> plugin.setVersion(this.settings.getVersion()));
|
||||
build.plugins().add("org.jetbrains.kotlin.plugin.spring",
|
||||
(plugin) -> plugin.setVersion(this.settings.getVersion()));
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
build.tasks().customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
(compile) -> customizeKotlinOptions(this.settings, compile));
|
||||
}
|
||||
|
||||
protected abstract void customizeKotlinOptions(KotlinProjectSettings settings, TaskCustomization compile);
|
||||
protected abstract void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile);
|
||||
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package io.spring.initializr.generator.spring.build.gradle;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.ConfigurationCustomization;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -36,9 +35,10 @@ class GradleAnnotationProcessorScopeBuildCustomizerTests {
|
||||
build.dependencies().add("lib", "com.example", "lib", DependencyScope.COMPILE);
|
||||
build.dependencies().add("ap", "com.example", "model-generator", DependencyScope.ANNOTATION_PROCESSOR);
|
||||
customize(build);
|
||||
assertThat(build.getConfigurationCustomizations()).containsOnlyKeys("compileOnly");
|
||||
ConfigurationCustomization compileOnly = build.getConfigurationCustomizations().get("compileOnly");
|
||||
assertThat(compileOnly.getExtendsFrom()).containsOnly("annotationProcessor");
|
||||
assertThat(build.configurations().customizations()).hasOnlyOneElementSatisfying((configuration) -> {
|
||||
assertThat(configuration.getName()).isEqualTo("compileOnly");
|
||||
assertThat(configuration.getExtendsFrom()).containsOnly("annotationProcessor");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -47,7 +47,7 @@ class GradleAnnotationProcessorScopeBuildCustomizerTests {
|
||||
build.dependencies().add("lib", "com.example", "lib", DependencyScope.COMPILE);
|
||||
build.dependencies().add("another", "com.example", "another", DependencyScope.RUNTIME);
|
||||
customize(build);
|
||||
assertThat(build.getConfigurationCustomizations()).isEmpty();
|
||||
assertThat(build.configurations().isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
private void customize(GradleBuild build) {
|
||||
|
||||
@@ -35,7 +35,7 @@ class GradleConfigurationBuildCustomizerTests {
|
||||
build.dependencies().add("lib", "com.example", "lib", DependencyScope.COMPILE);
|
||||
build.dependencies().add("servlet", "javax.servlet", "servlet-api", DependencyScope.PROVIDED_RUNTIME);
|
||||
customize(build);
|
||||
assertThat(build.getConfigurations()).containsOnly("providedRuntime");
|
||||
assertThat(build.configurations().names()).containsOnly("providedRuntime");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -45,7 +45,7 @@ class GradleConfigurationBuildCustomizerTests {
|
||||
build.dependencies().add("lib", "com.example", "lib", DependencyScope.COMPILE);
|
||||
build.dependencies().add("servlet", "javax.servlet", "servlet-api", DependencyScope.PROVIDED_RUNTIME);
|
||||
customize(build);
|
||||
assertThat(build.getConfigurationCustomizations()).isEmpty();
|
||||
assertThat(build.configurations().customizations()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -54,7 +54,7 @@ class GradleConfigurationBuildCustomizerTests {
|
||||
build.dependencies().add("lib", "com.example", "lib", DependencyScope.COMPILE);
|
||||
build.dependencies().add("another", "com.example", "another", DependencyScope.RUNTIME);
|
||||
customize(build);
|
||||
assertThat(build.getConfigurationCustomizations()).isEmpty();
|
||||
assertThat(build.configurations().customizations()).isEmpty();
|
||||
}
|
||||
|
||||
private void customize(GradleBuild build) {
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import org.assertj.core.groups.Tuple;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -44,21 +44,22 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
|
||||
void kotlinCompilationTasksAreCustomized() {
|
||||
GradleBuild build = new GradleBuild();
|
||||
new GroovyDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build);
|
||||
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"));
|
||||
assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
assertThat(build.tasks().values()).hasOnlyOneElementSatisfying((task) -> {
|
||||
assertThat(task.getName()).isEqualTo("KotlinCompile");
|
||||
assertKotlinOptions(task);
|
||||
});
|
||||
}
|
||||
|
||||
private void assertKotlinOptions(TaskCustomization compileTask) {
|
||||
assertThat(compileTask.getAssignments()).isEmpty();
|
||||
private void assertKotlinOptions(GradleTask compileTask) {
|
||||
assertThat(compileTask.getAttributes()).isEmpty();
|
||||
assertThat(compileTask.getInvocations()).isEmpty();
|
||||
assertThat(compileTask.getNested()).hasSize(1);
|
||||
TaskCustomization kotlinOptions = compileTask.getNested().get("kotlinOptions");
|
||||
GradleTask kotlinOptions = compileTask.getNested().get("kotlinOptions");
|
||||
assertThat(kotlinOptions.getInvocations()).hasSize(0);
|
||||
assertThat(kotlinOptions.getNested()).hasSize(0);
|
||||
assertThat(kotlinOptions.getAssignments()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAssignments()).containsEntry("freeCompilerArgs", "['-Xjsr305=strict']")
|
||||
assertThat(kotlinOptions.getAttributes()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "['-Xjsr305=strict']")
|
||||
.containsEntry("jvmTarget", "'1.8'");
|
||||
}
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import org.assertj.core.groups.Tuple;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -43,21 +43,22 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
|
||||
void kotlinCompilationTasksAreCustomized() {
|
||||
GradleBuild build = new GradleBuild();
|
||||
new KotlinDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build);
|
||||
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"));
|
||||
assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
assertThat(build.tasks().values()).hasOnlyOneElementSatisfying((task) -> {
|
||||
assertThat(task.getName()).isEqualTo("KotlinCompile");
|
||||
assertKotlinOptions(task);
|
||||
});
|
||||
}
|
||||
|
||||
private void assertKotlinOptions(TaskCustomization compileTask) {
|
||||
assertThat(compileTask.getAssignments()).isEmpty();
|
||||
private void assertKotlinOptions(GradleTask compileTask) {
|
||||
assertThat(compileTask.getAttributes()).isEmpty();
|
||||
assertThat(compileTask.getInvocations()).isEmpty();
|
||||
assertThat(compileTask.getNested()).hasSize(1);
|
||||
TaskCustomization kotlinOptions = compileTask.getNested().get("kotlinOptions");
|
||||
GradleTask kotlinOptions = compileTask.getNested().get("kotlinOptions");
|
||||
assertThat(kotlinOptions.getInvocations()).hasSize(0);
|
||||
assertThat(kotlinOptions.getNested()).hasSize(0);
|
||||
assertThat(kotlinOptions.getAssignments()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAssignments()).containsEntry("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")
|
||||
assertThat(kotlinOptions.getAttributes()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")
|
||||
.containsEntry("jvmTarget", "\"1.8\"");
|
||||
}
|
||||
|
||||
|
||||
@@ -16,24 +16,12 @@
|
||||
|
||||
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;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.Build;
|
||||
import io.spring.initializr.generator.buildsystem.BuildItemResolver;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSettings.Builder;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Gradle build configuration for a project.
|
||||
*
|
||||
@@ -46,17 +34,11 @@ public class GradleBuild extends Build {
|
||||
|
||||
private final GradlePluginContainer plugins = new GradlePluginContainer();
|
||||
|
||||
private final List<String> configurations = new ArrayList<>();
|
||||
private final GradleConfigurationContainer configurations = new GradleConfigurationContainer();
|
||||
|
||||
private final Map<String, ConfigurationCustomization> configurationCustomizations = new LinkedHashMap<>();
|
||||
private final GradleTaskContainer tasks = new GradleTaskContainer();
|
||||
|
||||
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();
|
||||
private final GradleBuildscript.Builder buildscript = new GradleBuildscript.Builder();
|
||||
|
||||
public GradleBuild(BuildItemResolver buildItemResolver) {
|
||||
super(buildItemResolver);
|
||||
@@ -80,168 +62,20 @@ public class GradleBuild extends Build {
|
||||
return this.plugins;
|
||||
}
|
||||
|
||||
public void buildscript(Consumer<Buildscript> customizer) {
|
||||
customizer.accept(this.buildscript);
|
||||
public GradleConfigurationContainer configurations() {
|
||||
return this.configurations;
|
||||
}
|
||||
|
||||
public Buildscript getBuildscript() {
|
||||
return this.buildscript;
|
||||
}
|
||||
|
||||
public void customizeConfiguration(String configurationName, Consumer<ConfigurationCustomization> customizer) {
|
||||
customizer.accept(this.configurationCustomizations.computeIfAbsent(configurationName,
|
||||
(name) -> new ConfigurationCustomization()));
|
||||
}
|
||||
|
||||
public void addConfiguration(String configurationName) {
|
||||
this.configurations.add(configurationName);
|
||||
}
|
||||
|
||||
public Map<String, ConfigurationCustomization> getConfigurationCustomizations() {
|
||||
return Collections.unmodifiableMap(this.configurationCustomizations);
|
||||
}
|
||||
|
||||
public List<String> getConfigurations() {
|
||||
return Collections.unmodifiableList(this.configurations);
|
||||
}
|
||||
|
||||
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()));
|
||||
}
|
||||
|
||||
public Map<String, TaskCustomization> getTaskCustomizations() {
|
||||
return Collections.unmodifiableMap(this.taskCustomizations);
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code buildscript} block in the {@code build.gradle} file.
|
||||
*/
|
||||
public static class Buildscript {
|
||||
|
||||
private final List<String> dependencies = new ArrayList<>();
|
||||
|
||||
private final Map<String, String> ext = new LinkedHashMap<>();
|
||||
|
||||
public Buildscript dependency(String coordinates) {
|
||||
this.dependencies.add(coordinates);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Buildscript ext(String key, String value) {
|
||||
this.ext.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<String> getDependencies() {
|
||||
return Collections.unmodifiableList(this.dependencies);
|
||||
}
|
||||
|
||||
public Map<String, String> getExt() {
|
||||
return Collections.unmodifiableMap(this.ext);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Customization of a configuration in a Gradle build.
|
||||
*/
|
||||
public static class ConfigurationCustomization {
|
||||
|
||||
private final Set<String> extendsFrom = new LinkedHashSet<>();
|
||||
|
||||
public void extendsFrom(String configurationName) {
|
||||
this.extendsFrom.add(configurationName);
|
||||
}
|
||||
|
||||
public Set<String> getExtendsFrom() {
|
||||
return Collections.unmodifiableSet(this.extendsFrom);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Customization of a task in a Gradle build.
|
||||
*/
|
||||
public static class TaskCustomization {
|
||||
|
||||
private final List<Invocation> invocations = new ArrayList<>();
|
||||
|
||||
private final Map<String, String> assignments = new LinkedHashMap<>();
|
||||
|
||||
private final Map<String, TaskCustomization> nested = new LinkedHashMap<>();
|
||||
|
||||
public void nested(String property, Consumer<TaskCustomization> customizer) {
|
||||
customizer.accept(this.nested.computeIfAbsent(property, (name) -> new TaskCustomization()));
|
||||
}
|
||||
|
||||
public Map<String, TaskCustomization> getNested() {
|
||||
return this.nested;
|
||||
}
|
||||
|
||||
public void invoke(String target, String... arguments) {
|
||||
this.invocations.add(new Invocation(target, Arrays.asList(arguments)));
|
||||
}
|
||||
|
||||
public List<Invocation> getInvocations() {
|
||||
return Collections.unmodifiableList(this.invocations);
|
||||
}
|
||||
|
||||
public void set(String target, String value) {
|
||||
this.assignments.put(target, value);
|
||||
}
|
||||
|
||||
public Map<String, String> getAssignments() {
|
||||
return Collections.unmodifiableMap(this.assignments);
|
||||
}
|
||||
|
||||
/**
|
||||
* An invocation of a method that customizes a task.
|
||||
*/
|
||||
public static class Invocation {
|
||||
|
||||
private final String target;
|
||||
|
||||
private final List<String> arguments;
|
||||
|
||||
Invocation(String target, List<String> arguments) {
|
||||
this.target = target;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
public List<String> getArguments() {
|
||||
return this.arguments;
|
||||
public GradleTaskContainer tasks() {
|
||||
return this.tasks;
|
||||
}
|
||||
|
||||
public void buildscript(Consumer<GradleBuildscript.Builder> buildscript) {
|
||||
buildscript.accept(this.buildscript);
|
||||
}
|
||||
|
||||
public GradleBuildscript getBuildscript() {
|
||||
return this.buildscript.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,8 +38,6 @@ import io.spring.initializr.generator.buildsystem.DependencyContainer;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.buildsystem.PropertyContainer;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.ConfigurationCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
|
||||
@@ -56,26 +54,27 @@ public abstract class GradleBuildWriter {
|
||||
|
||||
public final void writeTo(IndentingWriter writer, GradleBuild build) throws IOException {
|
||||
GradleBuildSettings settings = build.getSettings();
|
||||
writeImports(writer, build);
|
||||
writeImports(writer, build.tasks());
|
||||
writeBuildscript(writer, build);
|
||||
writePlugins(writer, build);
|
||||
writeProperty(writer, "group", settings.getGroup());
|
||||
writeProperty(writer, "version", settings.getVersion());
|
||||
writeJavaSourceCompatibility(writer, settings);
|
||||
writer.println();
|
||||
writeConfigurations(writer, build);
|
||||
writeConfigurations(writer, build.configurations());
|
||||
writeRepositories(writer, build);
|
||||
writeProperties(writer, build.properties());
|
||||
writeDependencies(writer, build);
|
||||
writeBoms(writer, build);
|
||||
writeTasksWithTypeCustomizations(writer, build);
|
||||
writeTaskCustomizations(writer, build);
|
||||
writeTasks(writer, build.tasks());
|
||||
// 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()) {
|
||||
private void writeImports(IndentingWriter writer, GradleTaskContainer tasks) {
|
||||
List<String> imports = tasks.importedTypes().sorted().collect(Collectors.toList());
|
||||
imports.forEach((importedType) -> writer.println("import " + importedType));
|
||||
if (!imports.isEmpty()) {
|
||||
writer.println();
|
||||
}
|
||||
}
|
||||
@@ -91,10 +90,7 @@ public abstract class GradleBuildWriter {
|
||||
|
||||
protected abstract void writeJavaSourceCompatibility(IndentingWriter writer, GradleBuildSettings settings);
|
||||
|
||||
protected abstract void writeConfigurations(IndentingWriter writer, GradleBuild build);
|
||||
|
||||
protected abstract void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
ConfigurationCustomization configurationCustomization, List<String> customConfigurations);
|
||||
protected abstract void writeConfigurations(IndentingWriter writer, GradleConfigurationContainer configurations);
|
||||
|
||||
protected final void writeRepositories(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "repositories", build.repositories().items().collect(Collectors.toList()),
|
||||
@@ -190,21 +186,19 @@ public abstract class GradleBuildWriter {
|
||||
|
||||
protected abstract String bomAsString(BillOfMaterials bom);
|
||||
|
||||
protected abstract void writeTasksWithTypeCustomizations(IndentingWriter writer, GradleBuild build);
|
||||
protected abstract void writeTasks(IndentingWriter writer, GradleTaskContainer tasks);
|
||||
|
||||
protected abstract void writeTaskCustomizations(IndentingWriter writer, GradleBuild build);
|
||||
|
||||
protected final void writeTaskCustomization(IndentingWriter writer, TaskCustomization customization) {
|
||||
writeCollection(writer, customization.getInvocations(), this::invocationAsString);
|
||||
writeMap(writer, customization.getAssignments(), (key, value) -> key + " = " + value);
|
||||
customization.getNested().forEach((property, nestedCustomization) -> {
|
||||
protected final void writeTaskCustomization(IndentingWriter writer, GradleTask task) {
|
||||
writeCollection(writer, task.getInvocations(), this::invocationAsString);
|
||||
writeMap(writer, task.getAttributes(), (key, value) -> key + " = " + value);
|
||||
task.getNested().forEach((property, nestedCustomization) -> {
|
||||
writer.println(property + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, nestedCustomization));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
protected abstract String invocationAsString(TaskCustomization.Invocation invocation);
|
||||
protected abstract String invocationAsString(GradleTask.Invocation invocation);
|
||||
|
||||
protected final <T> void writeNestedCollection(IndentingWriter writer, String name, Collection<T> collection,
|
||||
Function<T, String> itemToStringConverter) {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@code buildscript} section of a {@link GradleBuild}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class GradleBuildscript {
|
||||
|
||||
private final List<String> dependencies;
|
||||
|
||||
private final Map<String, String> ext;
|
||||
|
||||
protected GradleBuildscript(Builder builder) {
|
||||
this.dependencies = Collections.unmodifiableList(new ArrayList<>(builder.dependencies));
|
||||
this.ext = Collections.unmodifiableMap(new LinkedHashMap<>(builder.ext));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the dependencies required by this build.
|
||||
* @return the dependencies.
|
||||
*/
|
||||
public List<String> getDependencies() {
|
||||
return this.dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return additional properties this build uses.
|
||||
* @return build properties
|
||||
*/
|
||||
public Map<String, String> getExt() {
|
||||
return this.ext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builder for {@link GradleBuildscript}.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private final List<String> dependencies = new ArrayList<>();
|
||||
|
||||
private final Map<String, String> ext = new LinkedHashMap<>();
|
||||
|
||||
public Builder dependency(String coordinates) {
|
||||
this.dependencies.add(coordinates);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder ext(String key, String value) {
|
||||
this.ext.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GradleBuildscript build() {
|
||||
return new GradleBuildscript(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Custom {@code gradle} configuration.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class GradleConfiguration {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<String> extendsFrom;
|
||||
|
||||
protected GradleConfiguration(Builder builder) {
|
||||
this.name = builder.name;
|
||||
this.extendsFrom = Collections.unmodifiableSet(new LinkedHashSet<>(builder.extendsFrom));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the configuration.
|
||||
* @return the configuration name
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the names that this configuration should extend from.
|
||||
* @return the names that this configuration should extend from
|
||||
*/
|
||||
public Set<String> getExtendsFrom() {
|
||||
return this.extendsFrom;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private final String name;
|
||||
|
||||
private Set<String> extendsFrom = new LinkedHashSet<>();
|
||||
|
||||
public Builder(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Builder extendsFrom(String configurationName) {
|
||||
this.extendsFrom.add(configurationName);
|
||||
return this;
|
||||
}
|
||||
|
||||
public GradleConfiguration build() {
|
||||
return new GradleConfiguration(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleConfiguration.Builder;
|
||||
|
||||
/**
|
||||
* A container for custom configuration and {@link GradleConfiguration configuration
|
||||
* customizations}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class GradleConfigurationContainer {
|
||||
|
||||
private final Set<String> configurations = new LinkedHashSet<>();
|
||||
|
||||
private final Map<String, GradleConfiguration.Builder> configurationCustomizations = new LinkedHashMap<>();
|
||||
|
||||
/**
|
||||
* Specify if this container is empty.
|
||||
* @return {@code true} if no custom configuration is registered or no configuration
|
||||
* is customized
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return this.configurations.isEmpty() && this.configurationCustomizations.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if this container has a configuration with the specified {@code name}.
|
||||
* @param name the name of a configuration
|
||||
* @return {@code true} if a configuration with the specified {@code name} exists
|
||||
*/
|
||||
public boolean has(String name) {
|
||||
return this.configurations.contains(name) || this.configurationCustomizations.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configuration names that should be registered.
|
||||
* @return the configuration names
|
||||
*/
|
||||
public Stream<String> names() {
|
||||
return this.configurations.stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the configuration that should be customized.
|
||||
* @return the configuration customizations
|
||||
*/
|
||||
public Stream<GradleConfiguration> customizations() {
|
||||
return this.configurationCustomizations.values().stream().map(Builder::build);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@code configuration} with the specified name.
|
||||
* @param name the name of a configuration
|
||||
*/
|
||||
public void add(String name) {
|
||||
this.configurations.add(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize an existing {@code configuration} with the specified {@code name}. If the
|
||||
* configuration has already been customized, the consumer can be used to further tune
|
||||
* the existing configuration customization.
|
||||
* @param name the name of the configuration to customize
|
||||
* @param configuration a {@link Consumer} to customize the
|
||||
* {@link GradleConfiguration}
|
||||
*/
|
||||
public void customize(String name, Consumer<Builder> configuration) {
|
||||
Builder builder = this.configurationCustomizations.computeIfAbsent(name, Builder::new);
|
||||
configuration.accept(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the configuration with the specified {@code name}.
|
||||
* @param name the name of a configuration to register or customization
|
||||
* @return {@code true} if such a configuration was registered, {@code false}
|
||||
* otherwise
|
||||
*/
|
||||
public boolean remove(String name) {
|
||||
if (this.configurations.remove(name)) {
|
||||
return true;
|
||||
}
|
||||
return this.configurationCustomizations.remove(name) != null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A customization for a Gradle task.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class GradleTask {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String type;
|
||||
|
||||
private final Map<String, String> attributes;
|
||||
|
||||
private final List<Invocation> invocations;
|
||||
|
||||
private final Map<String, GradleTask> nested;
|
||||
|
||||
protected GradleTask(Builder builder) {
|
||||
this.name = builder.name;
|
||||
this.type = builder.type;
|
||||
this.attributes = Collections.unmodifiableMap(new LinkedHashMap<>(builder.attributes));
|
||||
this.invocations = Collections.unmodifiableList(new ArrayList<>(builder.invocations));
|
||||
this.nested = Collections.unmodifiableMap(resolve(builder.nested));
|
||||
}
|
||||
|
||||
private static Map<String, GradleTask> resolve(Map<String, Builder> tasks) {
|
||||
Map<String, GradleTask> result = new LinkedHashMap<>();
|
||||
tasks.forEach((name, builder) -> result.put(name, builder.build()));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the task.
|
||||
* @return the task name
|
||||
*/
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the type that this task is associated with or {@code null} if this task has
|
||||
* no type.
|
||||
* @return the associated task type or {@code null}
|
||||
*/
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the attributes that should be set for this task.
|
||||
* @return task attributes
|
||||
*/
|
||||
public Map<String, String> getAttributes() {
|
||||
return this.attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link Invocation invocations} of this task.
|
||||
* @return task invocations
|
||||
*/
|
||||
public List<Invocation> getInvocations() {
|
||||
return this.invocations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return nested {@link GradleTask tasks}.
|
||||
* @return nested tasks
|
||||
*/
|
||||
public Map<String, GradleTask> getNested() {
|
||||
return this.nested;
|
||||
}
|
||||
|
||||
/**
|
||||
* A builder for {@link GradleTask}.
|
||||
*/
|
||||
public static class Builder {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final String type;
|
||||
|
||||
private final Map<String, String> attributes = new LinkedHashMap<>();
|
||||
|
||||
private final List<Invocation> invocations = new ArrayList<>();
|
||||
|
||||
private final Map<String, Builder> nested = new LinkedHashMap<>();
|
||||
|
||||
protected Builder(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
protected Builder(String name) {
|
||||
this(name, null);
|
||||
}
|
||||
|
||||
public void attribute(String target, String value) {
|
||||
this.attributes.put(target, value);
|
||||
}
|
||||
|
||||
public void invoke(String target, String... arguments) {
|
||||
this.invocations.add(new Invocation(target, Arrays.asList(arguments)));
|
||||
}
|
||||
|
||||
public void nested(String property, Consumer<Builder> customizer) {
|
||||
customizer.accept(this.nested.computeIfAbsent(property, (name) -> new Builder(property)));
|
||||
}
|
||||
|
||||
public GradleTask build() {
|
||||
return new GradleTask(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* An invocation of a method that customizes a task.
|
||||
*/
|
||||
public static class Invocation {
|
||||
|
||||
private final String target;
|
||||
|
||||
private final List<String> arguments;
|
||||
|
||||
Invocation(String target, List<String> arguments) {
|
||||
this.target = target;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
public String getTarget() {
|
||||
return this.target;
|
||||
}
|
||||
|
||||
public List<String> getArguments() {
|
||||
return this.arguments;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Builder;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* A container for {@link GradleTask Gradle tasks}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class GradleTaskContainer {
|
||||
|
||||
private final Map<String, Builder> tasks = new LinkedHashMap<>();
|
||||
|
||||
private final Set<String> importedTypes = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Specify if this container is empty.
|
||||
* @return {@code true} if no task is registered
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return this.tasks.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify if this container has a task customization with the specified {@code name}.
|
||||
* @param name the name of a task
|
||||
* @return {@code true} if a customization for a task with the specified {@code name}
|
||||
* exists
|
||||
*/
|
||||
public boolean has(String name) {
|
||||
return this.tasks.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link GradleTask Gradle tasks} to customize.
|
||||
* @return the gradle tasks
|
||||
*/
|
||||
public Stream<GradleTask> values() {
|
||||
return this.tasks.values().stream().map(Builder::build);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a {@link GradleTask} with the specified task name.
|
||||
* @param task the name or type
|
||||
* @return the matching gradle task or {@code null}
|
||||
*/
|
||||
public GradleTask get(String task) {
|
||||
Builder builder = this.tasks.get(task);
|
||||
return (builder != null) ? builder.build() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the fully qualified name of types to import.
|
||||
* @return the imported types
|
||||
*/
|
||||
public Stream<String> importedTypes() {
|
||||
return this.importedTypes.stream();
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize a task with the specified name. If the task has already been customized,
|
||||
* the consumer can be used to further tune the existing task.
|
||||
* @param name the name of a task
|
||||
* @param task a callback to customize the task
|
||||
*/
|
||||
public void customize(String name, Consumer<Builder> task) {
|
||||
task.accept(this.tasks.computeIfAbsent(name, Builder::new));
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize a task matching a given type. If the task has already been customized,
|
||||
* the consumer can be used to further tune the existing task.
|
||||
* @param type 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 task a callback to customize the task
|
||||
*/
|
||||
public void customizeWithType(String type, Consumer<Builder> task) {
|
||||
String packageName = ClassUtils.getPackageName(type);
|
||||
if (!StringUtils.isEmpty(packageName)) {
|
||||
this.importedTypes.add(type);
|
||||
}
|
||||
String shortName = ClassUtils.getShortName(type);
|
||||
task.accept(this.tasks.computeIfAbsent(shortName, (name) -> new Builder(name, type)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the task with the specified {@code name}.
|
||||
* @param name the name of the task
|
||||
* @return {@code true} if such a task was registered, {@code false} otherwise
|
||||
*/
|
||||
public boolean remove(String name) {
|
||||
return this.tasks.remove(name) != null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,7 +25,6 @@ import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.ConfigurationCustomization;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
@@ -92,15 +91,6 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
|
||||
writeProperty(writer, "sourceCompatibility", settings.getSourceCompatibility());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
GradleBuild.ConfigurationCustomization configurationCustomization, List<String> customConfigurations) {
|
||||
writer.println(configurationName + " {");
|
||||
writer.indented(() -> writer.println(
|
||||
String.format("extendsFrom %s", String.join(", ", configurationCustomization.getExtendsFrom()))));
|
||||
writer.println("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String repositoryAsString(MavenRepository repository) {
|
||||
if (MavenRepository.MAVEN_CENTRAL.equals(repository)) {
|
||||
@@ -120,22 +110,26 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfigurations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, ConfigurationCustomization> configurationCustomizations = build.getConfigurationCustomizations();
|
||||
List<String> configurations = build.getConfigurations();
|
||||
if (configurations.isEmpty() && configurationCustomizations.isEmpty()) {
|
||||
protected void writeConfigurations(IndentingWriter writer, GradleConfigurationContainer configurations) {
|
||||
if (configurations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println("configurations {");
|
||||
writer.indented(() -> {
|
||||
configurations.forEach(writer::println);
|
||||
configurationCustomizations
|
||||
.forEach((name, customization) -> writeConfiguration(writer, name, customization, configurations));
|
||||
configurations.names().forEach(writer::println);
|
||||
configurations.customizations().forEach((configuration) -> writeConfiguration(writer, configuration));
|
||||
});
|
||||
writer.println("}");
|
||||
writer.println("");
|
||||
}
|
||||
|
||||
protected void writeConfiguration(IndentingWriter writer, GradleConfiguration configuration) {
|
||||
writer.println(configuration.getName() + " {");
|
||||
writer.indented(() -> writer
|
||||
.println(String.format("extendsFrom %s", String.join(", ", configuration.getExtendsFrom()))));
|
||||
writer.println("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
|
||||
String quoteStyle = determineQuoteStyle(dependency.getVersion());
|
||||
@@ -185,31 +179,23 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeTasksWithTypeCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, GradleBuild.TaskCustomization> tasksWithTypeCustomizations = build.getTasksWithTypeCustomizations();
|
||||
|
||||
tasksWithTypeCustomizations.forEach((typeName, customization) -> {
|
||||
protected void writeTasks(IndentingWriter writer, GradleTaskContainer tasks) {
|
||||
tasks.values().filter((candidate) -> candidate.getType() != null).forEach((task) -> {
|
||||
writer.println();
|
||||
writer.println("tasks.withType(" + typeName + ") {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("tasks.withType(" + task.getName() + ") {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, task));
|
||||
writer.println("}");
|
||||
});
|
||||
tasks.values().filter((candidate) -> candidate.getType() == null).forEach((task) -> {
|
||||
writer.println();
|
||||
writer.println(task.getName() + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, task));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeTaskCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, GradleBuild.TaskCustomization> taskCustomizations = build.getTaskCustomizations();
|
||||
|
||||
taskCustomizations.forEach((name, customization) -> {
|
||||
writer.println();
|
||||
writer.println(name + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String invocationAsString(GradleBuild.TaskCustomization.Invocation invocation) {
|
||||
protected String invocationAsString(GradleTask.Invocation invocation) {
|
||||
String arguments = (invocation.getArguments().isEmpty()) ? "()"
|
||||
: " " + String.join(", ", invocation.getArguments());
|
||||
return invocation.getTarget() + arguments;
|
||||
|
||||
@@ -27,8 +27,6 @@ import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency.Exclusion;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.ConfigurationCustomization;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomization;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
@@ -103,22 +101,6 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
|
||||
+ sourceCompatibilitiesToJavaVersion.get(settings.getSourceCompatibility()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
ConfigurationCustomization configurationCustomization, List<String> customConfigurations) {
|
||||
if (configurationCustomization.getExtendsFrom().isEmpty()) {
|
||||
writer.println(configurationName);
|
||||
}
|
||||
else {
|
||||
writer.println(configurationName + " {");
|
||||
writer.indented(() -> writer.println(String.format("extendsFrom(%s)",
|
||||
configurationCustomization.getExtendsFrom().stream()
|
||||
.map((name) -> configurationReference(name, customConfigurations))
|
||||
.collect(Collectors.joining(", ")))));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
private String configurationReference(String configurationName, Collection<String> customConfigurations) {
|
||||
if (customConfigurations.contains(configurationName)) {
|
||||
return configurationName;
|
||||
@@ -135,22 +117,35 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfigurations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, ConfigurationCustomization> configurationCustomizations = build.getConfigurationCustomizations();
|
||||
List<String> configurations = build.getConfigurations();
|
||||
for (String configuration : configurations) {
|
||||
writer.println("val " + configuration + " by configurations.creating");
|
||||
}
|
||||
if (configurationCustomizations.isEmpty()) {
|
||||
protected void writeConfigurations(IndentingWriter writer, GradleConfigurationContainer configurations) {
|
||||
configurations.names()
|
||||
.forEach((configuration) -> writer.println("val " + configuration + " by configurations.creating"));
|
||||
if (!configurations.customizations().findFirst().isPresent()) {
|
||||
return;
|
||||
}
|
||||
writer.println("configurations {");
|
||||
writer.indented(() -> configurationCustomizations
|
||||
.forEach((name, customization) -> writeConfiguration(writer, name, customization, configurations)));
|
||||
List<String> customConfigurations = configurations.names().collect(Collectors.toList());
|
||||
writer.indented(() -> configurations.customizations()
|
||||
.forEach((configuration) -> writeConfiguration(writer, configuration, customConfigurations)));
|
||||
writer.println("}");
|
||||
writer.println("");
|
||||
}
|
||||
|
||||
protected void writeConfiguration(IndentingWriter writer, GradleConfiguration configuration,
|
||||
List<String> customConfigurations) {
|
||||
if (configuration.getExtendsFrom().isEmpty()) {
|
||||
writer.println(configuration.getName());
|
||||
}
|
||||
else {
|
||||
writer.println(configuration.getName() + " {");
|
||||
writer.indented(() -> writer.println(String.format("extendsFrom(%s)",
|
||||
configuration.getExtendsFrom().stream()
|
||||
.map((name) -> configurationReference(name, customConfigurations))
|
||||
.collect(Collectors.joining(", ")))));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeDependency(IndentingWriter writer, Dependency dependency) {
|
||||
String version = determineVersion(dependency.getVersion());
|
||||
@@ -202,31 +197,23 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeTasksWithTypeCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, TaskCustomization> tasksWithTypeCustomizations = build.getTasksWithTypeCustomizations();
|
||||
|
||||
tasksWithTypeCustomizations.forEach((typeName, customization) -> {
|
||||
protected void writeTasks(IndentingWriter writer, GradleTaskContainer tasks) {
|
||||
tasks.values().filter((candidate) -> candidate.getType() != null).forEach((task) -> {
|
||||
writer.println();
|
||||
writer.println("tasks.withType<" + typeName + "> {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("tasks.withType<" + task.getName() + "> {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, task));
|
||||
writer.println("}");
|
||||
});
|
||||
tasks.values().filter((candidate) -> candidate.getType() == null).forEach((task) -> {
|
||||
writer.println();
|
||||
writer.println("tasks." + task.getName() + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, task));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeTaskCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, TaskCustomization> taskCustomizations = build.getTaskCustomizations();
|
||||
|
||||
taskCustomizations.forEach((name, customization) -> {
|
||||
writer.println();
|
||||
writer.println("tasks." + name + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String invocationAsString(TaskCustomization.Invocation invocation) {
|
||||
protected String invocationAsString(GradleTask.Invocation invocation) {
|
||||
return invocation.getTarget() + "(" + String.join(", ", invocation.getArguments()) + ")";
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GradleConfigurationContainer}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GradleConfigurationContainerTests {
|
||||
|
||||
@Test
|
||||
void isEmptyWithEmptyContainer() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isEmptyWithRegisteredName() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.add("devOnly");
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isEmptyWithCustomization() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.customize("runtime", (configuration) -> configuration.extendsFrom("test"));
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithMatchingRegisteredName() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.add("devOnly");
|
||||
assertThat(container.has("devOnly")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithMatchingCustomization() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.customize("runtime", (configuration) -> configuration.extendsFrom("test"));
|
||||
assertThat(container.has("runtime")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithNonMatchingNameOrCustomization() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.add("devOnly");
|
||||
container.customize("runtime", (configuration) -> configuration.extendsFrom("test"));
|
||||
assertThat(container.has("test")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithMatchingName() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.add("com.example");
|
||||
assertThat(container.remove("com.example")).isTrue();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithMatchingCustomization() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.customize("runtime", (configuration) -> configuration.extendsFrom("test"));
|
||||
assertThat(container.remove("runtime")).isTrue();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithNonMatchingNameOrCustomization() {
|
||||
GradleConfigurationContainer container = new GradleConfigurationContainer();
|
||||
container.add("devOnly");
|
||||
container.customize("runtime", (configuration) -> configuration.extendsFrom("test"));
|
||||
assertThat(container.remove("test")).isFalse();
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* Copyright 2012-2019 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.initializr.generator.buildsystem.gradle;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
|
||||
/**
|
||||
* Tests for {@link GradleTaskContainer}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GradleTaskContainerTests {
|
||||
|
||||
@Test
|
||||
void isEmptyWithEmptyContainer() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isEmptyWithTask() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void isEmptyWithTaskWithType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("JavaCompile", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithMatchingTask() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.has("test")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithMatchingTaskWithType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("JavaCompile", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.has("JavaCompile")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void hasWithNonMatchingNameOrType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
container.customizeWithType("JavaCompile", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.has("another")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeTask() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.values()).hasOnlyOneElementSatisfying((task) -> {
|
||||
assertThat(task.getName()).isEqualTo("test");
|
||||
assertThat(task.getType()).isNull();
|
||||
assertThat(task.getAttributes()).containsOnly(entry("fork", "true"));
|
||||
assertThat(task.getInvocations()).isEmpty();
|
||||
assertThat(task.getNested()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeTaskWithType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("com.example.MyTask", (task) -> {
|
||||
task.attribute("fork", "true");
|
||||
task.invoke("property", "taskDir");
|
||||
});
|
||||
assertThat(container.values()).hasOnlyOneElementSatisfying((task) -> {
|
||||
assertThat(task.getName()).isEqualTo("MyTask");
|
||||
assertThat(task.getType()).isEqualTo("com.example.MyTask");
|
||||
assertThat(task.getAttributes()).containsOnly(entry("fork", "true"));
|
||||
assertThat(task.getInvocations()).hasOnlyOneElementSatisfying((invocation) -> {
|
||||
assertThat(invocation.getTarget()).isEqualTo("property");
|
||||
assertThat(invocation.getArguments()).containsOnly("taskDir");
|
||||
});
|
||||
assertThat(task.getNested()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeTaskSeveralTimeReuseConfiguration() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> {
|
||||
task.attribute("ignore", "false");
|
||||
task.attribute("fork", "true");
|
||||
});
|
||||
container.customize("test", (task) -> {
|
||||
task.attribute("fork", "false");
|
||||
task.invoke("method", "arg1", "arg2");
|
||||
});
|
||||
assertThat(container.values()).hasOnlyOneElementSatisfying((task) -> {
|
||||
assertThat(task.getName()).isEqualTo("test");
|
||||
assertThat(task.getType()).isNull();
|
||||
assertThat(task.getAttributes()).containsOnly(entry("ignore", "false"), entry("fork", "false"));
|
||||
assertThat(task.getInvocations()).hasOnlyOneElementSatisfying((invocation) -> {
|
||||
assertThat(invocation.getTarget()).isEqualTo("method");
|
||||
assertThat(invocation.getArguments()).containsOnly("arg1", "arg2");
|
||||
});
|
||||
assertThat(task.getNested()).isEmpty();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeTaskWithFqnImportType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("com.example.MyTask", (task) -> {
|
||||
});
|
||||
assertThat(container.importedTypes()).containsOnly("com.example.MyTask");
|
||||
}
|
||||
|
||||
@Test
|
||||
void customizeTaskWithWellKnownTypeDoesNotImportType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("JavaCompile", (task) -> {
|
||||
});
|
||||
assertThat(container.importedTypes()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithMatchingTask() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.remove("test")).isTrue();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithMatchingTaskWithType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customizeWithType("JavaCompile", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.remove("JavaCompile")).isTrue();
|
||||
assertThat(container.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeWithNonMatchingNameOrType() {
|
||||
GradleTaskContainer container = new GradleTaskContainer();
|
||||
container.customize("test", (task) -> task.attribute("fork", "true"));
|
||||
container.customizeWithType("JavaCompile", (task) -> task.attribute("fork", "true"));
|
||||
assertThat(container.remove("another")).isFalse();
|
||||
assertThat(container.isEmpty()).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -139,11 +139,11 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesCustomizedWithNestedAssignments() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
build.tasks().customizeWithType("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'")));
|
||||
(kotlinOptions) -> kotlinOptions.attribute("freeCompilerArgs", "['-Xjsr305=strict']")));
|
||||
build.tasks().customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile", (task) -> task
|
||||
.nested("kotlinOptions", (kotlinOptions) -> kotlinOptions.attribute("jvmTarget", "'1.8'")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsOnlyOnce("import org.jetbrains.kotlin.gradle.tasks.KotlinCompile").containsSequence(
|
||||
"tasks.withType(KotlinCompile) {", " kotlinOptions {",
|
||||
@@ -153,7 +153,7 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesAndShortTypes() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("JavaCompile", (javaCompile) -> javaCompile.set("options.fork", "true"));
|
||||
build.tasks().customizeWithType("JavaCompile", (javaCompile) -> javaCompile.attribute("options.fork", "true"));
|
||||
assertThat(generateBuild(build)).doesNotContain("import JavaCompile")
|
||||
.containsSequence("tasks.withType(JavaCompile) {", " options.fork = true", "}");
|
||||
}
|
||||
@@ -161,7 +161,7 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithInvocations() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("asciidoctor", (task) -> {
|
||||
build.tasks().customize("asciidoctor", (task) -> {
|
||||
task.invoke("inputs.dir", "snippetsDir");
|
||||
task.invoke("dependsOn", "test");
|
||||
});
|
||||
@@ -172,7 +172,7 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithInvocationAndNoArgument() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("test", (task) -> task.invoke("myMethod"));
|
||||
build.tasks().customize("test", (task) -> task.invoke("myMethod"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("test {", " myMethod()", "}");
|
||||
}
|
||||
@@ -180,9 +180,9 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithAssignments() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("compileKotlin", (task) -> {
|
||||
task.set("kotlinOptions.freeCompilerArgs", "['-Xjsr305=strict']");
|
||||
task.set("kotlinOptions.jvmTarget", "'1.8'");
|
||||
build.tasks().customize("compileKotlin", (task) -> {
|
||||
task.attribute("kotlinOptions.freeCompilerArgs", "['-Xjsr305=strict']");
|
||||
task.attribute("kotlinOptions.jvmTarget", "'1.8'");
|
||||
});
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("compileKotlin {",
|
||||
@@ -192,10 +192,10 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithNestedCustomization() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("compileKotlin",
|
||||
build.tasks().customize("compileKotlin",
|
||||
(compileKotlin) -> compileKotlin.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
kotlinOptions.set("freeCompilerArgs", "['-Xjsr305=strict']");
|
||||
kotlinOptions.set("jvmTarget", "'1.8'");
|
||||
kotlinOptions.attribute("freeCompilerArgs", "['-Xjsr305=strict']");
|
||||
kotlinOptions.attribute("jvmTarget", "'1.8'");
|
||||
}));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("compileKotlin {", " kotlinOptions {",
|
||||
@@ -257,7 +257,7 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfiguration() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addConfiguration("developmentOnly");
|
||||
build.configurations().add("developmentOnly");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly", "}");
|
||||
}
|
||||
@@ -265,8 +265,9 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomization() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeConfiguration("developmentOnly", (configuration) -> configuration.extendsFrom("compile"));
|
||||
build.customizeConfiguration("developmentOnly", (configuration) -> configuration.extendsFrom("testCompile"));
|
||||
build.configurations().customize("developmentOnly", (configuration) -> configuration.extendsFrom("compile"));
|
||||
build.configurations().customize("developmentOnly",
|
||||
(configuration) -> configuration.extendsFrom("testCompile"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly {",
|
||||
" extendsFrom compile, testCompile", " }", "}");
|
||||
@@ -275,8 +276,8 @@ class GroovyDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomizations() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeConfiguration("developmentOnly", (configuration) -> configuration.extendsFrom("compile"));
|
||||
build.customizeConfiguration("testOnly", (configuration) -> configuration.extendsFrom("testCompile"));
|
||||
build.configurations().customize("developmentOnly", (configuration) -> configuration.extendsFrom("compile"));
|
||||
build.configurations().customize("testOnly", (configuration) -> configuration.extendsFrom("testCompile"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly {", " extendsFrom compile",
|
||||
" }", " testOnly {", " extendsFrom testCompile", " }", "}");
|
||||
|
||||
@@ -150,11 +150,11 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesCustomizedWithNestedAssignments() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
build.tasks().customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
(task) -> task.nested("kotlinOptions",
|
||||
(kotlinOptions) -> kotlinOptions.set("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")));
|
||||
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
(task) -> task.nested("kotlinOptions", (kotlinOptions) -> kotlinOptions.set("jvmTarget", "\"1.8\"")));
|
||||
(kotlinOptions) -> kotlinOptions.attribute("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")));
|
||||
build.tasks().customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile", (task) -> task
|
||||
.nested("kotlinOptions", (kotlinOptions) -> kotlinOptions.attribute("jvmTarget", "\"1.8\"")));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsOnlyOnce("import org.jetbrains.kotlin.gradle.tasks.KotlinCompile").containsSequence(
|
||||
"tasks.withType<KotlinCompile> {", " kotlinOptions {",
|
||||
@@ -164,7 +164,7 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesAndShortTypes() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("JavaCompile", (javaCompile) -> javaCompile.set("options.fork", "true"));
|
||||
build.tasks().customizeWithType("JavaCompile", (javaCompile) -> javaCompile.attribute("options.fork", "true"));
|
||||
assertThat(generateBuild(build)).doesNotContain("import JavaCompile")
|
||||
.containsSequence("tasks.withType<JavaCompile> {", " options.fork = true", "}");
|
||||
}
|
||||
@@ -172,7 +172,7 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithInvocations() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("asciidoctor", (task) -> {
|
||||
build.tasks().customize("asciidoctor", (task) -> {
|
||||
task.invoke("inputs.dir", "snippetsDir");
|
||||
task.invoke("dependsOn", "test");
|
||||
});
|
||||
@@ -184,9 +184,9 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithAssignments() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("compileKotlin", (task) -> {
|
||||
task.set("kotlinOptions.freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
task.set("kotlinOptions.jvmTarget", "\"1.8\"");
|
||||
build.tasks().customize("compileKotlin", (task) -> {
|
||||
task.attribute("kotlinOptions.freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
task.attribute("kotlinOptions.jvmTarget", "\"1.8\"");
|
||||
});
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.compileKotlin {",
|
||||
@@ -197,10 +197,10 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithNestedCustomization() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("compileKotlin",
|
||||
build.tasks().customize("compileKotlin",
|
||||
(compileKotlin) -> compileKotlin.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
kotlinOptions.set("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
kotlinOptions.set("jvmTarget", "\"1.8\"");
|
||||
kotlinOptions.attribute("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
kotlinOptions.attribute("jvmTarget", "\"1.8\"");
|
||||
}));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.compileKotlin {", " kotlinOptions {",
|
||||
@@ -262,7 +262,7 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfiguration() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addConfiguration("developmentOnly");
|
||||
build.configurations().add("developmentOnly");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("val developmentOnly by configurations.creating");
|
||||
}
|
||||
@@ -270,9 +270,9 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomization() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addConfiguration("custom");
|
||||
build.customizeConfiguration("runtimeClasspath", (configuration) -> configuration.extendsFrom("custom"));
|
||||
build.customizeConfiguration("runtimeClasspath", (configuration) -> configuration.extendsFrom("builtIn"));
|
||||
build.configurations().add("custom");
|
||||
build.configurations().customize("runtimeClasspath", (configuration) -> configuration.extendsFrom("custom"));
|
||||
build.configurations().customize("runtimeClasspath", (configuration) -> configuration.extendsFrom("builtIn"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("val custom by configurations.creating", "configurations {",
|
||||
" runtimeClasspath {", " extendsFrom(custom, configurations.builtIn.get())", " }", "}");
|
||||
@@ -281,9 +281,10 @@ class KotlinDslGradleBuildWriterTests {
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomizations() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addConfiguration("custom");
|
||||
build.customizeConfiguration("runtimeClasspath", (configuration) -> configuration.extendsFrom("custom"));
|
||||
build.customizeConfiguration("testRuntimeClasspath", (configuration) -> configuration.extendsFrom("builtIn"));
|
||||
build.configurations().add("custom");
|
||||
build.configurations().customize("runtimeClasspath", (configuration) -> configuration.extendsFrom("custom"));
|
||||
build.configurations().customize("testRuntimeClasspath",
|
||||
(configuration) -> configuration.extendsFrom("builtIn"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("val custom by configurations.creating", "configurations {",
|
||||
" runtimeClasspath {", " extendsFrom(custom)", " }", " testRuntimeClasspath {",
|
||||
|
||||
Reference in New Issue
Block a user