mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-19 01:58:16 +08:00
Add Gradle Kotlin DSL support
This commit refactors the existing Groovy DSL writers in order to share code between the Groovy DSL and Kotlin DSL writers. See gh-851
This commit is contained in:
@@ -19,11 +19,11 @@ package io.spring.initializr.generator.buildsystem.gradle;
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuildWriter} suitable for Gradle 3.
|
||||
* A {@link GroovyDslGradleBuildWriter} suitable for Gradle 3.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class Gradle3BuildWriter extends GradleBuildWriter {
|
||||
public class Gradle3BuildWriter extends GroovyDslGradleBuildWriter {
|
||||
|
||||
protected String configurationForScope(DependencyScope type) {
|
||||
switch (type) {
|
||||
|
@@ -43,23 +43,26 @@ import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code build.gradle}.
|
||||
* A {@link GradleBuild} writer template for build.gradle and build.gradle.kts. A subclass
|
||||
* of this class exists for the Groovy DSL and for the Kotlin DSL.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GradleBuildWriter {
|
||||
public abstract class GradleBuildWriter {
|
||||
|
||||
public void writeTo(IndentingWriter writer, GradleBuild build) throws IOException {
|
||||
public final void writeTo(IndentingWriter writer, GradleBuild build)
|
||||
throws IOException {
|
||||
writeImports(writer, build);
|
||||
boolean buildScriptWritten = writeBuildscript(writer, build);
|
||||
writePlugins(writer, build, buildScriptWritten);
|
||||
writeBuildscript(writer, build);
|
||||
writePlugins(writer, build);
|
||||
writeProperty(writer, "group", build.getGroup());
|
||||
writeProperty(writer, "version", build.getVersion());
|
||||
writeProperty(writer, "sourceCompatibility", build.getSourceCompatibility());
|
||||
writeJavaSourceCompatibility(writer, build);
|
||||
writer.println();
|
||||
writeConfigurations(writer, build);
|
||||
writeRepositories(writer, build, writer::println);
|
||||
writeRepositories(writer, build);
|
||||
writeProperties(writer, build);
|
||||
writeDependencies(writer, build);
|
||||
writeBoms(writer, build);
|
||||
@@ -75,61 +78,12 @@ public class GradleBuildWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean writeBuildscript(IndentingWriter writer, GradleBuild build) {
|
||||
List<String> dependencies = build.getBuildscript().getDependencies();
|
||||
Map<String, String> ext = build.getBuildscript().getExt();
|
||||
if (dependencies.isEmpty() && ext.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
writer.println("buildscript {");
|
||||
writer.indented(() -> {
|
||||
writeBuildscriptExt(writer, build);
|
||||
writeBuildscriptRepositories(writer, build);
|
||||
writeBuildscriptDependencies(writer, build);
|
||||
});
|
||||
writer.println("}");
|
||||
return true;
|
||||
}
|
||||
protected abstract void writeBuildscript(IndentingWriter writer, GradleBuild build);
|
||||
|
||||
private void writeBuildscriptExt(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedMap(writer, "ext", build.getBuildscript().getExt(),
|
||||
(key, value) -> key + " = " + value);
|
||||
}
|
||||
protected abstract void writePlugins(IndentingWriter writer, GradleBuild build);
|
||||
|
||||
private void writeBuildscriptRepositories(IndentingWriter writer, GradleBuild build) {
|
||||
writeRepositories(writer, build);
|
||||
}
|
||||
|
||||
private void writeBuildscriptDependencies(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "dependencies",
|
||||
build.getBuildscript().getDependencies(),
|
||||
(dependency) -> "classpath '" + dependency + "'");
|
||||
}
|
||||
|
||||
private void writePlugins(IndentingWriter writer, GradleBuild build,
|
||||
boolean buildScriptWritten) {
|
||||
writeNestedCollection(writer, "plugins", build.getPlugins(), this::pluginAsString,
|
||||
determineBeforeWriting(buildScriptWritten, writer));
|
||||
writeCollection(writer, build.getAppliedPlugins(),
|
||||
(plugin) -> "apply plugin: '" + plugin + "'", writer::println);
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private Runnable determineBeforeWriting(boolean buildScriptWritten,
|
||||
IndentingWriter writer) {
|
||||
if (buildScriptWritten) {
|
||||
return writer::println;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String pluginAsString(GradlePlugin plugin) {
|
||||
String string = "id '" + plugin.getId() + "'";
|
||||
if (plugin.getVersion() != null) {
|
||||
string += " version '" + plugin.getVersion() + "'";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
protected abstract void writeJavaSourceCompatibility(IndentingWriter writer,
|
||||
GradleBuild build);
|
||||
|
||||
private void writeConfigurations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, ConfigurationCustomization> configurationCustomizations = build
|
||||
@@ -137,43 +91,24 @@ public class GradleBuildWriter {
|
||||
if (configurationCustomizations.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println();
|
||||
writer.println("configurations {");
|
||||
writer.indented(() -> configurationCustomizations.forEach((name,
|
||||
customization) -> writeConfiguration(writer, name, customization)));
|
||||
writer.println("}");
|
||||
writer.println("");
|
||||
}
|
||||
|
||||
private void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
ConfigurationCustomization configurationCustomization) {
|
||||
if (configurationCustomization.getExtendsFrom().isEmpty()) {
|
||||
writer.println(configurationName);
|
||||
}
|
||||
else {
|
||||
writer.println(configurationName + " {");
|
||||
writer.indented(() -> writer.println(String.format("extendsFrom %s",
|
||||
String.join(", ", configurationCustomization.getExtendsFrom()))));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
protected abstract void writeConfiguration(IndentingWriter writer,
|
||||
String configurationName,
|
||||
ConfigurationCustomization configurationCustomization);
|
||||
|
||||
private void writeRepositories(IndentingWriter writer, GradleBuild build) {
|
||||
writeRepositories(writer, build, null);
|
||||
}
|
||||
|
||||
private void writeRepositories(IndentingWriter writer, GradleBuild build,
|
||||
Runnable beforeWriting) {
|
||||
protected final void writeRepositories(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "repositories",
|
||||
build.repositories().items().collect(Collectors.toList()),
|
||||
this::repositoryAsString, beforeWriting);
|
||||
this::repositoryAsString);
|
||||
}
|
||||
|
||||
private String repositoryAsString(MavenRepository repository) {
|
||||
if (MavenRepository.MAVEN_CENTRAL.equals(repository)) {
|
||||
return "mavenCentral()";
|
||||
}
|
||||
return "maven { url '" + repository.getUrl() + "' }";
|
||||
}
|
||||
protected abstract String repositoryAsString(MavenRepository repository);
|
||||
|
||||
private void writeProperties(IndentingWriter writer, GradleBuild build) {
|
||||
if (build.getExt().isEmpty() && build.getVersionProperties().isEmpty()) {
|
||||
@@ -182,19 +117,17 @@ public class GradleBuildWriter {
|
||||
Map<String, String> allProperties = new LinkedHashMap<>(build.getExt());
|
||||
build.getVersionProperties().entrySet().forEach((entry) -> allProperties
|
||||
.put(getVersionPropertyKey(entry), "'" + entry.getValue() + "'"));
|
||||
writeNestedCollection(writer, "ext", allProperties.entrySet(),
|
||||
(e) -> getFormattedProperty(e.getKey(), e.getValue()), writer::println);
|
||||
writeExtraProperties(writer, allProperties);
|
||||
}
|
||||
|
||||
protected abstract void writeExtraProperties(IndentingWriter writer,
|
||||
Map<String, String> allProperties);
|
||||
|
||||
private String getVersionPropertyKey(Entry<VersionProperty, String> entry) {
|
||||
return entry.getKey().isInternal() ? entry.getKey().toCamelCaseFormat()
|
||||
: entry.getKey().toStandardFormat();
|
||||
}
|
||||
|
||||
private String getFormattedProperty(String key, String value) {
|
||||
return String.format("set('%s', %s)", key, value);
|
||||
}
|
||||
|
||||
private void writeDependencies(IndentingWriter writer, GradleBuild build) {
|
||||
Set<Dependency> sortedDependencies = new LinkedHashSet<>();
|
||||
DependencyContainer dependencies = build.dependencies();
|
||||
@@ -216,15 +149,7 @@ public class GradleBuildWriter {
|
||||
this::dependencyAsString, writer::println);
|
||||
}
|
||||
|
||||
private String dependencyAsString(Dependency dependency) {
|
||||
String quoteStyle = determineQuoteStyle(dependency.getVersion());
|
||||
String version = determineVersion(dependency.getVersion());
|
||||
String type = dependency.getType();
|
||||
return configurationForScope(dependency.getScope()) + " " + quoteStyle
|
||||
+ dependency.getGroupId() + ":" + dependency.getArtifactId()
|
||||
+ ((version != null) ? ":" + version : "")
|
||||
+ ((type != null) ? "@" + type : "") + quoteStyle;
|
||||
}
|
||||
protected abstract String dependencyAsString(Dependency dependency);
|
||||
|
||||
protected String configurationForScope(DependencyScope type) {
|
||||
switch (type) {
|
||||
@@ -263,23 +188,18 @@ public class GradleBuildWriter {
|
||||
}
|
||||
|
||||
private String bomAsString(BillOfMaterials bom) {
|
||||
String quoteStyle = determineQuoteStyle(bom.getVersion());
|
||||
String version = determineVersion(bom.getVersion());
|
||||
return "mavenBom " + quoteStyle + bom.getGroupId() + ":" + bom.getArtifactId()
|
||||
+ ":" + version + quoteStyle;
|
||||
return bomAsString(bom, version);
|
||||
}
|
||||
|
||||
private String determineQuoteStyle(VersionReference versionReference) {
|
||||
return (versionReference != null && versionReference.isProperty()) ? "\"" : "'";
|
||||
}
|
||||
protected abstract String bomAsString(BillOfMaterials bom, String version);
|
||||
|
||||
private String determineVersion(VersionReference versionReference) {
|
||||
protected final String determineVersion(VersionReference versionReference) {
|
||||
if (versionReference != null) {
|
||||
if (versionReference.isProperty()) {
|
||||
VersionProperty property = versionReference.getProperty();
|
||||
return "${"
|
||||
+ (property.isInternal() ? property.toCamelCaseFormat()
|
||||
: "property('" + property.toStandardFormat() + "')")
|
||||
return "${" + (property.isInternal() ? property.toCamelCaseFormat()
|
||||
: externalVersionPropertyAsString(property.toStandardFormat()))
|
||||
+ "}";
|
||||
}
|
||||
return versionReference.getValue();
|
||||
@@ -287,33 +207,17 @@ 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("}");
|
||||
});
|
||||
}
|
||||
protected abstract String externalVersionPropertyAsString(String standardFormat);
|
||||
|
||||
private void writeTaskCustomizations(IndentingWriter writer, GradleBuild build) {
|
||||
Map<String, TaskCustomization> taskCustomizations = build.getTaskCustomizations();
|
||||
taskCustomizations.forEach((name, customization) -> {
|
||||
writer.println();
|
||||
writer.println(name + " {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
writer.println("}");
|
||||
});
|
||||
}
|
||||
protected abstract void writeTasksWithTypeCustomizations(IndentingWriter writer,
|
||||
GradleBuild build);
|
||||
|
||||
private void writeTaskCustomization(IndentingWriter writer,
|
||||
protected abstract void writeTaskCustomizations(IndentingWriter writer,
|
||||
GradleBuild build);
|
||||
|
||||
protected final void writeTaskCustomization(IndentingWriter writer,
|
||||
TaskCustomization customization) {
|
||||
writeCollection(writer, customization.getInvocations(),
|
||||
(invocation) -> invocation.getTarget() + " "
|
||||
+ String.join(", ", invocation.getArguments()));
|
||||
writeCollection(writer, customization.getInvocations(), this::invocationAsString);
|
||||
writeMap(writer, customization.getAssignments(),
|
||||
(key, value) -> key + " = " + value);
|
||||
customization.getNested().forEach((property, nestedCustomization) -> {
|
||||
@@ -323,12 +227,14 @@ public class GradleBuildWriter {
|
||||
});
|
||||
}
|
||||
|
||||
private <T> void writeNestedCollection(IndentingWriter writer, String name,
|
||||
protected abstract String invocationAsString(TaskCustomization.Invocation invocation);
|
||||
|
||||
protected final <T> void writeNestedCollection(IndentingWriter writer, String name,
|
||||
Collection<T> collection, Function<T, String> itemToStringConverter) {
|
||||
this.writeNestedCollection(writer, name, collection, itemToStringConverter, null);
|
||||
}
|
||||
|
||||
private <T> void writeNestedCollection(IndentingWriter writer, String name,
|
||||
protected final <T> void writeNestedCollection(IndentingWriter writer, String name,
|
||||
Collection<T> collection, Function<T, String> converter,
|
||||
Runnable beforeWriting) {
|
||||
if (!collection.isEmpty()) {
|
||||
@@ -347,8 +253,9 @@ public class GradleBuildWriter {
|
||||
writeCollection(writer, collection, converter, null);
|
||||
}
|
||||
|
||||
private <T> void writeCollection(IndentingWriter writer, Collection<T> collection,
|
||||
Function<T, String> itemToStringConverter, Runnable beforeWriting) {
|
||||
protected final <T> void writeCollection(IndentingWriter writer,
|
||||
Collection<T> collection, Function<T, String> itemToStringConverter,
|
||||
Runnable beforeWriting) {
|
||||
if (!collection.isEmpty()) {
|
||||
if (beforeWriting != null) {
|
||||
beforeWriting.run();
|
||||
@@ -357,25 +264,13 @@ public class GradleBuildWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private <T, U> void writeNestedMap(IndentingWriter writer, String name, Map<T, U> map,
|
||||
BiFunction<T, U, String> converter) {
|
||||
if (!map.isEmpty()) {
|
||||
writer.println(name + " {");
|
||||
writer.indented(() -> writeMap(writer, map, converter));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
private <T, U> void writeMap(IndentingWriter writer, Map<T, U> map,
|
||||
protected final <T, U> void writeMap(IndentingWriter writer, Map<T, U> map,
|
||||
BiFunction<T, U, String> converter) {
|
||||
map.forEach((key, value) -> writer.println(converter.apply(key, value)));
|
||||
}
|
||||
|
||||
private void writeProperty(IndentingWriter writer, String name, String value) {
|
||||
if (value != null) {
|
||||
writer.println(String.format("%s = '%s'", name, value));
|
||||
}
|
||||
}
|
||||
protected abstract void writeProperty(IndentingWriter writer, String name,
|
||||
String value);
|
||||
|
||||
private static Collection<Dependency> filterDependencies(
|
||||
DependencyContainer dependencies, DependencyScope... types) {
|
||||
|
@@ -22,15 +22,19 @@ import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code settings.gradle}.
|
||||
* A {@link GradleBuild} writer template for {@code settings.gradle} and
|
||||
* {@code settings.gradle.kts}. A subclass of this class exists for the Groovy DSL and for
|
||||
* the Kotlin DSL.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GradleSettingsWriter {
|
||||
public abstract class GradleSettingsWriter {
|
||||
|
||||
public void writeTo(IndentingWriter writer, GradleBuild build) throws IOException {
|
||||
public final void writeTo(IndentingWriter writer, GradleBuild build)
|
||||
throws IOException {
|
||||
writePluginManagement(writer, build);
|
||||
writer.println("rootProject.name = '" + build.getArtifact() + "'");
|
||||
writer.println("rootProject.name = " + wrapWithQuotes(build.getArtifact()));
|
||||
}
|
||||
|
||||
private void writePluginManagement(IndentingWriter writer, GradleBuild build) {
|
||||
@@ -62,7 +66,8 @@ public class GradleSettingsWriter {
|
||||
writer.indented(() -> {
|
||||
writer.println("eachPlugin {");
|
||||
writer.indented(() -> {
|
||||
writer.println("if (requested.id.id == 'org.springframework.boot') {");
|
||||
writer.println("if (requested.id.id == "
|
||||
+ wrapWithQuotes("org.springframework.boot") + ") {");
|
||||
writer.indented(() -> writer.println(
|
||||
"useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")"));
|
||||
writer.println("}");
|
||||
@@ -76,7 +81,11 @@ public class GradleSettingsWriter {
|
||||
if (MavenRepository.MAVEN_CENTRAL.equals(repository)) {
|
||||
return "mavenCentral()";
|
||||
}
|
||||
return "maven { url '" + repository.getUrl() + "' }";
|
||||
return "maven { " + urlAssignment(repository.getUrl()) + " }";
|
||||
}
|
||||
|
||||
protected abstract String wrapWithQuotes(String value);
|
||||
|
||||
protected abstract String urlAssignment(String url);
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,202 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
import io.spring.initializr.generator.buildsystem.MavenRepository;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code build.gradle}.
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
|
||||
|
||||
@Override
|
||||
protected void writeBuildscript(IndentingWriter writer, GradleBuild build) {
|
||||
List<String> dependencies = build.getBuildscript().getDependencies();
|
||||
Map<String, String> ext = build.getBuildscript().getExt();
|
||||
if (dependencies.isEmpty() && ext.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
writer.println("buildscript {");
|
||||
writer.indented(() -> {
|
||||
writeBuildscriptExt(writer, build);
|
||||
writeBuildscriptRepositories(writer, build);
|
||||
writeBuildscriptDependencies(writer, build);
|
||||
});
|
||||
writer.println("}");
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private void writeBuildscriptExt(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedMap(writer, "ext", build.getBuildscript().getExt(),
|
||||
(key, value) -> key + " = " + value);
|
||||
}
|
||||
|
||||
private void writeBuildscriptRepositories(IndentingWriter writer, GradleBuild build) {
|
||||
writeRepositories(writer, build);
|
||||
}
|
||||
|
||||
private void writeBuildscriptDependencies(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "dependencies",
|
||||
build.getBuildscript().getDependencies(),
|
||||
(dependency) -> "classpath '" + dependency + "'");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writePlugins(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "plugins", build.getPlugins(),
|
||||
this::pluginAsString);
|
||||
writeCollection(writer, build.getAppliedPlugins(),
|
||||
(plugin) -> "apply plugin: '" + plugin + "'", writer::println);
|
||||
writer.println();
|
||||
}
|
||||
|
||||
private String pluginAsString(GradlePlugin plugin) {
|
||||
String string = "id '" + plugin.getId() + "'";
|
||||
if (plugin.getVersion() != null) {
|
||||
string += " version '" + plugin.getVersion() + "'";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeJavaSourceCompatibility(IndentingWriter writer,
|
||||
GradleBuild build) {
|
||||
writeProperty(writer, "sourceCompatibility", build.getSourceCompatibility());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
GradleBuild.ConfigurationCustomization configurationCustomization) {
|
||||
if (configurationCustomization.getExtendsFrom().isEmpty()) {
|
||||
writer.println(configurationName);
|
||||
}
|
||||
else {
|
||||
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)) {
|
||||
return "mavenCentral()";
|
||||
}
|
||||
return "maven { url '" + repository.getUrl() + "' }";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeExtraProperties(IndentingWriter writer,
|
||||
Map<String, String> allProperties) {
|
||||
writeNestedCollection(writer, "ext", allProperties.entrySet(),
|
||||
(e) -> getFormattedExtraProperty(e.getKey(), e.getValue()),
|
||||
writer::println);
|
||||
}
|
||||
|
||||
private String getFormattedExtraProperty(String key, String value) {
|
||||
return String.format("set('%s', '%s')", key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String dependencyAsString(Dependency dependency) {
|
||||
String quoteStyle = determineQuoteStyle(dependency.getVersion());
|
||||
String version = determineVersion(dependency.getVersion());
|
||||
String type = dependency.getType();
|
||||
return configurationForScope(dependency.getScope()) + " " + quoteStyle
|
||||
+ dependency.getGroupId() + ":" + dependency.getArtifactId()
|
||||
+ ((version != null) ? ":" + version : "")
|
||||
+ ((type != null) ? "@" + type : "") + quoteStyle;
|
||||
}
|
||||
|
||||
private String determineQuoteStyle(VersionReference versionReference) {
|
||||
return (versionReference != null && versionReference.isProperty()) ? "\"" : "'";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String bomAsString(BillOfMaterials bom, String version) {
|
||||
String quoteStyle = determineQuoteStyle(bom.getVersion());
|
||||
return "mavenBom " + quoteStyle + bom.getGroupId() + ":" + bom.getArtifactId()
|
||||
+ ":" + version + quoteStyle;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String externalVersionPropertyAsString(String standardFormat) {
|
||||
return "property('" + standardFormat + "')";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected 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("}");
|
||||
});
|
||||
}
|
||||
|
||||
@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) {
|
||||
return invocation.getTarget() + " "
|
||||
+ String.join(", ", invocation.getArguments());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeProperty(IndentingWriter writer, String name, String value) {
|
||||
if (value != null) {
|
||||
writer.println(String.format("%s = '%s'", name, value));
|
||||
}
|
||||
}
|
||||
|
||||
private <T, U> void writeNestedMap(IndentingWriter writer, String name, Map<T, U> map,
|
||||
BiFunction<T, U, String> converter) {
|
||||
if (!map.isEmpty()) {
|
||||
writer.println(name + " {");
|
||||
writer.indented(() -> writeMap(writer, map, converter));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code settings.gradle}.
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class GroovyDslGradleSettingsWriter extends GradleSettingsWriter {
|
||||
|
||||
@Override
|
||||
protected String wrapWithQuotes(String value) {
|
||||
return "'" + value + "'";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String urlAssignment(String url) {
|
||||
return "url " + wrapWithQuotes(url);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.BillOfMaterials;
|
||||
import io.spring.initializr.generator.buildsystem.Dependency;
|
||||
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;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code build.gradle.kts}.
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
|
||||
|
||||
private static final Map<String, String> sourceCompatibilitiesToJavaVersion = createSourceCompatibilitiesToJavaVersion();
|
||||
|
||||
private static Map<String, String> createSourceCompatibilitiesToJavaVersion() {
|
||||
Map<String, String> result = new HashMap<>();
|
||||
for (int version = 6; version <= 10; version++) {
|
||||
result.put(Integer.toString(version), "VERSION_1_" + version);
|
||||
result.put("1." + version, "VERSION_1_" + version);
|
||||
}
|
||||
for (int version = 11; version <= 12; version++) {
|
||||
result.put(Integer.toString(version), "VERSION_" + version);
|
||||
result.put("1." + version, "VERSION_" + version);
|
||||
}
|
||||
|
||||
return Collections.unmodifiableMap(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeBuildscript(IndentingWriter writer, GradleBuild build) {
|
||||
if (!(build.getBuildscript().getDependencies().isEmpty()
|
||||
&& build.getBuildscript().getExt().isEmpty())) {
|
||||
throw new IllegalStateException(
|
||||
"build.gradle.kts scripts shouldn't need a buildscript");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writePlugins(IndentingWriter writer, GradleBuild build) {
|
||||
writeNestedCollection(writer, "plugins", build.getPlugins(), this::pluginAsString,
|
||||
null);
|
||||
writer.println();
|
||||
|
||||
if (!build.getAppliedPlugins().isEmpty()) {
|
||||
throw new IllegalStateException(
|
||||
"build.gradle.kts scripts shouldn't apply plugins. They should use the plugins block instead.");
|
||||
}
|
||||
}
|
||||
|
||||
private String pluginAsString(GradlePlugin plugin) {
|
||||
String result = shortPluginNotation(plugin.getId());
|
||||
if (result == null) {
|
||||
result = "id(\"" + plugin.getId() + "\")";
|
||||
}
|
||||
if (plugin.getVersion() != null) {
|
||||
result += " version \"" + plugin.getVersion() + "\"";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String shortPluginNotation(String pluginId) {
|
||||
if (pluginId.equals("java") || pluginId.equals("war")
|
||||
|| pluginId.equals("groovy")) {
|
||||
return pluginId;
|
||||
}
|
||||
|
||||
String kotlinPluginPrefix = "org.jetbrains.kotlin.";
|
||||
if (pluginId.startsWith(kotlinPluginPrefix)) {
|
||||
return "kotlin(\"" + pluginId.substring(kotlinPluginPrefix.length()) + "\")";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeJavaSourceCompatibility(IndentingWriter writer,
|
||||
GradleBuild build) {
|
||||
writer.println("java.sourceCompatibility = JavaVersion."
|
||||
+ sourceCompatibilitiesToJavaVersion.get(build.getSourceCompatibility()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeConfiguration(IndentingWriter writer, String configurationName,
|
||||
ConfigurationCustomization configurationCustomization) {
|
||||
if (configurationCustomization.getExtendsFrom().isEmpty()) {
|
||||
writer.println(configurationName);
|
||||
}
|
||||
else {
|
||||
writer.println(configurationName + " {");
|
||||
writer.indented(() -> writer.println(String.format("extendsFrom(%s)",
|
||||
configurationCustomization.getExtendsFrom().stream()
|
||||
.map((c) -> "configurations." + c + ".get()")
|
||||
.collect(Collectors.joining(", ")))));
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String repositoryAsString(MavenRepository repository) {
|
||||
if (MavenRepository.MAVEN_CENTRAL.equals(repository)) {
|
||||
return "mavenCentral()";
|
||||
}
|
||||
return "maven { url = uri(\"" + repository.getUrl() + "\") }";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String dependencyAsString(Dependency dependency) {
|
||||
String version = determineVersion(dependency.getVersion());
|
||||
String type = dependency.getType();
|
||||
return configurationForScope(dependency.getScope()) + "(\""
|
||||
+ dependency.getGroupId() + ":" + dependency.getArtifactId()
|
||||
+ ((version != null) ? ":" + version : "")
|
||||
+ ((type != null) ? "@" + type : "") + "\")";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeExtraProperties(IndentingWriter writer,
|
||||
Map<String, String> allProperties) {
|
||||
writeCollection(writer, allProperties.entrySet(),
|
||||
(e) -> getFormattedExtraProperty(e.getKey(), e.getValue()),
|
||||
writer::println);
|
||||
}
|
||||
|
||||
private String getFormattedExtraProperty(String key, String value) {
|
||||
return String.format("extra[\"%s\"] = \"%s\"", key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String bomAsString(BillOfMaterials bom, String version) {
|
||||
return "mavenBom(\"" + bom.getGroupId() + ":" + bom.getArtifactId() + ":"
|
||||
+ version + "\")";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String externalVersionPropertyAsString(String standardFormat) {
|
||||
return "property(\"" + standardFormat + "\")";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeTasksWithTypeCustomizations(IndentingWriter writer,
|
||||
GradleBuild build) {
|
||||
Map<String, TaskCustomization> tasksWithTypeCustomizations = build
|
||||
.getTasksWithTypeCustomizations();
|
||||
|
||||
tasksWithTypeCustomizations.forEach((typeName, customization) -> {
|
||||
writer.println();
|
||||
writer.println("tasks.withType<" + typeName + "> {");
|
||||
writer.indented(() -> writeTaskCustomization(writer, customization));
|
||||
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) {
|
||||
return invocation.getTarget() + "(" + String.join(", ", invocation.getArguments())
|
||||
+ ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeProperty(IndentingWriter writer, String name, String value) {
|
||||
if (value != null) {
|
||||
writer.println(String.format("%s = \"%s\"", name, value));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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;
|
||||
|
||||
/**
|
||||
* A {@link GradleBuild} writer for {@code settings.gradle.kts}.
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
public class KotlinDslGradleSettingsWriter extends GradleSettingsWriter {
|
||||
|
||||
@Override
|
||||
protected String wrapWithQuotes(String value) {
|
||||
return "\"" + value + "\"";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String urlAssignment(String url) {
|
||||
return "url = uri(\"" + url + "\")";
|
||||
}
|
||||
|
||||
}
|
@@ -30,13 +30,13 @@ import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GradleBuildWriter}
|
||||
* Tests for {@link GroovyDslGradleBuildWriter}
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
class GradleBuildWriterTests {
|
||||
class GroovyDslGradleBuildWriterTests {
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCoordinates() throws IOException {
|
||||
@@ -426,7 +426,7 @@ class GradleBuildWriterTests {
|
||||
}
|
||||
|
||||
private List<String> generateBuild(GradleBuild build) throws IOException {
|
||||
GradleBuildWriter writer = new GradleBuildWriter();
|
||||
GradleBuildWriter writer = new GroovyDslGradleBuildWriter();
|
||||
StringWriter out = new StringWriter();
|
||||
writer.writeTo(new IndentingWriter(out), build);
|
||||
return TextTestUtils.readAllLines(out.toString());
|
@@ -27,11 +27,12 @@ import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link GradleSettingsWriter}.
|
||||
* Tests for {@link GroovyDslGradleSettingsWriter}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
class GradleSettingsWriterTests {
|
||||
class GroovyDslGradleSettingsWriterTests {
|
||||
|
||||
@Test
|
||||
void gradleBuildWithMavenCentralPluginRepository() throws IOException {
|
||||
@@ -81,7 +82,7 @@ class GradleSettingsWriterTests {
|
||||
}
|
||||
|
||||
private List<String> generateSettings(GradleBuild build) throws IOException {
|
||||
GradleSettingsWriter writer = new GradleSettingsWriter();
|
||||
GradleSettingsWriter writer = new GroovyDslGradleSettingsWriter();
|
||||
StringWriter out = new StringWriter();
|
||||
writer.writeTo(new IndentingWriter(out), build);
|
||||
return TextTestUtils.readAllLines(out.toString());
|
@@ -0,0 +1,457 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.DependencyScope;
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import io.spring.initializr.generator.version.VersionProperty;
|
||||
import io.spring.initializr.generator.version.VersionReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
|
||||
/**
|
||||
* Tests for {@link KotlinDslGradleBuildWriter}
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
class KotlinDslGradleBuildWriterTests {
|
||||
|
||||
@Test
|
||||
void gradleBuildWithImports() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addImportedType(
|
||||
"org.springframework.boot.gradle.tasks.buildinfo.BuildInfo");
|
||||
build.addImportedType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile");
|
||||
build.addImportedType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile"); // same
|
||||
// import
|
||||
// added
|
||||
// twice
|
||||
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines.subList(0, 3)).containsExactly(
|
||||
"import org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
|
||||
"import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCoordinates() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setGroup("com.example");
|
||||
build.setVersion("1.0.1-SNAPSHOT");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).contains("group = \"com.example\"",
|
||||
"version = \"1.0.1-SNAPSHOT\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithSourceCompatibility11() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setSourceCompatibility("11");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).contains("java.sourceCompatibility = JavaVersion.VERSION_11");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithSourceCompatibility1Dot8() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setSourceCompatibility("1.8");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).contains("java.sourceCompatibility = JavaVersion.VERSION_1_8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithBuildscriptDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.buildscript((buildscript) -> buildscript.dependency(
|
||||
"org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RELEASE"));
|
||||
|
||||
assertThatIllegalStateException().isThrownBy(() -> {
|
||||
generateBuild(build);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithBuildscriptExtProperty() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.buildscript((buildscript) -> buildscript.ext("kotlinVersion", "\1.2.51\""));
|
||||
assertThatIllegalStateException().isThrownBy(() -> {
|
||||
generateBuild(build);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithBuiltinPlugin() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addPlugin("java");
|
||||
build.addPlugin("war");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("plugins {", " java", " war", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithKotlinPluginAndVersion() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addPlugin("org.jetbrains.kotlin.jvm", "1.3.21");
|
||||
build.addPlugin("org.jetbrains.kotlin.plugin.spring", "1.3.21");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("plugins {",
|
||||
" kotlin(\"jvm\") version \"1.3.21\"",
|
||||
" kotlin(\"plugin.spring\") version \"1.3.21\"", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithPluginAndVersion() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addPlugin("org.springframework.boot", "2.1.0.RELEASE");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("plugins {",
|
||||
" id(\"org.springframework.boot\") version \"2.1.0.RELEASE\"", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithApplyPlugin() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.applyPlugin("io.spring.dependency-management");
|
||||
|
||||
assertThatIllegalStateException().isThrownBy(() -> generateBuild(build));
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithMavenCentralRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("maven-central");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {", " mavenCentral()", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-milestones", "Spring Milestones",
|
||||
"https://repo.spring.io/milestone");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/milestone\") }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithSnapshotRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.repositories().add("spring-snapshots", "Spring Snapshots",
|
||||
"https://repo.spring.io/snapshot", true);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/snapshot\") }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones",
|
||||
"https://repo.spring.io/milestone");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).doesNotContain("repositories {");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTaskWithTypesCustomizedWithNestedAssignments()
|
||||
throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTasksWithType("KotlinCompile", (task) -> {
|
||||
task.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
kotlinOptions.set("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
kotlinOptions.set("jvmTarget", "\"1.8\"");
|
||||
});
|
||||
});
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.withType<KotlinCompile> {",
|
||||
" kotlinOptions {",
|
||||
" freeCompilerArgs = listOf(\"-Xjsr305=strict\")",
|
||||
" jvmTarget = \"1.8\"", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithInvocations() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("asciidoctor", (task) -> {
|
||||
task.invoke("inputs.dir", "snippetsDir");
|
||||
task.invoke("dependsOn", "test");
|
||||
});
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.asciidoctor {",
|
||||
" inputs.dir(snippetsDir)", " dependsOn(test)", "}");
|
||||
}
|
||||
|
||||
@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\"");
|
||||
});
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.compileKotlin {",
|
||||
" kotlinOptions.freeCompilerArgs = listOf(\"-Xjsr305=strict\")",
|
||||
" kotlinOptions.jvmTarget = \"1.8\"", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTaskCustomizedWithNestedCustomization() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeTask("compileKotlin", (compileKotlin) -> compileKotlin
|
||||
.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
kotlinOptions.set("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")");
|
||||
kotlinOptions.set("jvmTarget", "\"1.8\"");
|
||||
}));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("tasks.compileKotlin {", " kotlinOptions {",
|
||||
" freeCompilerArgs = listOf(\"-Xjsr305=strict\")",
|
||||
" jvmTarget = \"1.8\"", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithExt() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setGroup("com.example.demo");
|
||||
build.setArtifact("demo");
|
||||
build.ext("java.version", "1.8").ext("alpha", "a");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("extra[\"alpha\"] = \"a\"",
|
||||
"extra[\"java.version\"] = \"1.8\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithVersionProperties() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addVersionProperty(VersionProperty.of("version.property"), "1.2.3");
|
||||
build.addInternalVersionProperty("internal.property", "4.5.6");
|
||||
build.addExternalVersionProperty("external.property", "7.8.9");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("extra[\"external.property\"] = \"7.8.9\"",
|
||||
"extra[\"internalProperty\"] = \"4.5.6\"",
|
||||
"extra[\"versionProperty\"] = \"1.2.3\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("kotlin-stdlib", "org.jetbrains.kotlin",
|
||||
"kotlin-stdlib-jdk8", VersionReference.ofProperty("kotlin.version"),
|
||||
DependencyScope.COMPILE);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithExternalVersionedDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("acme", "com.example", "acme",
|
||||
VersionReference.ofProperty(VersionProperty.of("acme.version", false)),
|
||||
DependencyScope.COMPILE);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"com.example:acme:${property(\"acme.version\")}\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithExtAndVersionProperties() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setGroup("com.example.demo");
|
||||
build.setArtifact("demo");
|
||||
build.addInternalVersionProperty("test-version", "1.0");
|
||||
build.addExternalVersionProperty("alpha-version", "0.1");
|
||||
build.ext("myProperty", "42");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("extra[\"myProperty\"] = \"42\"",
|
||||
"extra[\"alpha-version\"] = \"0.1\"", "extra[\"testVersion\"] = \"1.0\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithConfiguration() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.addConfiguration("developmentOnly");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomization() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeConfiguration("developmentOnly",
|
||||
(configuration) -> configuration.extendsFrom("compile"));
|
||||
build.customizeConfiguration("developmentOnly",
|
||||
(configuration) -> configuration.extendsFrom("testCompile"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly {",
|
||||
" extendsFrom(configurations.compile.get(), configurations.testCompile.get())",
|
||||
" }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithConfigurationCustomizations() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.customizeConfiguration("developmentOnly",
|
||||
(configuration) -> configuration.extendsFrom("compile"));
|
||||
build.customizeConfiguration("testOnly",
|
||||
(configuration) -> configuration.extendsFrom("testCompile"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("configurations {", " developmentOnly {",
|
||||
" extendsFrom(configurations.compile.get())", " }",
|
||||
" testOnly {", " extendsFrom(configurations.testCompile.get())",
|
||||
" }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithAnnotationProcessorDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("annotation-processor", "org.springframework.boot",
|
||||
"spring-boot-configuration-processor",
|
||||
DependencyScope.ANNOTATION_PROCESSOR);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" annotationProcessor(\"org.springframework.boot:spring-boot-configuration-processor\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCompileDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", DependencyScope.COMPILE);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"org.springframework.boot:spring-boot-starter\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("driver", "com.example", "jdbc-driver",
|
||||
VersionReference.ofValue("1.0.0"), DependencyScope.RUNTIME);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" runtimeOnly(\"com.example:jdbc-driver:1.0.0\")", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithProvidedRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("tomcat", "org.springframework.boot",
|
||||
"spring-boot-starter-tomcat", DependencyScope.PROVIDED_RUNTIME);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" providedRuntime(\"org.springframework.boot:spring-boot-starter-tomcat\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTestCompileDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("test", "org.springframework.boot",
|
||||
"spring-boot-starter-test", DependencyScope.TEST_COMPILE);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" testImplementation(\"org.springframework.boot:spring-boot-starter-test\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCompileOnlyDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("test", "org.springframework.boot",
|
||||
"spring-boot-starter-foobar", DependencyScope.COMPILE_ONLY);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" compileOnly(\"org.springframework.boot:spring-boot-starter-foobar\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithTestRuntimeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("embed-mongo", "de.flapdoodle.embed",
|
||||
"de.flapdoodle.embed.mongo", DependencyScope.TEST_RUNTIME);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" testRuntimeOnly(\"de.flapdoodle.embed:de.flapdoodle.embed.mongo\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithNonNullArtifactTypeDependency() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.dependencies().add("root", "org.springframework.boot",
|
||||
"spring-boot-starter", null, DependencyScope.COMPILE, "tar.gz");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencies {",
|
||||
" implementation(\"org.springframework.boot:spring-boot-starter@tar.gz\")",
|
||||
"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithBom() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("test", "com.example", "my-project-dependencies",
|
||||
VersionReference.ofValue("1.0.0.RELEASE"));
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")",
|
||||
" }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithOrderedBoms() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.boms().add("bom1", "com.example", "my-project-dependencies",
|
||||
VersionReference.ofValue("1.0.0.RELEASE"), 5);
|
||||
build.boms().add("bom2", "com.example", "root-dependencies",
|
||||
VersionReference.ofProperty("root.version"), 2);
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).containsSequence("dependencyManagement {", " imports {",
|
||||
" mavenBom(\"com.example:my-project-dependencies:1.0.0.RELEASE\")",
|
||||
" mavenBom(\"com.example:root-dependencies:${rootVersion}\")",
|
||||
" }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithCustomVersion() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setVersion("1.2.4.RELEASE");
|
||||
List<String> lines = generateBuild(build);
|
||||
assertThat(lines).contains("version = \"1.2.4.RELEASE\"");
|
||||
}
|
||||
|
||||
private List<String> generateBuild(GradleBuild build) throws IOException {
|
||||
GradleBuildWriter writer = new KotlinDslGradleBuildWriter();
|
||||
StringWriter out = new StringWriter();
|
||||
writer.writeTo(new IndentingWriter(out), build);
|
||||
return Arrays.asList(out.toString().split("\\r?\\n"));
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* http://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.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.io.IndentingWriter;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link KotlinDslGradleSettingsWriter}.
|
||||
*
|
||||
* @author Jean-Baptiste Nizet
|
||||
*/
|
||||
class KotlinDslGradleSettingsWriterTests {
|
||||
|
||||
@Test
|
||||
void gradleBuildWithMavenCentralPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("maven-central");
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" mavenCentral()", " gradlePluginPortal()", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-milestones", "Spring Milestones",
|
||||
"https://repo.spring.io/milestone");
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/milestone\") }",
|
||||
" gradlePluginPortal()", " }", " resolutionStrategy {",
|
||||
" eachPlugin {",
|
||||
" if (requested.id.id == \"org.springframework.boot\") {",
|
||||
" useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")",
|
||||
" }", " }", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gradleBuildWithSnapshotPluginRepository() throws IOException {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.pluginRepositories().add("spring-snapshots", "Spring Snapshots",
|
||||
"https://repo.spring.io/snapshot", true);
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
|
||||
" maven { url = uri(\"https://repo.spring.io/snapshot\") }",
|
||||
" gradlePluginPortal()", " }", " resolutionStrategy {",
|
||||
" eachPlugin {",
|
||||
" if (requested.id.id == \"org.springframework.boot\") {",
|
||||
" useModule(\"org.springframework.boot:spring-boot-gradle-plugin:${requested.version}\")",
|
||||
" }", " }", " }", "}");
|
||||
}
|
||||
|
||||
@Test
|
||||
void artifactIdShouldBeUsedAsTheRootProjectName() throws Exception {
|
||||
GradleBuild build = new GradleBuild();
|
||||
build.setArtifact("my-application");
|
||||
List<String> lines = generateSettings(build);
|
||||
assertThat(lines).containsSequence("rootProject.name = \"my-application\"");
|
||||
}
|
||||
|
||||
private List<String> generateSettings(GradleBuild build) throws IOException {
|
||||
GradleSettingsWriter writer = new KotlinDslGradleSettingsWriter();
|
||||
StringWriter out = new StringWriter();
|
||||
writer.writeTo(new IndentingWriter(out), build);
|
||||
return Arrays.asList(out.toString().split("\\r?\\n"));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user