Skip to content

Commit f42994b

Browse files
committed
Merge pull request #1543 from mzeijen
* pr/1543: Polish "Add inherited to MavenPlugin" Add inherited to MavenPlugin Closes gh-1543
2 parents 9ff395d + 5ba0703 commit f42994b

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Jafer Khan Shamshad
6262
* @author Joachim Pasquali
6363
* @author Niklas Herder
64+
* @author Maurice Zeijen
6465
*/
6566
public class MavenBuildWriter {
6667

@@ -413,6 +414,9 @@ private void writePlugin(IndentingWriter writer, MavenPlugin plugin) {
413414
writeSingleElement(writer, "groupId", plugin.getGroupId());
414415
writeSingleElement(writer, "artifactId", plugin.getArtifactId());
415416
writeSingleElement(writer, "version", plugin.getVersion());
417+
if (!plugin.isInherited()) {
418+
writeSingleElement(writer, "inherited", "false");
419+
}
416420
if (plugin.isExtensions()) {
417421
writeSingleElement(writer, "extensions", "true");
418422
}

initializr-generator/src/main/java/io/spring/initializr/generator/buildsystem/maven/MavenPlugin.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Andy Wilkinson
3030
* @author Olga Maciaszek-Sharma
31+
* @author Maurice Zeijen
3132
*/
3233
public class MavenPlugin {
3334

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

4041
private final boolean extensions;
4142

43+
private final boolean inherited;
44+
4245
private final List<Execution> executions;
4346

4447
private final List<Dependency> dependencies;
@@ -50,6 +53,7 @@ protected MavenPlugin(Builder builder) {
5053
this.artifactId = builder.artifactId;
5154
this.version = builder.version;
5255
this.extensions = builder.extensions;
56+
this.inherited = builder.inherited;
5357
this.executions = builder.executions.values().stream().map(ExecutionBuilder::build).toList();
5458
this.dependencies = List.copyOf(builder.dependencies);
5559
this.configuration = (builder.configurationBuilder == null) ? null : builder.configurationBuilder.build();
@@ -88,6 +92,14 @@ public boolean isExtensions() {
8892
return this.extensions;
8993
}
9094

95+
/**
96+
* Return whether to inherit plugin configuration to child POMs.
97+
* @return whether to inherit plugin configuration
98+
*/
99+
public boolean isInherited() {
100+
return this.inherited;
101+
}
102+
91103
/**
92104
* Return the {@linkplain Execution executions} of the plugin.
93105
* @return the executions
@@ -125,6 +137,8 @@ public static class Builder {
125137

126138
private boolean extensions;
127139

140+
private boolean inherited = true;
141+
128142
private final Map<String, ExecutionBuilder> executions = new LinkedHashMap<>();
129143

130144
private final List<Dependency> dependencies = new ArrayList<>();
@@ -157,6 +171,16 @@ public Builder extensions(boolean extensions) {
157171
return this;
158172
}
159173

174+
/**
175+
* Set whether to inherit plugin configuration to child POMs.
176+
* @param inherited whether to inherit plugin configuration
177+
* @return this for method chaining
178+
*/
179+
public Builder inherited(boolean inherited) {
180+
this.inherited = inherited;
181+
return this;
182+
}
183+
160184
/**
161185
* Customize the {@code configuration} of the plugin using the specified consumer.
162186
* @param configuration a consumer of the current configuration

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*
2626
* @author Stephane Nicoll
2727
* @author Olga Maciaszek-Sharma
28+
* @author Maurice Zeijen
2829
*/
2930
class MavenBuildTests {
3031

@@ -126,6 +127,22 @@ void mavenPluginExecutionCanBeAmended() {
126127
});
127128
}
128129

130+
@Test
131+
void mavenPluginInheritedByDefault() {
132+
MavenBuild build = new MavenBuild();
133+
build.plugins().add("com.example", "test-plugin");
134+
assertThat(build.plugins().values()).singleElement()
135+
.satisfies((testPlugin) -> assertThat(testPlugin.isInherited()).isTrue());
136+
}
137+
138+
@Test
139+
void mavenPluginCanBeSetToNotBeInherited() {
140+
MavenBuild build = new MavenBuild();
141+
build.plugins().add("com.example", "test-plugin", (plugin) -> plugin.inherited(false));
142+
assertThat(build.plugins().values()).singleElement()
143+
.satisfies((testPlugin) -> assertThat(testPlugin.isInherited()).isFalse());
144+
}
145+
129146
@Test
130147
void mavenPluginExtensionsNotLoadedByDefault() {
131148
MavenBuild build = new MavenBuild();

initializr-generator/src/test/java/io/spring/initializr/generator/buildsystem/maven/MavenBuildWriterTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Olga Maciaszek-Sharma
4141
* @author Jafer Khan Shamshad
4242
* @author Joachim Pasquali
43+
* @author Maurice Zeijen
4344
*/
4445
class MavenBuildWriterTests {
4546

@@ -614,6 +615,7 @@ void pomWithPlugin() {
614615
assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot");
615616
assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin");
616617
assertThat(plugin).textAtPath("version").isNullOrEmpty();
618+
assertThat(plugin).textAtPath("inherited").isNullOrEmpty();
617619
assertThat(plugin).textAtPath("extensions").isNullOrEmpty();
618620
});
619621
}
@@ -687,6 +689,19 @@ void pomWithPluginWithDependency() {
687689
});
688690
}
689691

692+
@Test
693+
void pomWithNonInheritedPlugin() {
694+
MavenBuild build = new MavenBuild();
695+
build.settings().coordinates("com.example.demo", "demo");
696+
build.plugins().add("com.example.demo", "demo-plugin", (plugin) -> plugin.inherited(false));
697+
generatePom(build, (pom) -> {
698+
NodeAssert plugin = pom.nodeAtPath("/project/build/plugins/plugin");
699+
assertThat(plugin).textAtPath("groupId").isEqualTo("com.example.demo");
700+
assertThat(plugin).textAtPath("artifactId").isEqualTo("demo-plugin");
701+
assertThat(plugin).textAtPath("inherited").isEqualTo("false");
702+
});
703+
}
704+
690705
@Test
691706
void pomWithPluginWithExtensions() {
692707
MavenBuild build = new MavenBuild();
@@ -1181,6 +1196,7 @@ void pomWithProfilePlugin() {
11811196
assertThat(plugin).textAtPath("groupId").isEqualTo("org.springframework.boot");
11821197
assertThat(plugin).textAtPath("artifactId").isEqualTo("spring-boot-maven-plugin");
11831198
assertThat(plugin).textAtPath("version").isNullOrEmpty();
1199+
assertThat(plugin).textAtPath("inherited").isNullOrEmpty();
11841200
assertThat(plugin).textAtPath("extensions").isNullOrEmpty();
11851201
});
11861202
}

0 commit comments

Comments
 (0)