From bac7d3192f59c2e7b4332dfa9a7b1eba21daac51 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Fri, 20 Dec 2024 14:51:13 +0100 Subject: [PATCH] Apply 'all-open' plugin when generation Kotlin Maven projects with JPA Closes gh-1607 --- .../kotlin/KotlinJpaMavenBuildCustomizer.java | 6 ++-- .../KotlinJpaMavenBuildCustomizerTests.java | 33 ++++++++++++------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizer.java b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizer.java index 6d288aa92c..4505359cd8 100644 --- a/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizer.java +++ b/initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizer.java @@ -44,8 +44,10 @@ public void customize(MavenBuild build) { if (this.buildMetadataResolver.hasFacet(build, "jpa")) { build.plugins().add("org.jetbrains.kotlin", "kotlin-maven-plugin", (kotlinPlugin) -> { kotlinPlugin.configuration((configuration) -> { - configuration.configure("compilerPlugins", - (compilerPlugins) -> compilerPlugins.add("plugin", "jpa")); + configuration.configure("compilerPlugins", (compilerPlugins) -> { + compilerPlugins.add("plugin", "jpa"); + compilerPlugins.add("plugin", "all-open"); + }); configuration.configure("pluginOptions", (option) -> { option.add("option", "all-open:annotation=jakarta.persistence.Entity"); option.add("option", "all-open:annotation=jakarta.persistence.MappedSuperclass"); diff --git a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizerTests.java b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizerTests.java index c62918d890..30c11a84ca 100644 --- a/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizerTests.java +++ b/initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJpaMavenBuildCustomizerTests.java @@ -18,7 +18,6 @@ import java.util.Collections; import java.util.List; -import java.util.Objects; import io.spring.initializr.generator.buildsystem.maven.MavenBuild; import io.spring.initializr.generator.buildsystem.maven.MavenPlugin; @@ -29,6 +28,7 @@ import io.spring.initializr.metadata.Dependency; import io.spring.initializr.metadata.InitializrMetadata; import io.spring.initializr.metadata.support.MetadataBuildItemResolver; +import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; @@ -76,19 +76,17 @@ void customizeWhenJpaFacetPresentShouldCustomizeAllOpen() { Dependency dependency = Dependency.withId("foo"); dependency.setFacets(Collections.singletonList("jpa")); MavenBuild build = getCustomizedBuild(dependency); - assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> { MavenPlugin.Configuration configuration = plugin.getConfiguration(); - - assertThat(configuration.getSettings()).filteredOn((setting) -> setting.getName().equals("pluginOptions")) - .isNotEmpty() - .first() - .satisfies((pluginOptions) -> assertThat(((List) pluginOptions.getValue())) - .filteredOn((option) -> Objects.equals(option.getName(), "option")) - .map(MavenPlugin.Setting::getValue) - .containsExactlyInAnyOrder("all-open:annotation=jakarta.persistence.Entity", - "all-open:annotation=jakarta.persistence.MappedSuperclass", - "all-open:annotation=jakarta.persistence.Embeddable")); + List compilerPlugins = getSettingValue(plugin.getConfiguration(), "compilerPlugins"); + assertThat(compilerPlugins).isNotNull() + .map(MavenPlugin.Setting::getValue) + .containsExactlyInAnyOrder("jpa", "all-open"); + List pluginOptions = getSettingValue(plugin.getConfiguration(), "pluginOptions"); + assertThat(pluginOptions).map(MavenPlugin.Setting::getValue) + .containsExactlyInAnyOrder("all-open:annotation=jakarta.persistence.Entity", + "all-open:annotation=jakarta.persistence.MappedSuperclass", + "all-open:annotation=jakarta.persistence.Embeddable"); }); } @@ -112,4 +110,15 @@ private MavenBuild getCustomizedBuild(Dependency dependency) { return build; } + @SuppressWarnings("unchecked") + private T getSettingValue(MavenPlugin.Configuration configuration, String name) { + for (MavenPlugin.Setting setting : configuration.getSettings()) { + if (setting.getName().equals(name)) { + return (T) setting.getValue(); + } + } + Assertions.fail("No setting with name '%s' found", name); + return null; + } + }