Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the inherited option on Maven plugins #1543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Andy Wilkinson
* @author Olga Maciaszek-Sharma
* @author Maurice Zeijen
*/
public class MavenPlugin {

Expand All @@ -39,6 +40,8 @@ public class MavenPlugin {

private final boolean extensions;

private final boolean inherited;

private final List<Execution> executions;

private final List<Dependency> dependencies;
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -125,6 +137,8 @@ public static class Builder {

private boolean extensions;

private boolean inherited = true;

private final Map<String, ExecutionBuilder> executions = new LinkedHashMap<>();

private final List<Dependency> dependencies = new ArrayList<>();
Expand Down Expand Up @@ -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
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 @@ -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();
Expand Down
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 @@ -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();
});
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
}
Expand Down