From f69c3bc95388bc1ace94bce3ad54efeace614c54 Mon Sep 17 00:00:00 2001 From: Maurice Zeijen Date: Mon, 17 Jun 2024 08:16:24 +0200 Subject: [PATCH 1/2] Add inherited to MavenPlugin This allows writing the inherited element of a Maven plugin to the POM with the false value. When true, the inherited element is not written as it is the default value. See gh-1543 --- .../buildsystem/maven/MavenBuildWriter.java | 4 ++++ .../buildsystem/maven/MavenPlugin.java | 24 +++++++++++++++++++ .../buildsystem/maven/MavenBuildTests.java | 17 +++++++++++++ .../maven/MavenBuildWriterTests.java | 16 +++++++++++++ 4 files changed, 61 insertions(+) diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java index 5e45164a22..6bc0b904a7 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java @@ -61,6 +61,7 @@ * @author Jafer Khan Shamshad * @author Joachim Pasquali * @author Niklas Herder + * @author Maurice Zeijen */ public class MavenBuildWriter { @@ -413,6 +414,9 @@ private void writePlugin(IndentingWriter writer, MavenPlugin plugin) { writeSingleElement(writer, "groupId", plugin.getGroupId()); writeSingleElement(writer, "artifactId", plugin.getArtifactId()); writeSingleElement(writer, "version", plugin.getVersion()); + if (!plugin.isInherited()) { + writeSingleElement(writer, "inherited", "false"); + } if (plugin.isExtensions()) { writeSingleElement(writer, "extensions", "true"); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java index 0a92c35b15..545e300981 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java @@ -28,6 +28,7 @@ * * @author Andy Wilkinson * @author Olga Maciaszek-Sharma + * @author Maurice Zeijen */ public class MavenPlugin { @@ -39,6 +40,8 @@ public class MavenPlugin { private final boolean extensions; + private final boolean inherited; + private final List executions; private final List dependencies; @@ -50,6 +53,7 @@ protected MavenPlugin(Builder builder) { this.artifactId = builder.artifactId; this.version = builder.version; this.extensions = builder.extensions; + this.inherited = builder.inherited; this.executions = builder.executions.values().stream().map(ExecutionBuilder::build).toList(); this.dependencies = List.copyOf(builder.dependencies); this.configuration = (builder.configurationBuilder == null) ? null : builder.configurationBuilder.build(); @@ -88,6 +92,14 @@ public boolean isExtensions() { return this.extensions; } + /** + * Return whether to propagate plugin configuration to child POMs. + * @return {@code true} whether to propagate plugin configuration + */ + public boolean isInherited() { + return this.inherited; + } + /** * Return the {@linkplain Execution executions} of the plugin. * @return the executions @@ -125,6 +137,8 @@ public static class Builder { private boolean extensions; + private boolean inherited = true; + private final Map executions = new LinkedHashMap<>(); private final List dependencies = new ArrayList<>(); @@ -157,6 +171,16 @@ public Builder extensions(boolean extensions) { return this; } + /** + * Set whether to propagate plugin configuration to child POMs. + * @param inherited whether to propagate plugin configuration + * @return this for method chaining + */ + public Builder inherited(boolean inherited) { + this.inherited = inherited; + return this; + } + /** * Customize the {@code configuration} of the plugin using the specified consumer. * @param configuration a consumer of the current configuration diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java index c42c3ae314..8a7f0102f3 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java @@ -25,6 +25,7 @@ * * @author Stephane Nicoll * @author Olga Maciaszek-Sharma + * @author Maurice Zeijen */ class MavenBuildTests { @@ -126,6 +127,22 @@ void mavenPluginExecutionCanBeAmended() { }); } + @Test + void mavenPluginInheritedByDefault() { + MavenBuild build = new MavenBuild(); + build.plugins().add("com.example", "test-plugin"); + assertThat(build.plugins().values()).singleElement() + .satisfies((testPlugin) -> assertThat(testPlugin.isInherited()).isTrue()); + } + + @Test + void mavenPluginCanBeSetToNotBeInherited() { + MavenBuild build = new MavenBuild(); + build.plugins().add("com.example", "test-plugin", (plugin) -> plugin.inherited(false)); + assertThat(build.plugins().values()).singleElement() + .satisfies((testPlugin) -> assertThat(testPlugin.isInherited()).isFalse()); + } + @Test void mavenPluginExtensionsNotLoadedByDefault() { MavenBuild build = new MavenBuild(); diff --git a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java index ac03067996..a1319a02a2 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java +++ b/initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java @@ -40,6 +40,7 @@ * @author Olga Maciaszek-Sharma * @author Jafer Khan Shamshad * @author Joachim Pasquali + * @author Maurice Zeijen */ class MavenBuildWriterTests { @@ -614,6 +615,7 @@ void pomWithPlugin() { assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot"); assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin"); assertThat(plugin).textAtPath("version").isNullOrEmpty(); + assertThat(plugin).textAtPath("inherited").isNullOrEmpty(); assertThat(plugin).textAtPath("extensions").isNullOrEmpty(); }); } @@ -687,6 +689,19 @@ void pomWithPluginWithDependency() { }); } + @Test + void pomWithNonInheritedPlugin() { + MavenBuild build = new MavenBuild(); + build.settings().coordinates("com.example.demo", "demo"); + build.plugins().add("com.example.demo", "demo-plugin", (plugin) -> plugin.inherited(false)); + generatePom(build, (pom) -> { + NodeAssert plugin = pom.nodeAtPath("/project/build/plugins/plugin"); + assertThat(plugin).textAtPath("groupId").isEqualTo("com.example.demo"); + assertThat(plugin).textAtPath("artifactId").isEqualTo("demo-plugin"); + assertThat(plugin).textAtPath("inherited").isEqualTo("false"); + }); + } + @Test void pomWithPluginWithExtensions() { MavenBuild build = new MavenBuild(); @@ -1181,6 +1196,7 @@ void pomWithProfilePlugin() { assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot"); assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin"); assertThat(plugin).textAtPath("version").isNullOrEmpty(); + assertThat(plugin).textAtPath("inherited").isNullOrEmpty(); assertThat(plugin).textAtPath("extensions").isNullOrEmpty(); }); } From 5ba070395552ca3044bef14953968b4f9e415584 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Fri, 21 Jun 2024 09:21:43 +0200 Subject: [PATCH 2/2] Polish "Add inherited to MavenPlugin" See gh-1543 --- .../generator/buildsystem/maven/MavenPlugin.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java index 545e300981..95418dbb46 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java @@ -93,8 +93,8 @@ public boolean isExtensions() { } /** - * Return whether to propagate plugin configuration to child POMs. - * @return {@code true} whether to propagate plugin configuration + * Return whether to inherit plugin configuration to child POMs. + * @return whether to inherit plugin configuration */ public boolean isInherited() { return this.inherited; @@ -172,8 +172,8 @@ public Builder extensions(boolean extensions) { } /** - * Set whether to propagate plugin configuration to child POMs. - * @param inherited whether to propagate plugin configuration + * Set whether to inherit plugin configuration to child POMs. + * @param inherited whether to inherit plugin configuration * @return this for method chaining */ public Builder inherited(boolean inherited) {