Make sure that Maven plugin executions can be amended

See gh-867
This commit is contained in:
Stephane Nicoll
2019-03-14 15:05:43 +01:00
parent a5e2994f1d
commit ae87d7381c
2 changed files with 42 additions and 5 deletions

View File

@@ -37,7 +37,7 @@ public class MavenPlugin {
private final String version;
private final List<Execution> executions = new ArrayList<>();
private final Map<String, ExecutionBuilder> executions = new LinkedHashMap<>();
private final List<Dependency> dependencies = new ArrayList<>();
@@ -73,13 +73,13 @@ public class MavenPlugin {
}
public void execution(String id, Consumer<ExecutionBuilder> customizer) {
ExecutionBuilder builder = new ExecutionBuilder(id);
customizer.accept(builder);
this.executions.add(builder.build());
customizer.accept(
this.executions.computeIfAbsent(id, (key) -> new ExecutionBuilder(id)));
}
public List<Execution> getExecutions() {
return this.executions;
return this.executions.values().stream().map(ExecutionBuilder::build)
.collect(Collectors.toList());
}
public void dependency(String groupId, String artifactId, String version) {

View File

@@ -109,4 +109,41 @@ class MavenPluginTests {
.withMessageContaining("test").withMessageContaining("value");
}
@Test
void executionPhasesCanBeOverridden() {
MavenPlugin plugin = new MavenPlugin("com.example", "test-plugin");
plugin.execution("test", (test) -> test.phase("compile"));
plugin.execution("test", (test) -> test.phase("process-resources"));
assertThat(plugin.getExecutions()).hasSize(1);
assertThat(plugin.getExecutions().get(0).getPhase())
.isEqualTo("process-resources");
}
@Test
void executionGoalsCanBeAmended() {
MavenPlugin plugin = new MavenPlugin("com.example", "test-plugin");
plugin.execution("test", (test) -> test.goal("first"));
plugin.execution("test", (test) -> test.goal("second"));
assertThat(plugin.getExecutions()).hasSize(1);
assertThat(plugin.getExecutions().get(0).getGoals()).containsExactly("first",
"second");
}
@Test
void executionConfigurationCanBeOverridden() {
MavenPlugin plugin = new MavenPlugin("com.example", "test-plugin");
plugin.execution("test",
(test) -> test.configuration((testConfiguration) -> testConfiguration
.parameter("enabled", "true").parameter("another", "test")));
plugin.execution("test", (test) -> test.configuration(
(testConfiguration) -> testConfiguration.parameter("enabled", "false")));
assertThat(plugin.getExecutions()).hasSize(1);
List<Setting> settings = plugin.getExecutions().get(0).getConfiguration()
.getSettings();
assertThat(settings.stream().map(Setting::getName)).containsExactly("enabled",
"another");
assertThat(settings.stream().map(Setting::getValue)).containsExactly("false",
"test");
}
}