Skip to content

Commit

Permalink
Merge pull request #1543 from mzeijen
Browse files Browse the repository at this point in the history
* pr/1543:
  Polish "Add inherited to MavenPlugin"
  Add inherited to MavenPlugin

Closes gh-1543
  • Loading branch information
mhalbritter committed Jun 21, 2024
2 parents 9ff395d + 5ba0703 commit f42994b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
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 inherit plugin configuration to child POMs.
* @return whether to inherit 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 inherit plugin configuration to child POMs.
* @param inherited whether to inherit 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

0 comments on commit f42994b

Please sign in to comment.