Polish "Use Java configuration for Gradle build"

See gh-1427
This commit is contained in:
Stephane Nicoll 2023-06-14 11:31:08 +02:00
parent a0d98918c5
commit 711d2c36da
6 changed files with 12 additions and 23 deletions

View File

@ -67,7 +67,6 @@ public abstract class GradleBuildWriter {
writeProperty(writer, "version", settings.getVersion());
writer.println();
writeJavaSourceCompatibility(writer, settings);
writer.println();
writeConfigurations(writer, build.configurations());
writeRepositories(writer, build);
writeProperties(writer, build.properties());

View File

@ -92,6 +92,7 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
writer.indented(
() -> writer.println(String.format("sourceCompatibility = '%s'", settings.getSourceCompatibility())));
writer.println("}");
writer.println("");
}
@Override

View File

@ -86,6 +86,7 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
writer.indented(() -> writer.println(
String.format("sourceCompatibility = %s", getJavaVersionConstant(settings.getSourceCompatibility()))));
writer.println("}");
writer.println();
}
private String getJavaVersionConstant(String jvmVersion) {

View File

@ -16,11 +16,8 @@
package io.spring.initializr.generator.buildsystem.gradle;
import java.io.StringWriter;
import java.util.Set;
import io.spring.initializr.generator.io.IndentingWriter;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -93,19 +90,4 @@ public abstract class GradleBuildWriterTests {
protected abstract String write(GradleBuild build);
/**
* A helper method that tests if java configuration is written properly
* @param format in which source compatibility must be written
* @param jvmVersion for the java
* @return the string representation for java configuration
*/
protected String javaConfigurationWriter(String format, String jvmVersion) {
StringWriter stringWriter = new StringWriter();
IndentingWriter writer = new IndentingWriter(stringWriter, new SimpleIndentStrategy("\t"));
writer.println("java {");
writer.indented(() -> writer.println(String.format(format, jvmVersion)));
writer.println("}");
return stringWriter.toString().replace("\r\n", "\n");
}
}

View File

@ -52,8 +52,11 @@ class GroovyDslGradleBuildWriterTests extends GradleBuildWriterTests {
void gradleBuildWithSourceCompatibility() {
GradleBuild build = new GradleBuild();
build.settings().sourceCompatibility("11");
String javaConfiguration = javaConfigurationWriter("sourceCompatibility = '%s'", "11");
assertThat(write(build)).contains(javaConfiguration);
assertThat(write(build)).contains("""
java {
sourceCompatibility = '11'
}
""");
}
@Test

View File

@ -60,8 +60,11 @@ class KotlinDslGradleBuildWriterTests extends GradleBuildWriterTests {
void gradleBuildWithSourceCompatibility15(String sourceCompatibility, String javaVersionConstant) {
GradleBuild build = new GradleBuild();
build.settings().sourceCompatibility(sourceCompatibility);
String javaConfiguration = javaConfigurationWriter("sourceCompatibility = %s", javaVersionConstant);
assertThat(write(build)).contains(javaConfiguration);
assertThat(write(build)).contains("""
java {
sourceCompatibility = %s
}
""".formatted(javaVersionConstant));
}
static Stream<Arguments> sourceCompatibilityParameters() {