Skip to content

Commit

Permalink
Add plugin management to Maven build and profiles
Browse files Browse the repository at this point in the history
This allows writing plugin management plugin elements in the POM
within the build and profiles elements.
  • Loading branch information
mzeijen committed Jun 17, 2024
1 parent 0c7882e commit 8bffb09
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Niklas Herder
* @author Maurice Zeijen
*/
public class MavenBuild extends Build {

Expand All @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* @author Jafer Khan Shamshad
* @author Joachim Pasquali
* @author Niklas Herder
* @author Maurice Zeijen
*/
public class MavenBuildWriter {

Expand Down Expand Up @@ -358,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;
}
Expand All @@ -369,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);
});
Expand Down Expand Up @@ -408,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());
Expand Down Expand Up @@ -596,13 +606,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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*
* @author Daniel Andres Pelaez Lopez
* @author Stephane Nicoll
* @author Maurice Zeijen
*/
public class MavenProfile {

Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*
* @author Stephane Nicoll
* @author Olga Maciaszek-Sharma
* @author Maurice Zeijen
*/
class MavenBuildTests {

Expand Down Expand Up @@ -57,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();
Expand Down Expand Up @@ -184,4 +196,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");
}));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Olga Maciaszek-Sharma
* @author Jafer Khan Shamshad
* @author Joachim Pasquali
* @author Maurice Zeijen
*/
class MavenBuildWriterTests {

Expand Down Expand Up @@ -604,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();
Expand Down Expand Up @@ -1170,6 +1185,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();
Expand Down

0 comments on commit 8bffb09

Please sign in to comment.