From 98bbc9b8ca048829bf3ec6c21c14aecd6bc142bb Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 4 Jun 2024 10:10:29 +0200 Subject: [PATCH] Use non-deprecated Kotlin DSL for Kotlin compile options Closes gh-1495 --- .../GroovyDslKotlinGradleBuildCustomizer.java | 52 ----------- .../KotlinDslKotlinGradleBuildCustomizer.java | 52 ----------- .../kotlin/KotlinGradleBuildCustomizer.java | 43 ++++++--- ...ationDefaultContributorsConfiguration.java | 47 +++------- .../code/kotlin/KotlinProjectSettings.java | 3 +- ...vyDslKotlinGradleBuildCustomizerTests.java | 87 ------------------- ...inDslKotlinGradleBuildCustomizerTests.java | 86 ------------------ .../KotlinGradleBuildCustomizerTests.java | 80 +++++++++++++++++ .../gradle/kotlin-java11-build.gradle.gen | 10 +-- .../gradle/kotlin-java11-build.gradle.kts.gen | 10 +-- .../project/kotlin/standard/build.gradle.gen | 10 +-- .../kotlin/standard/build.gradle.kts.gen | 10 +-- .../kotlin/standard/war-build.gradle.gen | 10 +-- .../kotlin/standard/war-build.gradle.kts.gen | 10 +-- 14 files changed, 157 insertions(+), 353 deletions(-) delete mode 100644 initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizer.java delete mode 100644 initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizer.java delete mode 100644 initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizerTests.java delete mode 100644 initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizerTests.java create mode 100644 initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizerTests.java diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizer.java deleted file mode 100644 index 12d53a3348..0000000000 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizer.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2012-2023 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.code.kotlin; - -import java.util.List; -import java.util.stream.Collectors; - -import io.spring.initializr.generator.buildsystem.gradle.GradleTask; -import io.spring.initializr.generator.spring.build.BuildCustomizer; - -/** - * {@link BuildCustomizer} for Kotlin projects build with Gradle (Groovy DSL). - * - * @author Jean-Baptiste Nizet - */ -class GroovyDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer { - - GroovyDslKotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) { - super(kotlinProjectSettings); - } - - @Override - protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) { - compile.nested("kotlinOptions", (kotlinOptions) -> { - kotlinOptions.append("freeCompilerArgs", compilerArgsAsString(settings.getCompilerArgs())); - kotlinOptions.attribute("jvmTarget", "'" + settings.getJvmTarget() + "'"); - }); - } - - private String compilerArgsAsString(List compilerArgs) { - if (compilerArgs.size() == 1) { - return "'" + compilerArgs.get(0) + "'"; - } - String values = compilerArgs.stream().map((arg) -> "'" + arg + "'").collect(Collectors.joining(", ")); - return "[%s]".formatted(values); - } - -} diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizer.java deleted file mode 100644 index 354cadcb96..0000000000 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizer.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2012-2023 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.code.kotlin; - -import java.util.List; -import java.util.stream.Collectors; - -import io.spring.initializr.generator.buildsystem.gradle.GradleTask; -import io.spring.initializr.generator.spring.build.BuildCustomizer; - -/** - * {@link BuildCustomizer} for Kotlin projects build with Gradle (Kotlin DSL). - * - * @author Jean-Baptiste Nizet - */ -class KotlinDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer { - - KotlinDslKotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) { - super(kotlinProjectSettings); - } - - @Override - protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) { - compile.nested("kotlinOptions", (kotlinOptions) -> { - kotlinOptions.append("freeCompilerArgs", compilerArgsAsString(settings.getCompilerArgs())); - kotlinOptions.attribute("jvmTarget", "\"" + settings.getJvmTarget() + "\""); - }); - } - - private String compilerArgsAsString(List compilerArgs) { - if (compilerArgs.size() == 1) { - return "\"" + compilerArgs.get(0) + "\""; - } - String values = compilerArgs.stream().map((arg) -> "\"" + arg + "\"").collect(Collectors.joining(", ")); - return "listOf(%s)".formatted(values); - } - -} diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizer.java index 41e0e078bd..da7f9624c2 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizer.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -16,24 +16,29 @@ package io.spring.initializr.generator.spring.code.kotlin; +import java.util.List; + import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; -import io.spring.initializr.generator.buildsystem.gradle.GradleTask; import io.spring.initializr.generator.spring.build.BuildCustomizer; +import org.springframework.util.CollectionUtils; + /** - * {@link BuildCustomizer} abstraction for Kotlin projects build with Gradle. + * {@link BuildCustomizer} for Kotlin projects build with Gradle. * * @author Andy Wilkinson * @author Jean-Baptiste Nizet - * @see GroovyDslKotlinGradleBuildCustomizer - * @see KotlinDslKotlinGradleBuildCustomizer + * @author Moritz Halbritter */ -abstract class KotlinGradleBuildCustomizer implements BuildCustomizer { +class KotlinGradleBuildCustomizer implements BuildCustomizer { private final KotlinProjectSettings settings; - KotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) { + private final char quote; + + KotlinGradleBuildCustomizer(KotlinProjectSettings kotlinProjectSettings, char quote) { this.settings = kotlinProjectSettings; + this.quote = quote; } @Override @@ -41,11 +46,27 @@ public void customize(GradleBuild build) { build.plugins().add("org.jetbrains.kotlin.jvm", (plugin) -> plugin.setVersion(this.settings.getVersion())); build.plugins() .add("org.jetbrains.kotlin.plugin.spring", (plugin) -> plugin.setVersion(this.settings.getVersion())); - build.tasks() - .customizeWithType("org.jetbrains.kotlin.gradle.tasks.KotlinCompile", - (compile) -> customizeKotlinOptions(this.settings, compile)); + customizeCompilerOptions(build); + } + + private void customizeCompilerOptions(GradleBuild build) { + build.extensions().customize("kotlin", (kotlin) -> kotlin.nested("compilerOptions", (compilerOptions) -> { + compilerOptions.attributeWithType("jvmTarget", getJvmTarget(), "org.jetbrains.kotlin.gradle.dsl.JvmTarget"); + if (!CollectionUtils.isEmpty(this.settings.getCompilerArgs())) { + compilerOptions.invoke("freeCompilerArgs.addAll", quote(this.settings.getCompilerArgs())); + } + })); + } + + private List quote(List compilerArgs) { + return compilerArgs.stream().map((element) -> this.quote + element + this.quote).toList(); } - protected abstract void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile); + private String getJvmTarget() { + if ("1.8".equals(this.settings.getJvmTarget())) { + return "JvmTarget.JVM_1_8"; + } + return "JvmTarget.JVM_" + this.settings.getJvmTarget(); + } } diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectGenerationDefaultContributorsConfiguration.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectGenerationDefaultContributorsConfiguration.java index 79e51ee731..343b60658f 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectGenerationDefaultContributorsConfiguration.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectGenerationDefaultContributorsConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -46,6 +46,7 @@ * @author Andy Wilkinson * @author Stephane Nicoll * @author Jean-Baptiste Nizet + * @author Moritz Halbritter */ @Configuration class KotlinProjectGenerationDefaultContributorsConfiguration { @@ -78,6 +79,18 @@ KotlinJpaMavenBuildCustomizer kotlinJpaMavenBuildCustomizer(InitializrMetadata m return new KotlinJpaMavenBuildCustomizer(metadata); } + @Bean + @ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN) + KotlinGradleBuildCustomizer kotlinBuildCustomizerKotlinDsl(KotlinProjectSettings kotlinProjectSettings) { + return new KotlinGradleBuildCustomizer(kotlinProjectSettings, '\"'); + } + + @Bean + @ConditionalOnBuildSystem(value = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY) + KotlinGradleBuildCustomizer kotlinBuildCustomizerGroovyDsl(KotlinProjectSettings kotlinProjectSettings) { + return new KotlinGradleBuildCustomizer(kotlinProjectSettings, '\''); + } + /** * Configuration for Kotlin projects using Spring Boot 2.0 and later. */ @@ -126,36 +139,4 @@ ServletInitializerCustomizer javaServletInitializerCustom } - /** - * Configuration for Kotlin projects built with Gradle (Groovy DSL). - * - * @author Andy Wilkinson - */ - @Configuration - @ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY) - static class KotlinGradleProjectConfiguration { - - @Bean - KotlinGradleBuildCustomizer kotlinBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) { - return new GroovyDslKotlinGradleBuildCustomizer(kotlinProjectSettings); - } - - } - - /** - * Configuration for Kotlin projects built with Gradle (Kotlin DSL). - * - * @author Jean-Baptiste Nizet - */ - @Configuration - @ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN) - static class KotlinGradleKtsProjectConfiguration { - - @Bean - KotlinDslKotlinGradleBuildCustomizer kotlinBuildCustomizer(KotlinProjectSettings kotlinProjectSettings) { - return new KotlinDslKotlinGradleBuildCustomizer(kotlinProjectSettings); - } - - } - } diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectSettings.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectSettings.java index 39dddd5564..0a18d49259 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectSettings.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectSettings.java @@ -16,7 +16,6 @@ package io.spring.initializr.generator.spring.code.kotlin; -import java.util.Collections; import java.util.List; /** @@ -44,7 +43,7 @@ public interface KotlinProjectSettings { * @return the compiler arguments */ default List getCompilerArgs() { - return Collections.singletonList("-Xjsr305=strict"); + return List.of("-Xjsr305=strict"); } } diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizerTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizerTests.java deleted file mode 100644 index 5843053bc0..0000000000 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/GroovyDslKotlinGradleBuildCustomizerTests.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2012-2023 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.code.kotlin; - -import java.util.List; - -import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; -import io.spring.initializr.generator.buildsystem.gradle.GradleTask; -import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Attribute; -import org.assertj.core.groups.Tuple; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link GroovyDslKotlinGradleBuildCustomizer}. - * - * @author Andy Wilkinson - * @author Jean-Baptiste Nizet - */ -@SuppressWarnings("removal") -class GroovyDslKotlinGradleBuildCustomizerTests { - - @Test - void kotlinPluginsAreConfigured() { - GradleBuild build = new GradleBuild(); - new GroovyDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build); - assertThat(build.plugins().values()).extracting("id", "version") - .containsExactlyInAnyOrder(Tuple.tuple("org.jetbrains.kotlin.jvm", "1.2.70"), - Tuple.tuple("org.jetbrains.kotlin.plugin.spring", "1.2.70")); - } - - @Test - void kotlinCompilationTasksAreCustomized() { - GradleBuild build = new GradleBuild(); - new GroovyDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", "11")).customize(build); - assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile"); - assertThat(build.tasks().values()).singleElement().satisfies((task) -> { - assertThat(task.getName()).isEqualTo("KotlinCompile"); - assertKotlinOptions(task, "11"); - }); - } - - @Test - void kotlinCompilationTasksWithListOfCompilerArgsAreCustomizer() { - GradleBuild build = new GradleBuild(); - KotlinProjectSettings kotlinProjectSettings = new SimpleKotlinProjectSettings("1.2.70", "11") { - @Override - public List getCompilerArgs() { - return List.of("-Xjsr305=strict", "-Xmx=128M"); - } - }; - new GroovyDslKotlinGradleBuildCustomizer(kotlinProjectSettings).customize(build); - assertThat(build.tasks().values()).singleElement().satisfies((task) -> { - GradleTask kotlinOptions = task.getNested().get("kotlinOptions"); - assertThat(kotlinOptions.getAttributes()) - .contains(Attribute.append("freeCompilerArgs", "['-Xjsr305=strict', '-Xmx=128M']")); - }); - } - - private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) { - assertThat(compileTask.getAttributes()).isEmpty(); - assertThat(compileTask.getInvocations()).isEmpty(); - assertThat(compileTask.getNested()).hasSize(1); - GradleTask kotlinOptions = compileTask.getNested().get("kotlinOptions"); - assertThat(kotlinOptions.getInvocations()).hasSize(0); - assertThat(kotlinOptions.getNested()).hasSize(0); - assertThat(kotlinOptions.getAttributes()).hasSize(2); - assertThat(kotlinOptions.getAttributes()).contains(Attribute.append("freeCompilerArgs", "'-Xjsr305=strict'"), - Attribute.set("jvmTarget", String.format("'%s'", jvmTarget))); - } - -} diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizerTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizerTests.java deleted file mode 100644 index afb0f531ed..0000000000 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinDslKotlinGradleBuildCustomizerTests.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2012-2023 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.code.kotlin; - -import java.util.List; - -import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; -import io.spring.initializr.generator.buildsystem.gradle.GradleTask; -import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Attribute; -import org.assertj.core.groups.Tuple; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * Tests for {@link KotlinDslKotlinGradleBuildCustomizer}. - * - * @author Jean-Baptiste Nizet - */ -@SuppressWarnings("removal") -class KotlinDslKotlinGradleBuildCustomizerTests { - - @Test - void kotlinPluginsAreConfigured() { - GradleBuild build = new GradleBuild(); - new KotlinDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70")).customize(build); - assertThat(build.plugins().values()).extracting("id", "version") - .containsExactlyInAnyOrder(Tuple.tuple("org.jetbrains.kotlin.jvm", "1.2.70"), - Tuple.tuple("org.jetbrains.kotlin.plugin.spring", "1.2.70")); - } - - @Test - void kotlinCompilationTasksAreCustomized() { - GradleBuild build = new GradleBuild(); - new KotlinDslKotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", "11")).customize(build); - assertThat(build.tasks().importedTypes()).contains("org.jetbrains.kotlin.gradle.tasks.KotlinCompile"); - assertThat(build.tasks().values()).singleElement().satisfies((task) -> { - assertThat(task.getName()).isEqualTo("KotlinCompile"); - assertKotlinOptions(task, "11"); - }); - } - - @Test - void kotlinCompilationTasksWithListOfCompilerArgsAreCustomized() { - GradleBuild build = new GradleBuild(); - KotlinProjectSettings kotlinProjectSettings = new SimpleKotlinProjectSettings("1.2.70", "11") { - @Override - public List getCompilerArgs() { - return List.of("-Xjsr305=strict", "-Xmx=128M"); - } - }; - new KotlinDslKotlinGradleBuildCustomizer(kotlinProjectSettings).customize(build); - assertThat(build.tasks().values()).singleElement().satisfies((task) -> { - GradleTask kotlinOptions = task.getNested().get("kotlinOptions"); - assertThat(kotlinOptions.getAttributes()) - .contains(Attribute.append("freeCompilerArgs", "listOf(\"-Xjsr305=strict\", \"-Xmx=128M\")")); - }); - } - - private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) { - assertThat(compileTask.getAttributes()).isEmpty(); - assertThat(compileTask.getInvocations()).isEmpty(); - assertThat(compileTask.getNested()).hasSize(1); - GradleTask kotlinOptions = compileTask.getNested().get("kotlinOptions"); - assertThat(kotlinOptions.getInvocations()).hasSize(0); - assertThat(kotlinOptions.getNested()).hasSize(0); - assertThat(kotlinOptions.getAttributes()).hasSize(2); - assertThat(kotlinOptions.getAttributes()).contains(Attribute.append("freeCompilerArgs", "\"-Xjsr305=strict\""), - Attribute.set("jvmTarget", String.format("\"%s\"", jvmTarget))); - } - -} diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizerTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizerTests.java new file mode 100644 index 0000000000..deb53aed9e --- /dev/null +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinGradleBuildCustomizerTests.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2024 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.code.kotlin; + +import io.spring.initializr.generator.buildsystem.gradle.Attribute; +import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; +import org.assertj.core.groups.Tuple; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link KotlinGradleBuildCustomizer}. + * + * @author Andy Wilkinson + * @author Jean-Baptiste Nizet + * @author Moritz Halbritter + */ +class KotlinGradleBuildCustomizerTests { + + @Test + void kotlinPluginsAreConfigured() { + GradleBuild build = new GradleBuild(); + new KotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70"), '"').customize(build); + assertThat(build.plugins().values()).extracting("id", "version") + .containsExactlyInAnyOrder(Tuple.tuple("org.jetbrains.kotlin.jvm", "1.2.70"), + Tuple.tuple("org.jetbrains.kotlin.plugin.spring", "1.2.70")); + } + + @Test + void shouldCustomizeCompilerOptions() { + GradleBuild build = new GradleBuild(); + new KotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70"), '\'').customize(build); + assertThat(build.extensions().importedTypes()).contains("org.jetbrains.kotlin.gradle.dsl.JvmTarget"); + assertThat(build.extensions().values()).singleElement().satisfies((kotlin) -> { + assertThat(kotlin.getName()).isEqualTo("kotlin"); + assertThat(kotlin.getNested().values()).singleElement().satisfies((compilerOptions) -> { + assertThat(compilerOptions.getName()).isEqualTo("compilerOptions"); + assertThat(compilerOptions.getAttributes()).singleElement() + .isEqualTo(Attribute.set("jvmTarget", "JvmTarget.JVM_1_8")); + assertThat(compilerOptions.getInvocations()).singleElement().satisfies((freeCompilerArgs) -> { + assertThat(freeCompilerArgs.getTarget()).isEqualTo("freeCompilerArgs.addAll"); + assertThat(freeCompilerArgs.getArguments()).containsExactly("'-Xjsr305=strict'"); + }); + }); + }); + } + + @ParameterizedTest + @CsvSource(textBlock = """ + 1.8, JvmTarget.JVM_1_8 + 17, JvmTarget.JVM_17 + 21, JvmTarget.JVM_21 + """) + void shouldSetJvmTarget(String jvmTarget, String constant) { + GradleBuild build = new GradleBuild(); + new KotlinGradleBuildCustomizer(new SimpleKotlinProjectSettings("1.2.70", jvmTarget), '\'').customize(build); + assertThat(build.extensions().values()).singleElement() + .satisfies((kotlin) -> assertThat(kotlin.getNested().values()).singleElement() + .satisfies((compilerOptions) -> assertThat(compilerOptions.getAttributes()).singleElement() + .isEqualTo(Attribute.set("jvmTarget", constant)))); + } + +} diff --git a/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.gen b/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.gen index 90ad417fd6..cfa21ce84d 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id 'org.springframework.boot' version '2.4.1' @@ -25,10 +25,10 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -tasks.withType(KotlinCompile) { - kotlinOptions { - freeCompilerArgs += '-Xjsr305=strict' - jvmTarget = '11' +kotlin { + compilerOptions { + freeCompilerArgs.addAll '-Xjsr305=strict' + jvmTarget = JvmTarget.JVM_11 } } diff --git a/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.kts.gen b/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.kts.gen index b4f888050d..93cab15931 100644 --- a/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.kts.gen +++ b/initializr-generator-spring/src/test/resources/project/gradle/kotlin-java11-build.gradle.kts.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id("org.springframework.boot") version "2.4.1" @@ -25,10 +25,10 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } -tasks.withType { - kotlinOptions { - freeCompilerArgs += "-Xjsr305=strict" - jvmTarget = "11" +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + jvmTarget = JvmTarget.JVM_11 } } diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.gen index b47866105e..5d0481429b 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id 'org.springframework.boot' version '2.4.1' @@ -25,10 +25,10 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -tasks.withType(KotlinCompile) { - kotlinOptions { - freeCompilerArgs += '-Xjsr305=strict' - jvmTarget = '1.8' +kotlin { + compilerOptions { + freeCompilerArgs.addAll '-Xjsr305=strict' + jvmTarget = JvmTarget.JVM_1_8 } } diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.kts.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.kts.gen index 981273c5b9..5404d475e8 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.kts.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/build.gradle.kts.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id("org.springframework.boot") version "2.4.1" @@ -25,10 +25,10 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } -tasks.withType { - kotlinOptions { - freeCompilerArgs += "-Xjsr305=strict" - jvmTarget = "1.8" +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + jvmTarget = JvmTarget.JVM_1_8 } } diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.gen index a0d5026a2f..14c23bf401 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { id 'war' @@ -27,10 +27,10 @@ dependencies { testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } -tasks.withType(KotlinCompile) { - kotlinOptions { - freeCompilerArgs += '-Xjsr305=strict' - jvmTarget = '1.8' +kotlin { + compilerOptions { + freeCompilerArgs.addAll '-Xjsr305=strict' + jvmTarget = JvmTarget.JVM_1_8 } } diff --git a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.kts.gen b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.kts.gen index 5ac372c9b5..87165829e8 100644 --- a/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.kts.gen +++ b/initializr-generator-spring/src/test/resources/project/kotlin/standard/war-build.gradle.kts.gen @@ -1,4 +1,4 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.jetbrains.kotlin.gradle.dsl.JvmTarget plugins { war @@ -27,10 +27,10 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } -tasks.withType { - kotlinOptions { - freeCompilerArgs += "-Xjsr305=strict" - jvmTarget = "1.8" +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + jvmTarget = JvmTarget.JVM_1_8 } }