Skip to content

Commit

Permalink
Fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
andirady committed Aug 12, 2024
1 parent 0450d39 commit 3411352
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/github/andirady/pomcli/AddPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ public Plugin addPlugin(Model model, Plugin plugin) {
}
};

PluginContainer pluginContainer = "pom".equals(model.getPackaging()) ? build.getPluginManagement() : build;
PluginContainer pluginContainer = switch (model.getPackaging()) {
case "pom" -> {
if (build.getPluginManagement() == null) {
build.setPluginManagement(new PluginManagement());
}

yield build.getPluginManagement();
}
default -> build;
};
var alreadyPlugged = pluginContainer.getPlugins().stream().filter(p -> p.getKey().equals(plugin.getKey()))
.findFirst().isPresent();
if (alreadyPlugged) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/github/andirady/pomcli/PlugCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import static org.junit.jupiter.api.Assertions.assertSame;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -82,4 +84,24 @@ void supportsManagedPlugin(String pomContent, String input) throws IOException {
var expr = "/project/build/plugins/plugin[groupId='org.springframework.boot' and artifactId='spring-boot-maven-plugin']";
assertXpath(pomPath, expr, 1);
}

@Test
void addToPluginManagementWhenPackagedAsPom() throws IOException {
var pomPath = writeString(tempDir.resolve("pom.xml"), """
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>g</groupId>
<artifactId>a</artifactId>
<version>1</version>
<packaging>pom</packaging>
</project>
""");
var ec = underTest.execute("plug", "-f", pomPath.toString(), "org.apache.maven.plugins:maven-failsafe-plugin");
assertSame(0, ec, "Exit code");

System.out.println(Files.readString(pomPath));

var expr = "/project/build/pluginManagement/plugins/plugin[artifactId='maven-failsafe-plugin' and version]";
assertXpath(pomPath, expr, 1);
}
}

0 comments on commit 3411352

Please sign in to comment.