Polish "Add Gradle Kotlin DSL support"

In particular, this commit replaces the GradleKts dedicated build system
in favor of a dedicated build system dialect.

Closes gh-851

Co-authored-by: Andy Wilkinson <awilkinson@pivotal.io>
This commit is contained in:
Stephane Nicoll 2019-05-02 16:41:46 +02:00
parent 239116b29a
commit c7c16872c4
57 changed files with 523 additions and 776 deletions

View File

@ -34,6 +34,11 @@
<scope>test</scope>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>

View File

@ -19,7 +19,6 @@ package io.spring.initializr.generator.spring.build;
import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.DependencyScope;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnLanguage;
@ -72,7 +71,7 @@ public class BuildProjectGenerationConfiguration {
@Bean
@ConditionalOnLanguage(KotlinLanguage.ID)
@ConditionalOnBuildSystem({ GradleBuildSystem.ID, GradleKtsBuildSystem.ID })
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
public KotlinJpaGradleBuildCustomizer kotlinJpaGradleBuildCustomizer(
InitializrMetadata metadata, KotlinProjectSettings settings) {
return new KotlinJpaGradleBuildCustomizer(metadata, settings);

View File

@ -29,14 +29,12 @@ import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} template for the project's {@code build.gradle} or
* {@code build.gradle.kts} file. A subclass exists for each DSL.
* {@link ProjectContributor} for the project's main build file.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
public abstract class GradleBuildProjectContributor
implements BuildWriter, ProjectContributor {
public class GradleBuildProjectContributor implements BuildWriter, ProjectContributor {
private final GradleBuildWriter buildWriter;
@ -46,9 +44,8 @@ public abstract class GradleBuildProjectContributor
private final String buildFileName;
protected GradleBuildProjectContributor(GradleBuildWriter buildWriter,
GradleBuild build, IndentingWriterFactory indentingWriterFactory,
String buildFileName) {
GradleBuildProjectContributor(GradleBuildWriter buildWriter, GradleBuild build,
IndentingWriterFactory indentingWriterFactory, String buildFileName) {
this.buildWriter = buildWriter;
this.build = build;
this.indentingWriterFactory = indentingWriterFactory;
@ -56,13 +53,13 @@ public abstract class GradleBuildProjectContributor
}
@Override
public final void contribute(Path projectRoot) throws IOException {
public void contribute(Path projectRoot) throws IOException {
Path buildGradle = Files.createFile(projectRoot.resolve(this.buildFileName));
writeBuild(Files.newBufferedWriter(buildGradle));
}
@Override
public final void writeBuild(Writer out) throws IOException {
public void writeBuild(Writer out) throws IOException {
try (IndentingWriter writer = this.indentingWriterFactory
.createIndentingWriter("gradle", out)) {
this.buildWriter.writeTo(writer, this.build);

View File

@ -23,9 +23,10 @@ import io.spring.initializr.generator.buildsystem.BuildItemResolver;
import io.spring.initializr.generator.buildsystem.gradle.Gradle3BuildWriter;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleBuildWriter;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleSettingsWriter;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleBuildWriter;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleSettingsWriter;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnLanguage;
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
@ -51,7 +52,7 @@ import org.springframework.context.annotation.Configuration;
* @author Jean-Baptiste Nizet
*/
@ProjectGenerationConfiguration
@ConditionalOnBuildSystem({ GradleBuildSystem.ID, GradleKtsBuildSystem.ID })
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
public class GradleProjectGenerationConfiguration {
private final IndentingWriterFactory indentingWriterFactory;
@ -104,25 +105,28 @@ public class GradleProjectGenerationConfiguration {
@Bean
@ConditionalOnPlatformVersion("2.0.0.M1")
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_GROOVY)
public BuildCustomizer<GradleBuild> applyDependencyManagementPluginContributor() {
return (build) -> build.applyPlugin("io.spring.dependency-management");
}
@Bean
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_GROOVY)
public GradleBuildProjectContributor gradleBuildProjectContributor(
GroovyDslGradleBuildWriter buildWriter, GradleBuild build) {
return new GroovyDslGradleBuildProjectContributor(buildWriter, build,
this.indentingWriterFactory);
return new GradleBuildProjectContributor(buildWriter, build,
this.indentingWriterFactory, "build.gradle");
}
@Bean
@ConditionalOnBuildSystem(GradleKtsBuildSystem.ID)
public KotlinDslGradleBuildProjectContributor gradleKtsBuildProjectContributor(
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_KOTLIN)
public GradleBuildProjectContributor gradleKtsBuildProjectContributor(
KotlinDslGradleBuildWriter buildWriter, GradleBuild build) {
return new KotlinDslGradleBuildProjectContributor(buildWriter, build,
this.indentingWriterFactory);
return new GradleBuildProjectContributor(buildWriter, build,
this.indentingWriterFactory, "build.gradle.kts");
}
/**
@ -195,7 +199,8 @@ public class GradleProjectGenerationConfiguration {
* Configuration specific to projects using Gradle (Groovy DSL) 4 or 5.
*/
@Configuration
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_GROOVY)
@ConditionalOnGradleVersion({ "4", "5" })
static class Gradle4Or5ProjectGenerationConfiguration {
@ -207,8 +212,8 @@ public class GradleProjectGenerationConfiguration {
@Bean
public SettingsGradleProjectContributor settingsGradleProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new GroovyDslSettingsGradleProjectContributor(build,
indentingWriterFactory);
return new SettingsGradleProjectContributor(build, indentingWriterFactory,
new GroovyDslGradleSettingsWriter(), "settings.gradle");
}
@Bean
@ -229,7 +234,9 @@ public class GradleProjectGenerationConfiguration {
* Configuration specific to projects using Gradle (Kotlin DSL).
*/
@Configuration
@ConditionalOnBuildSystem(GradleKtsBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_KOTLIN)
@ConditionalOnGradleVersion("5")
static class GradleKtsProjectGenerationConfiguration {
@Bean
@ -238,10 +245,10 @@ public class GradleProjectGenerationConfiguration {
}
@Bean
public KotlinDslSettingsGradleProjectContributor settingsGradleKtsProjectContributor(
public SettingsGradleProjectContributor settingsGradleKtsProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new KotlinDslSettingsGradleProjectContributor(build,
indentingWriterFactory);
return new SettingsGradleProjectContributor(build, indentingWriterFactory,
new KotlinDslGradleSettingsWriter(), "settings.gradle.kts");
}
@Bean

View File

@ -1,37 +0,0 @@
/*
* 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.spring.build.gradle;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleBuildWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} for the project's {@code build.gradle} file.
*
* @author Jean-Baptiste Nizet
*/
public class GroovyDslGradleBuildProjectContributor
extends GradleBuildProjectContributor {
GroovyDslGradleBuildProjectContributor(GroovyDslGradleBuildWriter buildWriter,
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
super(buildWriter, build, indentingWriterFactory, "build.gradle");
}
}

View File

@ -1,37 +0,0 @@
/*
* 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.spring.build.gradle;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleSettingsWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} for the project's {@code settings.gradle} file.
*
* @author Jean-Baptiste Nizet
*/
class GroovyDslSettingsGradleProjectContributor extends SettingsGradleProjectContributor {
GroovyDslSettingsGradleProjectContributor(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) {
super(build, indentingWriterFactory, new GroovyDslGradleSettingsWriter(),
"settings.gradle");
}
}

View File

@ -1,37 +0,0 @@
/*
* 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.spring.build.gradle;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleBuildWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} for the project's {@code build.gradle.kts} file.
*
* @author Jean-Baptiste Nizet
*/
public class KotlinDslGradleBuildProjectContributor
extends GradleBuildProjectContributor {
KotlinDslGradleBuildProjectContributor(KotlinDslGradleBuildWriter buildWriter,
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
super(buildWriter, build, indentingWriterFactory, "build.gradle.kts");
}
}

View File

@ -1,37 +0,0 @@
/*
* 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.spring.build.gradle;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleSettingsWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} for the project's {@code settings.gradle.kts} file.
*
* @author Jean-Baptiste Nizet
*/
class KotlinDslSettingsGradleProjectContributor extends SettingsGradleProjectContributor {
KotlinDslSettingsGradleProjectContributor(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) {
super(build, indentingWriterFactory, new KotlinDslGradleSettingsWriter(),
"settings.gradle.kts");
}
}

View File

@ -27,13 +27,12 @@ import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.project.contributor.ProjectContributor;
/**
* {@link ProjectContributor} for the project's {@code settings.gradle}
* {@code settings.gradle.kts} or file. A subclass exists for each of the DSLs.
* {@link ProjectContributor} for the project's settings file.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
abstract class SettingsGradleProjectContributor implements ProjectContributor {
class SettingsGradleProjectContributor implements ProjectContributor {
private final GradleBuild build;
@ -43,7 +42,7 @@ abstract class SettingsGradleProjectContributor implements ProjectContributor {
private final String settingsFileName;
protected SettingsGradleProjectContributor(GradleBuild build,
SettingsGradleProjectContributor(GradleBuild build,
IndentingWriterFactory indentingWriterFactory,
GradleSettingsWriter settingsWriter, String settingsFileName) {
this.build = build;
@ -53,7 +52,7 @@ abstract class SettingsGradleProjectContributor implements ProjectContributor {
}
@Override
public final void contribute(Path projectRoot) throws IOException {
public void contribute(Path projectRoot) throws IOException {
Path settingsGradle = Files
.createFile(projectRoot.resolve(this.settingsFileName));
try (IndentingWriter writer = this.indentingWriterFactory.createIndentingWriter(

View File

@ -20,7 +20,6 @@ import java.lang.reflect.Modifier;
import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
@ -123,7 +122,7 @@ class GroovyProjectGenerationDefaultContributorsConfiguration {
* Configuration for Groovy projects built with Gradle.
*/
@Configuration
@ConditionalOnBuildSystem({ GradleBuildSystem.ID, GradleKtsBuildSystem.ID })
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
static class GroovyGradleProjectConfiguration {
@Bean

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -33,12 +33,13 @@ class GroovyDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
}
@Override
protected void customizeKotlinOptions(TaskCustomization compile) {
protected void customizeKotlinOptions(KotlinProjectSettings settings,
TaskCustomization compile) {
compile.nested("kotlinOptions", (kotlinOptions) -> {
String compilerArgs = this.settings.getCompilerArgs().stream()
String compilerArgs = settings.getCompilerArgs().stream()
.map((arg) -> "'" + arg + "'").collect(Collectors.joining(", "));
kotlinOptions.set("freeCompilerArgs", "[" + compilerArgs + "]");
kotlinOptions.set("jvmTarget", "'" + this.settings.getJvmTarget() + "'");
kotlinOptions.set("jvmTarget", "'" + settings.getJvmTarget() + "'");
});
}

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -33,12 +33,13 @@ class KotlinDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
}
@Override
protected void customizeKotlinOptions(TaskCustomization compile) {
protected void customizeKotlinOptions(KotlinProjectSettings settings,
TaskCustomization compile) {
compile.nested("kotlinOptions", (kotlinOptions) -> {
String compilerArgs = this.settings.getCompilerArgs().stream()
String compilerArgs = settings.getCompilerArgs().stream()
.map((arg) -> "\"" + arg + "\"").collect(Collectors.joining(", "));
kotlinOptions.set("freeCompilerArgs", "listOf(" + compilerArgs + ")");
kotlinOptions.set("jvmTarget", "\"" + this.settings.getJvmTarget() + "\"");
kotlinOptions.set("jvmTarget", "\"" + settings.getJvmTarget() + "\"");
});
}

View File

@ -21,28 +21,30 @@ import io.spring.initializr.generator.buildsystem.gradle.GradleBuild.TaskCustomi
import io.spring.initializr.generator.spring.build.BuildCustomizer;
/**
* {@link BuildCustomizer} template for Kotlin projects build with Gradle. A subclass
* exists for each DSL.
* {@link BuildCustomizer} abstraction for Kotlin projects build with Gradle.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
* @see GroovyDslKotlinGradleBuildCustomizer
* @see KotlinDslKotlinGradleBuildCustomizer
*/
abstract class KotlinGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {
protected final KotlinProjectSettings settings;
private final KotlinProjectSettings settings;
KotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) {
this.settings = kotlinProjectSettings;
}
@Override
public final void customize(GradleBuild build) {
public void customize(GradleBuild build) {
build.addPlugin("org.jetbrains.kotlin.jvm", this.settings.getVersion());
build.addPlugin("org.jetbrains.kotlin.plugin.spring", this.settings.getVersion());
build.customizeTasksWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile",
this::customizeKotlinOptions);
(compile) -> customizeKotlinOptions(this.settings, compile));
}
protected abstract void customizeKotlinOptions(TaskCustomization compile);
protected abstract void customizeKotlinOptions(KotlinProjectSettings settings,
TaskCustomization compile);
}

View File

@ -18,7 +18,6 @@ package io.spring.initializr.generator.spring.code.kotlin;
import io.spring.initializr.generator.buildsystem.Build;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnPackaging;
@ -162,7 +161,8 @@ class KotlinProjectGenerationDefaultContributorsConfiguration {
* @author Andy Wilkinson
*/
@Configuration
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_GROOVY)
static class KotlinGradleProjectConfiguration {
@Bean
@ -179,7 +179,8 @@ class KotlinProjectGenerationDefaultContributorsConfiguration {
* @author Jean-Baptiste Nizet
*/
@Configuration
@ConditionalOnBuildSystem(GradleKtsBuildSystem.ID)
@ConditionalOnBuildSystem(id = GradleBuildSystem.ID,
dialect = GradleBuildSystem.DIALECT_KOTLIN)
static class KotlinGradleKtsProjectConfiguration {
@Bean

View File

@ -17,7 +17,6 @@
package io.spring.initializr.generator.spring.scm.git;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
@ -57,7 +56,7 @@ public class GitProjectGenerationConfiguration {
}
@Bean
@ConditionalOnBuildSystem({ GradleBuildSystem.ID, GradleKtsBuildSystem.ID })
@ConditionalOnBuildSystem(GradleBuildSystem.ID)
public GitIgnoreCustomizer gradleGitIgnoreCustomizer() {
return (gitIgnore) -> {
gitIgnore.getGeneral().add(".gradle", "/build/",

View File

@ -20,7 +20,6 @@ import java.util.stream.Stream;
import io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
import io.spring.initializr.generator.language.Language;
import io.spring.initializr.generator.language.groovy.GroovyLanguage;
@ -64,9 +63,12 @@ class BuildComplianceTests extends AbstractComplianceTests {
}
static Stream<Arguments> parameters() {
return Stream.concat(previousGenerationParameters(),
Stream.of(Arguments.arguments(BuildSystem.forId(GradleKtsBuildSystem.ID),
"build.gradle.kts")));
return Stream
.concat(previousGenerationParameters(),
Stream.of(Arguments.arguments(
BuildSystem.forIdAndDialect(GradleBuildSystem.ID,
GradleBuildSystem.DIALECT_KOTLIN),
"build.gradle.kts")));
}
@ParameterizedTest

View File

@ -0,0 +1,133 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleBuildWriter;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleBuildWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.test.io.TextTestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GradleBuildProjectContributor}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Jean-Baptiste Nizet
*/
class GradleBuildProjectContributorTests {
@Test
void groovyDslGradleBuildIsContributedInProjectStructure(@TempDir Path projectDir)
throws IOException {
GradleBuild build = new GradleBuild();
groovyDslGradleBuildProjectContributor(build,
IndentingWriterFactory.withDefaultSettings()).contribute(projectDir);
Path buildGradle = projectDir.resolve("build.gradle");
assertThat(buildGradle).isRegularFile();
}
@Test
void groovyDslGradleBuildIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.setGroup("com.example");
build.setVersion("1.0.0-SNAPSHOT");
build.buildscript((buildscript) -> buildscript.ext("someVersion", "'1.2.3'"));
List<String> lines = generateBuild(groovyDslGradleBuildProjectContributor(build,
IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("buildscript {", " ext {",
" someVersion = '1.2.3'", " }", "}");
assertThat(lines).containsSequence("group = 'com.example'",
"version = '1.0.0-SNAPSHOT'");
}
@Test
void groovyDslGradleBuildIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.buildscript((buildscript) -> buildscript.ext("someVersion", "'1.2.3'"));
List<String> lines = generateBuild(
groovyDslGradleBuildProjectContributor(build, indentingWriterFactory));
assertThat(lines).containsSequence("buildscript {", " ext {",
" someVersion = '1.2.3'", " }", "}");
}
@Test
void kotlinDslGradleBuildIsContributedInProjectStructure(@TempDir Path projectDir)
throws IOException {
GradleBuild build = new GradleBuild();
kotlinDslGradleBuildProjectContributor(build,
IndentingWriterFactory.withDefaultSettings()).contribute(projectDir);
Path buildGradleKts = projectDir.resolve("build.gradle.kts");
assertThat(buildGradleKts).isRegularFile();
}
@Test
void kotlinDslGradleBuildIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.setGroup("com.example");
build.setVersion("1.0.0-SNAPSHOT");
List<String> lines = generateBuild(kotlinDslGradleBuildProjectContributor(build,
IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("group = \"com.example\"",
"version = \"1.0.0-SNAPSHOT\"");
}
@Test
void kotlinDslGradleBuildIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.addPlugin("java");
List<String> lines = generateBuild(
kotlinDslGradleBuildProjectContributor(build, indentingWriterFactory));
assertThat(lines).containsSequence("plugins {", " java", "}");
}
private List<String> generateBuild(GradleBuildProjectContributor contributor)
throws IOException {
StringWriter writer = new StringWriter();
contributor.writeBuild(writer);
return TextTestUtils.readAllLines(writer.toString());
}
private GradleBuildProjectContributor groovyDslGradleBuildProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new GradleBuildProjectContributor(new GroovyDslGradleBuildWriter(), build,
indentingWriterFactory, "build.gradle");
}
private GradleBuildProjectContributor kotlinDslGradleBuildProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new GradleBuildProjectContributor(new KotlinDslGradleBuildWriter(), build,
indentingWriterFactory, "build.gradle.kts");
}
}

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -25,14 +25,15 @@ import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import io.spring.initializr.generator.buildsystem.BuildWriter;
import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.buildsystem.DependencyScope;
import io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleBuildWriter;
import io.spring.initializr.generator.language.java.JavaLanguage;
import io.spring.initializr.generator.packaging.war.WarPackaging;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.build.BuildProjectGenerationConfiguration;
import io.spring.initializr.generator.spring.build.BuildWriter;
import io.spring.initializr.generator.spring.test.InitializrMetadataTestBuilder;
import io.spring.initializr.generator.test.project.ProjectAssetTester;
import io.spring.initializr.generator.test.project.ProjectStructure;
@ -45,6 +46,7 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.StreamUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -66,8 +68,8 @@ class GradleKtsProjectGenerationConfigurationTests {
.withDirectory(directory)
.withBean(InitializrMetadata.class,
() -> InitializrMetadataTestBuilder.withDefaults().build())
.withDescriptionCustomizer((description) -> description
.setBuildSystem(new GradleKtsBuildSystem()));
.withDescriptionCustomizer((description) -> description.setBuildSystem(
new GradleBuildSystem(GradleBuildSystem.DIALECT_KOTLIN)));
}
static Stream<Arguments> supportedPlatformVersions() {
@ -83,12 +85,13 @@ class GradleKtsProjectGenerationConfigurationTests {
description.setLanguage(new JavaLanguage());
BuildWriter buildWriter = this.projectTester.generate(description,
(context) -> context.getBean(BuildWriter.class));
assertThat(buildWriter)
.isInstanceOf(KotlinDslGradleBuildProjectContributor.class);
assertThat(buildWriter).isInstanceOf(GradleBuildProjectContributor.class);
assertThat(ReflectionTestUtils.getField(buildWriter, "buildWriter"))
.isInstanceOf(KotlinDslGradleBuildWriter.class);
}
static Stream<Arguments> gradleWrapperParameters() {
return Stream.of(Arguments.arguments("2.1.3.RELEASE", "5.2.1"));
return Stream.of(Arguments.arguments("2.1.3.RELEASE", "5.4.1"));
}
@ParameterizedTest(name = "Spring Boot {0}")

View File

@ -27,6 +27,7 @@ import io.spring.initializr.generator.buildsystem.BuildWriter;
import io.spring.initializr.generator.buildsystem.Dependency;
import io.spring.initializr.generator.buildsystem.DependencyScope;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleBuildWriter;
import io.spring.initializr.generator.language.java.JavaLanguage;
import io.spring.initializr.generator.packaging.war.WarPackaging;
import io.spring.initializr.generator.project.ProjectDescription;
@ -43,6 +44,8 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
@ -82,8 +85,8 @@ class GradleProjectGenerationConfigurationTests {
BuildWriter buildWriter = this.projectTester.generate(description,
(context) -> context.getBean(BuildWriter.class));
assertThat(buildWriter).isInstanceOf(GradleBuildProjectContributor.class);
assertThat(buildWriter)
.isNotInstanceOf(KotlinDslGradleBuildProjectContributor.class);
assertThat(ReflectionTestUtils.getField(buildWriter, "buildWriter"))
.isInstanceOf(GroovyDslGradleBuildWriter.class);
}
static Stream<Arguments> gradleWrapperParameters() {

View File

@ -1,91 +0,0 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleBuildWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.test.io.TextTestUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GroovyDslGradleBuildProjectContributor}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Jean-Baptiste Nizet
*/
class GroovyDslGradleBuildProjectContributorTests {
@Test
void gradleBuildIsContributedInProjectStructure(@TempDir Path projectDir)
throws IOException {
GradleBuild build = new GradleBuild();
new GroovyDslGradleBuildProjectContributor(new GroovyDslGradleBuildWriter(),
build, IndentingWriterFactory.withDefaultSettings())
.contribute(projectDir);
Path buildGradle = projectDir.resolve("build.gradle");
assertThat(buildGradle).isRegularFile();
}
@Test
void gradleBuildIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.setGroup("com.example");
build.setVersion("1.0.0-SNAPSHOT");
build.buildscript((buildscript) -> buildscript.ext("someVersion", "'1.2.3'"));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("buildscript {", " ext {",
" someVersion = '1.2.3'", " }", "}");
assertThat(lines).containsSequence("group = 'com.example'",
"version = '1.0.0-SNAPSHOT'");
}
@Test
void gradleBuildIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.buildscript((buildscript) -> buildscript.ext("someVersion", "'1.2.3'"));
List<String> lines = generateBuild(build, indentingWriterFactory);
assertThat(lines).containsSequence("buildscript {", " ext {",
" someVersion = '1.2.3'", " }", "}");
}
private List<String> generateBuild(GradleBuild build) throws IOException {
return generateBuild(build, IndentingWriterFactory.withDefaultSettings());
}
private List<String> generateBuild(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) throws IOException {
StringWriter writer = new StringWriter();
new GroovyDslGradleBuildProjectContributor(new GroovyDslGradleBuildWriter(),
build, indentingWriterFactory).writeBuild(writer);
return TextTestUtils.readAllLines(writer.toString());
}
}

View File

@ -1,86 +0,0 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.test.project.ProjectStructure;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GroovyDslSettingsGradleProjectContributor}.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
class GroovyDslSettingsGradleProjectContributorTests {
@TempDir
Path directory;
@Test
void gradleSettingsIsContributedToProject() 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 gradleSettingsIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(build, indentingWriterFactory);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void gradleSettingsDoesNotUseRepositories() throws IOException {
GradleBuild build = new GradleBuild();
build.repositories().add("maven-central");
List<String> lines = generateSettings(build);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" gradlePluginPortal()", " }", "}");
}
private List<String> generateSettings(GradleBuild build) throws IOException {
return generateSettings(build, IndentingWriterFactory.withDefaultSettings());
}
private List<String> generateSettings(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-");
new GroovyDslSettingsGradleProjectContributor(build, indentingWriterFactory)
.contribute(projectDir);
return new ProjectStructure(projectDir).readAllLines("settings.gradle");
}
}

View File

@ -1,85 +0,0 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleBuildWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link KotlinDslGradleBuildProjectContributor}.
*
* @author Jean-Baptiste Nizet
*/
class KotlinDslGradleBuildProjectContributorTests {
@Test
void gradleBuildIsContributedInProjectStructure(@TempDir Path projectDir)
throws IOException {
GradleBuild build = new GradleBuild();
new KotlinDslGradleBuildProjectContributor(new KotlinDslGradleBuildWriter(),
build, IndentingWriterFactory.withDefaultSettings())
.contribute(projectDir);
Path buildGradleKts = projectDir.resolve("build.gradle.kts");
assertThat(buildGradleKts).isRegularFile();
}
@Test
void gradleBuildIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.setGroup("com.example");
build.setVersion("1.0.0-SNAPSHOT");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("group = \"com.example\"",
"version = \"1.0.0-SNAPSHOT\"");
}
@Test
void gradleBuildIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.addPlugin("java");
List<String> lines = generateBuild(build, indentingWriterFactory);
assertThat(lines).containsSequence("plugins {", " java", "}");
}
private List<String> generateBuild(GradleBuild build) throws IOException {
return generateBuild(build, IndentingWriterFactory.withDefaultSettings());
}
private List<String> generateBuild(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) throws IOException {
StringWriter writer = new StringWriter();
new KotlinDslGradleBuildProjectContributor(new KotlinDslGradleBuildWriter(),
build, indentingWriterFactory).writeBuild(writer);
return Arrays.asList(writer.toString().split("\\r?\\n"));
}
}

View File

@ -1,86 +0,0 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link KotlinDslSettingsGradleProjectContributor}.
*
* @author Jean-Baptiste Nizet
*/
class KotlinDslSettingsGradleProjectContributorTests {
@TempDir
Path directory;
@Test
void gradleSettingsIsContributedToProject() 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 gradleSettingsIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(build, indentingWriterFactory);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void gradleSettingsDoesNotUseRepositories() throws IOException {
GradleBuild build = new GradleBuild();
build.repositories().add("maven-central");
List<String> lines = generateSettings(build);
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" gradlePluginPortal()", " }", "}");
}
private List<String> generateSettings(GradleBuild build) throws IOException {
return generateSettings(build, IndentingWriterFactory.withDefaultSettings());
}
private List<String> generateSettings(GradleBuild build,
IndentingWriterFactory indentingWriterFactory) throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-");
new KotlinDslSettingsGradleProjectContributor(build, indentingWriterFactory)
.contribute(projectDir);
Path settingsGradle = projectDir.resolve("settings.gradle.kts");
assertThat(settingsGradle).isRegularFile();
return Files.readAllLines(settingsGradle);
}
}

View File

@ -0,0 +1,131 @@
/*
* 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.spring.build.gradle;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GroovyDslGradleSettingsWriter;
import io.spring.initializr.generator.buildsystem.gradle.KotlinDslGradleSettingsWriter;
import io.spring.initializr.generator.io.IndentingWriterFactory;
import io.spring.initializr.generator.io.SimpleIndentStrategy;
import io.spring.initializr.generator.test.project.ProjectStructure;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SettingsGradleProjectContributorTests}.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
class SettingsGradleProjectContributorTests {
@TempDir
Path directory;
@Test
void groovyDslGradleSettingsIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(groovyDslSettingsGradleProjectContributor(
build, IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void groovyDslGradleSettingsIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(
groovyDslSettingsGradleProjectContributor(build, indentingWriterFactory));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void groovyDslGradleSettingsDoesNotUseRepositories() throws IOException {
GradleBuild build = new GradleBuild();
build.repositories().add("maven-central");
List<String> lines = generateSettings(groovyDslSettingsGradleProjectContributor(
build, IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" gradlePluginPortal()", " }", "}");
}
@Test
void kotlinDslGradleSettingsIsContributedToProject() throws IOException {
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(kotlinDslSettingsGradleProjectContributor(
build, IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void kotlinDslGradleSettingsIsContributedUsingGradleContentId() throws IOException {
IndentingWriterFactory indentingWriterFactory = IndentingWriterFactory
.create(new SimpleIndentStrategy(" "), (factory) -> factory
.indentingStrategy("gradle", new SimpleIndentStrategy(" ")));
GradleBuild build = new GradleBuild();
build.pluginRepositories().add("maven-central");
List<String> lines = generateSettings(
kotlinDslSettingsGradleProjectContributor(build, indentingWriterFactory));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" mavenCentral()", " gradlePluginPortal()", " }", "}");
}
@Test
void kotlinDslGradleSettingsDoesNotUseRepositories() throws IOException {
GradleBuild build = new GradleBuild();
build.repositories().add("maven-central");
List<String> lines = generateSettings(kotlinDslSettingsGradleProjectContributor(
build, IndentingWriterFactory.withDefaultSettings()));
assertThat(lines).containsSequence("pluginManagement {", " repositories {",
" gradlePluginPortal()", " }", "}");
}
private List<String> generateSettings(SettingsGradleProjectContributor contributor)
throws IOException {
Path projectDir = Files.createTempDirectory(this.directory, "project-");
contributor.contribute(projectDir);
return new ProjectStructure(projectDir).readAllLines("test.gradle");
}
private SettingsGradleProjectContributor groovyDslSettingsGradleProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new SettingsGradleProjectContributor(build, indentingWriterFactory,
new GroovyDslGradleSettingsWriter(), "test.gradle");
}
private SettingsGradleProjectContributor kotlinDslSettingsGradleProjectContributor(
GradleBuild build, IndentingWriterFactory indentingWriterFactory) {
return new SettingsGradleProjectContributor(build, indentingWriterFactory,
new KotlinDslGradleSettingsWriter(), "test.gradle");
}
}

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -25,11 +25,8 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link GroovyDslKotlinGradleBuildCustomizer}.
*
* @author Andy Wilkinson <<<<<<<
* HEAD:initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizerTests.java
* @author Jean-Baptiste Nizet ======= >>>>>>> 7ec6a29a... Configure spring project
* generation using Gradle Kotlin
* DSL:initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizerTests.java
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
class GroovyDslKotlinGradleBuildCustomizerTests {
@ -52,6 +49,8 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
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"));

View File

@ -48,6 +48,8 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
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"));

View File

@ -102,7 +102,7 @@ public class GradleBuildAssert extends AbstractStringAssert<GradleBuildAssert> {
}
for (int i = 0; i < values.length; i += 2) {
builder.append(
String.format("\tset('%s', '%s')%n", values[i], values[i + 1]));
String.format("\tset('%s', \"%s\")%n", values[i], values[i + 1]));
}
builder.append("}");
return contains(builder.toString());

View File

@ -1,26 +0,0 @@
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

View File

@ -14,7 +14,7 @@ repositories {
}
ext {
set('fooVersion', '1.3.3')
set('fooVersion', "1.3.3")
}
dependencies {

View File

@ -14,8 +14,8 @@ repositories {
}
ext {
set('springBarVersion', '0.2.0.RELEASE')
set('spring-foo.version', '0.1.0.RELEASE')
set('springBarVersion', "0.2.0.RELEASE")
set('spring-foo.version', "0.1.0.RELEASE")
}
dependencies {

View File

@ -36,6 +36,15 @@ public interface BuildSystem {
*/
String id();
/**
* The dialect of the build system, or {@code null} if the build system does not
* support multiple dialects.
* @return the dialect or {@code null}
*/
default String dialect() {
return null;
}
default Path getMainDirectory(Path projectRoot, Language language) {
return projectRoot.resolve("src/main/" + language.id());
}
@ -45,13 +54,17 @@ public interface BuildSystem {
}
static BuildSystem forId(String id) {
return forIdAndDialect(id, null);
}
static BuildSystem forIdAndDialect(String id, String dialect) {
return SpringFactoriesLoader
.loadFactories(BuildSystemFactory.class,
BuildSystem.class.getClassLoader())
.stream().map((factory) -> factory.createBuildSystem(id))
.filter(Objects::nonNull).findFirst()
.orElseThrow(() -> new IllegalStateException(
"Unrecognized build system id '" + id + "'"));
.stream().map((factory) -> factory.createBuildSystem(id, dialect))
.filter(Objects::nonNull).findFirst().orElseThrow(
() -> new IllegalStateException("Unrecognized build system id '"
+ id + "' and dialect '" + dialect + "'"));
}
}

View File

@ -31,4 +31,16 @@ public interface BuildSystemFactory {
*/
BuildSystem createBuildSystem(String id);
/**
* Creates and returns a {@link BuildSystem} for the given {@code id} and
* {@code dialect}. If the factory does not recognize the given {@code id} or
* {@code dialect}, {@code null} should be returned.
* @param id the id of the build system
* @param dialect the dialect of the build system
* @return the build system or {@code null}
*/
default BuildSystem createBuildSystem(String id, String dialect) {
return createBuildSystem(id);
}
}

View File

@ -19,8 +19,7 @@ package io.spring.initializr.generator.buildsystem.gradle;
import io.spring.initializr.generator.buildsystem.BuildSystem;
/**
* Gradle {@link BuildSystem} using the Groovy DSL (i.e. settings.gradle and
* build.gradle).
* Gradle {@link BuildSystem}.
*
* @author Andy Wilkinson
*/
@ -31,11 +30,36 @@ public final class GradleBuildSystem implements BuildSystem {
*/
public static final String ID = "gradle";
/**
* Gradle build using the Groovy DSL.
*/
public static final String DIALECT_GROOVY = "groovy";
/**
* Gradle build using the Kotlin DSL.
*/
public static final String DIALECT_KOTLIN = "kotlin";
private final String dialect;
public GradleBuildSystem() {
this(DIALECT_GROOVY);
}
public GradleBuildSystem(String dialect) {
this.dialect = dialect;
}
@Override
public String id() {
return ID;
}
@Override
public String dialect() {
return this.dialect;
}
@Override
public String toString() {
return id();

View File

@ -28,8 +28,19 @@ class GradleBuildSystemFactory implements BuildSystemFactory {
@Override
public BuildSystem createBuildSystem(String id) {
return createBuildSystem(id, null);
}
@Override
public BuildSystem createBuildSystem(String id, String dialect) {
if (GradleBuildSystem.ID.equals(id)) {
return new GradleBuildSystem();
if (dialect == null) {
return new GradleBuildSystem();
}
if (dialect.equals(GradleBuildSystem.DIALECT_GROOVY)
|| dialect.equals(GradleBuildSystem.DIALECT_KOTLIN)) {
return new GradleBuildSystem(dialect);
}
}
return null;
}

View File

@ -43,12 +43,13 @@ import io.spring.initializr.generator.version.VersionProperty;
import io.spring.initializr.generator.version.VersionReference;
/**
* 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.
* {@link GradleBuild} writer abstraction.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Jean-Baptiste Nizet
* @see GroovyDslGradleBuildWriter
* @see KotlinDslGradleBuildWriter
*/
public abstract class GradleBuildWriter {
@ -116,7 +117,7 @@ public abstract class GradleBuildWriter {
}
Map<String, String> allProperties = new LinkedHashMap<>(build.getExt());
build.getVersionProperties().entrySet().forEach((entry) -> allProperties
.put(getVersionPropertyKey(entry), "'" + entry.getValue() + "'"));
.put(getVersionPropertyKey(entry), "\"" + entry.getValue() + "\""));
writeExtraProperties(writer, allProperties);
}

View File

@ -1,44 +0,0 @@
/*
* 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 io.spring.initializr.generator.buildsystem.BuildSystem;
/**
* Gradle {@link BuildSystem} using the Kotlin DSL (i.e. settings.gradle.kts and
* build.gradle.kts).
*
* @author Jean-Baptiste Nizet
*/
public final class GradleKtsBuildSystem implements BuildSystem {
/**
* Gradle KTS {@link BuildSystem} identifier.
*/
public static final String ID = "gradle-kts";
@Override
public String id() {
return ID;
}
@Override
public String toString() {
return id();
}
}

View File

@ -1,37 +0,0 @@
/*
* 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 io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.buildsystem.BuildSystemFactory;
/**
* {@link BuildSystemFactory Factory} for {@link GradleKtsBuildSystem}.
*
* @author Jean-Baptiste Nizet
*/
class GradleKtsBuildSystemFactory implements BuildSystemFactory {
@Override
public BuildSystem createBuildSystem(String id) {
if (GradleKtsBuildSystem.ID.equals(id)) {
return new GradleKtsBuildSystem();
}
return null;
}
}

View File

@ -22,12 +22,12 @@ import io.spring.initializr.generator.buildsystem.MavenRepository;
import io.spring.initializr.generator.io.IndentingWriter;
/**
* 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.
* {@link GradleBuild} settings abstraction.
*
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
* @see GroovyDslGradleSettingsWriter
* @see KotlinDslGradleSettingsWriter
*/
public abstract class GradleSettingsWriter {

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -119,7 +119,7 @@ public class GroovyDslGradleBuildWriter extends GradleBuildWriter {
}
private String getFormattedExtraProperty(String key, String value) {
return String.format("set('%s', '%s')", key, value);
return String.format("set('%s', %s)", key, value);
}
@Override

View File

@ -5,7 +5,7 @@
* 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
* 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,

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -147,7 +147,7 @@ public class KotlinDslGradleBuildWriter extends GradleBuildWriter {
}
private String getFormattedExtraProperty(String key, String value) {
return String.format("extra[\"%s\"] = \"%s\"", key, value);
return String.format("extra[\"%s\"] = %s", key, value);
}
@Override

View File

@ -5,7 +5,7 @@
* 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
* 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,

View File

@ -25,6 +25,7 @@ import java.lang.annotation.Target;
import io.spring.initializr.generator.buildsystem.BuildSystem;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.annotation.AliasFor;
/**
* Condition that matches when a generated project will use a particular
@ -39,10 +40,24 @@ import org.springframework.context.annotation.Conditional;
public @interface ConditionalOnBuildSystem {
/**
* The {@link BuildSystem} that should be checked. The condition matches when at least
* one build system matches.
* @return the build systems to check
* The ID of the {@link BuildSystem} that should be checked.
* @return the build system to check. An alias for {@link #id()}.
*/
String[] value();
@AliasFor("id")
String value() default "";
/**
* The ID of the {@link BuildSystem} that should be checked.
* @return the build system to check
*/
@AliasFor("value")
String id() default "";
/**
* The dialect of the {@link BuildSystem} that should be checked. When not specified,
* any dialect will be matched.
* @return the dialect to check, if any
*/
String dialect() default "";
}

View File

@ -16,12 +16,13 @@
package io.spring.initializr.generator.condition;
import java.util.Arrays;
import io.spring.initializr.generator.buildsystem.BuildSystem;
import io.spring.initializr.generator.project.ResolvedProjectDescription;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
/**
* {@link ProjectGenerationCondition Condition} implementation for
@ -34,11 +35,18 @@ class OnBuildSystemCondition extends ProjectGenerationCondition {
@Override
protected boolean matches(ResolvedProjectDescription projectDescription,
ConditionContext context, AnnotatedTypeMetadata metadata) {
String[] buildSystemIds = (String[]) metadata
.getAllAnnotationAttributes(ConditionalOnBuildSystem.class.getName())
.getFirst("value");
return Arrays.asList(buildSystemIds)
.contains(projectDescription.getBuildSystem().id());
MultiValueMap<String, Object> attributes = metadata
.getAllAnnotationAttributes(ConditionalOnBuildSystem.class.getName());
String buildSystemId = (String) attributes.getFirst("value");
String dialect = (String) attributes.getFirst("dialect");
BuildSystem buildSystem = projectDescription.getBuildSystem();
if (buildSystem.id().equals(buildSystemId)) {
if (StringUtils.hasText(dialect)) {
return dialect.equals(buildSystem.dialect());
}
return true;
}
return false;
}
}

View File

@ -1,6 +1,5 @@
io.spring.initializr.generator.buildsystem.BuildSystemFactory=\
io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystemFactory,\
io.spring.initializr.generator.buildsystem.gradle.GradleKtsBuildSystemFactory,\
io.spring.initializr.generator.buildsystem.maven.MavenBuildSystemFactory
io.spring.initializr.generator.language.LanguageFactory=\

View File

@ -215,9 +215,9 @@ class GroovyDslGradleBuildWriterTests {
GradleBuild build = new GradleBuild();
build.setGroup("com.example.demo");
build.setArtifact("demo");
build.ext("java.version", "'1.8'").ext("alpha", "file('build/example')");
build.ext("java.version", "'1.8'").ext("alpha", "file(\"build/example\")");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence(" set('alpha', file('build/example'))",
assertThat(lines).containsSequence(" set('alpha', file(\"build/example\"))",
" set('java.version', '1.8')");
}
@ -229,9 +229,9 @@ class GroovyDslGradleBuildWriterTests {
build.addExternalVersionProperty("external.property", "7.8.9");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("ext {",
" set('external.property', '7.8.9')",
" set('internalProperty', '4.5.6')",
" set('versionProperty', '1.2.3')", "}");
" set('external.property', \"7.8.9\")",
" set('internalProperty', \"4.5.6\")",
" set('versionProperty', \"1.2.3\")", "}");
}
@Test
@ -268,7 +268,7 @@ class GroovyDslGradleBuildWriterTests {
build.ext("myProperty", "'42'");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence(" set('myProperty', '42')",
" set('alpha-version', '0.1')", " set('testVersion', '1.0')");
" set('alpha-version', \"0.1\")", " set('testVersion', \"1.0\")");
}
@Test

View File

@ -5,7 +5,7 @@
* 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
* 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,
@ -18,11 +18,11 @@ 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.test.io.TextTestUtils;
import io.spring.initializr.generator.version.VersionProperty;
import io.spring.initializr.generator.version.VersionReference;
import org.junit.jupiter.api.Test;
@ -37,23 +37,6 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/
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();
@ -81,23 +64,18 @@ class KotlinDslGradleBuildWriterTests {
}
@Test
void gradleBuildWithBuildscriptDependency() throws IOException {
void gradleBuildWithBuildscriptDependency() {
GradleBuild build = new GradleBuild();
build.buildscript((buildscript) -> buildscript.dependency(
"org.springframework.boot:spring-boot-gradle-plugin:2.1.0.RELEASE"));
assertThatIllegalStateException().isThrownBy(() -> {
generateBuild(build);
});
assertThatIllegalStateException().isThrownBy(() -> generateBuild(build));
}
@Test
void gradleBuildWithBuildscriptExtProperty() throws IOException {
void gradleBuildWithBuildscriptExtProperty() {
GradleBuild build = new GradleBuild();
build.buildscript((buildscript) -> buildscript.ext("kotlinVersion", "\1.2.51\""));
assertThatIllegalStateException().isThrownBy(() -> {
generateBuild(build);
});
assertThatIllegalStateException().isThrownBy(() -> generateBuild(build));
}
@Test
@ -130,10 +108,9 @@ class KotlinDslGradleBuildWriterTests {
}
@Test
void gradleBuildWithApplyPlugin() throws IOException {
void gradleBuildWithApplyPlugin() {
GradleBuild build = new GradleBuild();
build.applyPlugin("io.spring.dependency-management");
assertThatIllegalStateException().isThrownBy(() -> generateBuild(build));
}
@ -178,17 +155,30 @@ class KotlinDslGradleBuildWriterTests {
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\"");
});
});
build.customizeTasksWithType("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\"")));
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("tasks.withType<KotlinCompile> {",
" kotlinOptions {",
" freeCompilerArgs = listOf(\"-Xjsr305=strict\")",
" jvmTarget = \"1.8\"", " }", "}");
assertThat(lines)
.containsOnlyOnce(
"import org.jetbrains.kotlin.gradle.tasks.KotlinCompile")
.containsSequence("tasks.withType<KotlinCompile> {",
" kotlinOptions {",
" freeCompilerArgs = listOf(\"-Xjsr305=strict\")",
" jvmTarget = \"1.8\"", " }", "}");
}
@Test
void gradleBuildWithTaskWithTypesAndShortTypes() throws IOException {
GradleBuild build = new GradleBuild();
build.customizeTasksWithType("JavaCompile",
(javaCompile) -> javaCompile.set("options.fork", "true"));
assertThat(generateBuild(build)).doesNotContain("import JavaCompile")
.containsSequence("tasks.withType<JavaCompile> {",
" options.fork = true", "}");
}
@Test
@ -235,9 +225,9 @@ class KotlinDslGradleBuildWriterTests {
GradleBuild build = new GradleBuild();
build.setGroup("com.example.demo");
build.setArtifact("demo");
build.ext("java.version", "1.8").ext("alpha", "a");
build.ext("java.version", "\"1.8\"").ext("alpha", "file(\"build/example\")");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("extra[\"alpha\"] = \"a\"",
assertThat(lines).containsSequence("extra[\"alpha\"] = file(\"build/example\")",
"extra[\"java.version\"] = \"1.8\"");
}
@ -286,7 +276,7 @@ class KotlinDslGradleBuildWriterTests {
build.addExternalVersionProperty("alpha-version", "0.1");
build.ext("myProperty", "42");
List<String> lines = generateBuild(build);
assertThat(lines).containsSequence("extra[\"myProperty\"] = \"42\"",
assertThat(lines).containsSequence("extra[\"myProperty\"] = 42",
"extra[\"alpha-version\"] = \"0.1\"", "extra[\"testVersion\"] = \"1.0\"");
}
@ -451,7 +441,7 @@ class KotlinDslGradleBuildWriterTests {
GradleBuildWriter writer = new KotlinDslGradleBuildWriter();
StringWriter out = new StringWriter();
writer.writeTo(new IndentingWriter(out), build);
return Arrays.asList(out.toString().split("\\r?\\n"));
return TextTestUtils.readAllLines(out.toString());
}
}

View File

@ -5,7 +5,7 @@
* 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
* 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,

View File

@ -55,12 +55,11 @@ class ConditionalOnBuildSystemTests {
}
@Test
void outcomeWithSeveralBuildSystemsAndMatchingId() {
void conditionalOnGradleWithKotlinDialectMatchesWhenGradleBuildSystemUsesKotlinDialect() {
ProjectDescription projectDescription = new ProjectDescription();
projectDescription.setBuildSystem(new MavenBuildSystem());
assertThat(candidatesFor(projectDescription, BuildSystemTestConfiguration.class,
MavenOrGradleBuildSystemTestConfiguration.class))
.containsOnlyKeys("maven", "mavenOrGradle");
projectDescription.setBuildSystem(new GradleBuildSystem("kotlin"));
assertThat(candidatesFor(projectDescription, BuildSystemTestConfiguration.class))
.containsOnlyKeys("gradle", "gradleKotlin");
}
private Map<String, String> candidatesFor(ProjectDescription projectDescription,
@ -91,15 +90,10 @@ class ConditionalOnBuildSystemTests {
return "testNone";
}
}
@Configuration
static class MavenOrGradleBuildSystemTestConfiguration {
@Bean
@ConditionalOnBuildSystem({ "gradle", "maven" })
public String mavenOrGradle() {
return "testMavenOrGradle";
@ConditionalOnBuildSystem(id = "gradle", dialect = "kotlin")
public String gradleKotlin() {
return "testGradleKotlinDialect";
}
}