diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java index ffe936ed28..622524984e 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuild.java @@ -27,6 +27,7 @@ * @author Andy Wilkinson * @author Stephane Nicoll * @author Niklas Herder + * @author Maurice Zeijen */ public class MavenBuild extends Build { @@ -36,6 +37,8 @@ public class MavenBuild extends Build { private final MavenResourceContainer testResources = new MavenResourceContainer(); + private final MavenPluginContainer pluginManagementPlugins = new MavenPluginContainer(); + private final MavenPluginContainer plugins = new MavenPluginContainer(); private final MavenExtensionContainer extensions = new MavenExtensionContainer(); @@ -99,6 +102,15 @@ public MavenResourceContainer testResources() { return this.testResources; } + /** + * Return the {@linkplain MavenPluginContainer plugin container} to use to configure + * plugin management plugins. + * @return the {@link MavenPluginContainer} + */ + public MavenPluginContainer pluginManagementPlugins() { + return this.pluginManagementPlugins; + } + /** * Return the {@linkplain MavenPluginContainer plugin container} to use to configure * plugins. 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 6bc0b904a7..057e3c9ae9 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 @@ -359,7 +359,8 @@ private void writeBuild(IndentingWriter writer, MavenBuild build) { MavenBuildSettings settings = build.getSettings(); if (settings.getDefaultGoal() == null && settings.getFinalName() == null && settings.getSourceDirectory() == null && settings.getTestSourceDirectory() == null - && build.resources().isEmpty() && build.testResources().isEmpty() && build.plugins().isEmpty() + && build.resources().isEmpty() && build.testResources().isEmpty() + && build.pluginManagementPlugins().isEmpty() && build.plugins().isEmpty() && build.extensions().isEmpty()) { return; } @@ -370,6 +371,7 @@ private void writeBuild(IndentingWriter writer, MavenBuild build) { writeSingleElement(writer, "sourceDirectory", settings.getSourceDirectory()); writeSingleElement(writer, "testSourceDirectory", settings.getTestSourceDirectory()); writeResources(writer, build.resources(), build.testResources()); + writePluginManagement(writer, build.pluginManagementPlugins()); writeCollectionElement(writer, "plugins", build.plugins().values(), this::writePlugin); writeCollectionElement(writer, "extensions", build.extensions().values(), this::writeExtension); }); @@ -409,6 +411,13 @@ private void writeResourceExclude(IndentingWriter writer, String exclude) { writeSingleElement(writer, "exclude", exclude); } + private void writePluginManagement(IndentingWriter writer, MavenPluginContainer pluginManagementContainer) { + if (!pluginManagementContainer.isEmpty()) { + writeElement(writer, "pluginManagement", () -> writeCollectionElement(writer, "plugins", + pluginManagementContainer.values(), this::writePlugin)); + } + } + private void writePlugin(IndentingWriter writer, MavenPlugin plugin) { writeElement(writer, "plugin", () -> { writeSingleElement(writer, "groupId", plugin.getGroupId()); @@ -600,13 +609,15 @@ private void writeProfileActivation(IndentingWriter writer, MavenProfileActivati private void writeProfileBuild(IndentingWriter writer, MavenProfile profile) { MavenProfile.Settings settings = profile.getSettings(); if (settings.getDefaultGoal() == null && settings.getFinalName() == null && profile.resources().isEmpty() - && profile.testResources().isEmpty() && profile.plugins().isEmpty()) { + && profile.testResources().isEmpty() && profile.pluginManagementPlugins().isEmpty() + && profile.plugins().isEmpty()) { return; } writeElement(writer, "build", () -> { writeSingleElement(writer, "defaultGoal", settings.getDefaultGoal()); writeSingleElement(writer, "finalName", settings.getFinalName()); writeResources(writer, profile.resources(), profile.testResources()); + writePluginManagement(writer, profile.pluginManagementPlugins()); writeCollectionElement(writer, "plugins", profile.plugins().values(), this::writePlugin); }); } diff --git a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java index d3c54f416a..f0e49c4182 100644 --- a/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java +++ b/initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenProfile.java @@ -27,6 +27,7 @@ * * @author Daniel Andres Pelaez Lopez * @author Stephane Nicoll + * @author Maurice Zeijen */ public class MavenProfile { @@ -44,6 +45,8 @@ public class MavenProfile { private final MavenResourceContainer testResources = new MavenResourceContainer(); + private final MavenPluginContainer pluginManagementPlugins = new MavenPluginContainer(); + private final MavenPluginContainer plugins = new MavenPluginContainer(); private final BomContainer boms; @@ -184,6 +187,15 @@ public MavenResourceContainer testResources() { return this.testResources; } + /** + * Return the {@linkplain MavenPluginContainer plugin container} to use to configure + * plugin management plugins. + * @return the {@link MavenPluginContainer} + */ + public MavenPluginContainer pluginManagementPlugins() { + return this.pluginManagementPlugins; + } + /** * Return the {@linkplain MavenPluginContainer plugin container} to use to configure * plugins. 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 8a7f0102f3..cd00174961 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 @@ -58,6 +58,17 @@ void mavenTestResourcesCanBeConfigured() { }); } + @Test + void mavenPluginManagementCanBeConfigured() { + MavenBuild build = new MavenBuild(); + build.pluginManagementPlugins().add("com.example", "test-plugin", (plugin) -> plugin.version("1.2.3")); + assertThat(build.pluginManagementPlugins().values()).singleElement().satisfies((testPlugin) -> { + assertThat(testPlugin.getGroupId()).isEqualTo("com.example"); + assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin"); + assertThat(testPlugin.getVersion()).isEqualTo("1.2.3"); + }); + } + @Test void mavenPluginCanBeConfigured() { MavenBuild build = new MavenBuild(); @@ -201,4 +212,20 @@ void mavenProfileCanBeRemoved() { assertThat(build.profiles().values()).isEmpty(); } + @Test + void mavenPluginManagementInProfileCanBeConfigured() { + MavenBuild build = new MavenBuild(); + build.profiles() + .id("test") + .pluginManagementPlugins() + .add("com.example", "test-plugin", (plugin) -> plugin.version("1.2.3")); + assertThat(build.profiles().values()).singleElement() + .satisfies((profile) -> assertThat(profile.pluginManagementPlugins().values()).singleElement() + .satisfies((testPlugin) -> { + assertThat(testPlugin.getGroupId()).isEqualTo("com.example"); + assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin"); + assertThat(testPlugin.getVersion()).isEqualTo("1.2.3"); + })); + } + } 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 a1319a02a2..88e458a2f2 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 @@ -605,6 +605,20 @@ void pomWithTestResources() { }); } + @Test + void pomWithPluginManagement() { + MavenBuild build = new MavenBuild(); + build.settings().coordinates("com.example.demo", "demo"); + build.pluginManagementPlugins() + .add("org.springframework.boot", "spring-boot-maven-plugin", (plugin) -> plugin.version("1.2.3")); + generatePom(build, (pom) -> { + NodeAssert plugin = pom.nodeAtPath("/project/build/pluginManagement/plugins/plugin"); + assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot"); + assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin"); + assertThat(plugin).textAtPath("version").isEqualTo("1.2.3"); + }); + } + @Test void pomWithPlugin() { MavenBuild build = new MavenBuild(); @@ -1185,6 +1199,23 @@ void pomWithProfileTestResources() { }); } + @Test + void pomWithProfilePluginManagement() { + MavenBuild build = new MavenBuild(); + build.profiles() + .id("profile1") + .pluginManagementPlugins() + .add("org.springframework.boot", "spring-boot-maven-plugin", (plugin) -> plugin.version("1.2.3")); + generatePom(build, (pom) -> { + NodeAssert profile = pom.nodeAtPath("/project/profiles/profile"); + assertThat(profile).textAtPath("id").isEqualTo("profile1"); + NodeAssert plugin = profile.nodeAtPath("build/pluginManagement/plugins/plugin"); + assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot"); + assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin"); + assertThat(plugin).textAtPath("version").isEqualTo("1.2.3"); + }); + } + @Test void pomWithProfilePlugin() { MavenBuild build = new MavenBuild();