Make sure that Maven plugins can be amended

Closes gh-867
This commit is contained in:
Stephane Nicoll
2019-03-14 15:21:14 +01:00
parent ae87d7381c
commit 0805345e85
3 changed files with 116 additions and 9 deletions

View File

@@ -18,6 +18,7 @@ package io.spring.initializr.generator.buildsystem.maven;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -44,7 +45,7 @@ public class MavenBuild extends Build {
private final Map<String, String> properties = new TreeMap<>();
private final List<MavenPlugin> plugins = new ArrayList<>();
private final Map<String, MavenPlugin> plugins = new LinkedHashMap<>();
private String packaging;
@@ -106,19 +107,24 @@ public class MavenBuild extends Build {
}
public MavenPlugin plugin(String groupId, String artifactId) {
MavenPlugin plugin = new MavenPlugin(groupId, artifactId);
this.plugins.add(plugin);
return plugin;
return this.plugins.computeIfAbsent(pluginKey(groupId, artifactId),
(id) -> new MavenPlugin(groupId, artifactId));
}
public MavenPlugin plugin(String groupId, String artifactId, String version) {
MavenPlugin plugin = new MavenPlugin(groupId, artifactId, version);
this.plugins.add(plugin);
return plugin;
MavenPlugin mavenPlugin = this.plugins.computeIfAbsent(
pluginKey(groupId, artifactId),
(id) -> new MavenPlugin(groupId, artifactId));
mavenPlugin.setVersion(version);
return mavenPlugin;
}
private String pluginKey(String groupId, String artifactId) {
return String.format("%s:%s", groupId, artifactId);
}
public List<MavenPlugin> getPlugins() {
return Collections.unmodifiableList(this.plugins);
return Collections.unmodifiableList(new ArrayList<>(this.plugins.values()));
}
public void setPackaging(String packaging) {

View File

@@ -35,7 +35,7 @@ public class MavenPlugin {
private final String artifactId;
private final String version;
private String version;
private final Map<String, ExecutionBuilder> executions = new LinkedHashMap<>();
@@ -65,6 +65,10 @@ public class MavenPlugin {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
public void configuration(Consumer<ConfigurationCustomization> consumer) {
if (this.configurationCustomization == null) {
this.configurationCustomization = new ConfigurationCustomization();

View File

@@ -0,0 +1,97 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.buildsystem.maven;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link MavenBuild}.
*
* @author Stephane Nicoll
*/
class MavenBuildTests {
@Test
void mavenPluginCanBeConfigured() {
MavenBuild build = new MavenBuild();
build.plugin("com.example", "test-plugin").execution("first",
(first) -> first.goal("run-this"));
assertThat(build.getPlugins()).hasSize(1);
MavenPlugin testPlugin = build.getPlugins().get(0);
assertThat(testPlugin.getGroupId()).isEqualTo("com.example");
assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin");
assertThat(testPlugin.getVersion()).isNull();
assertThat(testPlugin.getExecutions()).hasSize(1);
assertThat(testPlugin.getExecutions().get(0).getId()).isEqualTo("first");
assertThat(testPlugin.getExecutions().get(0).getGoals())
.containsExactly("run-this");
}
@Test
void mavenPluginVersionCanBeAmended() {
MavenBuild build = new MavenBuild();
build.plugin("com.example", "test-plugin");
build.plugin("com.example", "test-plugin", "1.0.0");
assertThat(build.getPlugins()).hasSize(1);
MavenPlugin testPlugin = build.getPlugins().get(0);
assertThat(testPlugin.getGroupId()).isEqualTo("com.example");
assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin");
assertThat(testPlugin.getVersion()).isEqualTo("1.0.0");
}
@Test
void mavenPluginVersionCanBeAmendedWithCustomizer() {
MavenBuild build = new MavenBuild();
build.plugin("com.example", "test-plugin", "1.0.0");
build.plugin("com.example", "test-plugin").setVersion(null);
assertThat(build.getPlugins()).hasSize(1);
MavenPlugin testPlugin = build.getPlugins().get(0);
assertThat(testPlugin.getGroupId()).isEqualTo("com.example");
assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin");
assertThat(testPlugin.getVersion()).isNull();
}
@Test
void mavenPluginVersionIsNotLostOnAmend() {
MavenBuild build = new MavenBuild();
build.plugin("com.example", "test-plugin", "1.0.0");
build.plugin("com.example", "test-plugin");
assertThat(build.getPlugins()).hasSize(1);
MavenPlugin testPlugin = build.getPlugins().get(0);
assertThat(testPlugin.getGroupId()).isEqualTo("com.example");
assertThat(testPlugin.getArtifactId()).isEqualTo("test-plugin");
assertThat(testPlugin.getVersion()).isEqualTo("1.0.0");
}
@Test
void mavenPluginExecutionCanBeAmended() {
MavenBuild build = new MavenBuild();
build.plugin("com.example", "test-plugin").execution("first",
(first) -> first.goal("run-this"));
build.plugin("com.example", "test-plugin").execution("first",
(first) -> first.goal("run-that"));
assertThat(build.getPlugins()).hasSize(1);
MavenPlugin testPlugin = build.getPlugins().get(0);
assertThat(testPlugin.getExecutions()).hasSize(1);
assertThat(testPlugin.getExecutions().get(0).getId()).isEqualTo("first");
assertThat(testPlugin.getExecutions().get(0).getGoals())
.containsExactly("run-this", "run-that");
}
}