Skip to content

Commit

Permalink
Plugin configuration can be null (#3931)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jan 17, 2024
1 parent 97fd147 commit b760d96
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
13 changes: 13 additions & 0 deletions rewrite-maven/src/main/java/org/openrewrite/maven/tree/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class Plugin {
@Nullable
String inherited;

@Nullable
JsonNode configuration;

List<Dependency> dependencies;
Expand All @@ -66,6 +67,10 @@ public static class Execution {

@Nullable
public String getConfigurationStringValue(String path) {
if (configuration == null) {
return null;
}

JsonNode current = configuration;
if (!path.isEmpty()) {
String[] elements = path.split("\\.");
Expand All @@ -81,6 +86,10 @@ public String getConfigurationStringValue(String path) {

@Nullable
public <T> T getConfiguration(String path, Class<T> configClass) {
if (configuration == null) {
return null;
}

JsonNode current = configuration;
if (!path.isEmpty()) {
String[] elements = path.split("\\.");
Expand All @@ -102,6 +111,10 @@ public <T> T getConfiguration(String path, Class<T> configClass) {
}

public <T> List<T> getConfigurationList(String path, Class<T> elementClass) {
if (configuration == null) {
return Collections.emptyList();
}

JsonNode current = configuration;
if (!path.isEmpty()) {
String[] elements = path.split("\\.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2350,10 +2350,41 @@ void plugins() {
</build>
</project>
""",
spec -> spec.afterRecipe(pomXml ->
assertThat(pomXml.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom().getPlugins()
.get(0).getArtifactId())
.isEqualTo("maven-compiler-plugin")
spec -> spec.afterRecipe(pomXml -> {
Plugin plugin = pomXml.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom().getPlugins().get(0);
assertThat(plugin.getArtifactId()).isEqualTo("maven-compiler-plugin");
assertThat(plugin.getConfiguration()).isNotNull();
}
)
)
);
}

@Test
void pluginWithoutConfig() {
rewriteRun(
pomXml(
"""
<project>
<groupId>org.openrewrite.maven</groupId>
<artifactId>a</artifactId>
<version>0.1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.afterRecipe(pomXml -> {
Plugin plugin = pomXml.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom().getPlugins().get(0);
assertThat(plugin.getArtifactId()).isEqualTo("maven-compiler-plugin");
assertThat(plugin.getConfiguration()).isNull();
}
)
)
);
Expand Down

0 comments on commit b760d96

Please sign in to comment.