Skip to content

Commit

Permalink
Handle plugins without groupId (#4507)
Browse files Browse the repository at this point in the history
* Handle plugins without groupId

* Minor polish

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
DidierLoiseau and timtebeek authored Sep 20, 2024
1 parent 3dc8dde commit 20c44d0
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.openrewrite.internal.StringUtils.matchesGlob;
import static org.openrewrite.maven.tree.Plugin.PLUGIN_DEFAULT_GROUPID;

public class MavenVisitor<P> extends XmlVisitor<P> {

Expand Down Expand Up @@ -403,7 +404,7 @@ public boolean isDependencyLikeTag() {
for (Plugin resolvedPlugin : plugins) {
String reqGroup = resolvedPlugin.getGroupId();
String reqVersion = resolvedPlugin.getVersion();
if ((reqGroup == null || reqGroup.equals(tag.getChildValue("groupId").orElse(null))) &&
if (reqGroup.equals(tag.getChildValue("groupId").orElse(PLUGIN_DEFAULT_GROUPID)) &&
resolvedPlugin.getArtifactId().equals(tag.getChildValue("artifactId").orElse(null)) &&
(reqVersion == null || reqVersion.equals(tag.getChildValue("version").orElse(null)))) {
return resolvedPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.openrewrite.maven.tree.Plugin.PLUGIN_DEFAULT_GROUPID;

/**
* A value object deserialized directly from POM XML
Expand Down Expand Up @@ -249,6 +250,7 @@ public static class PluginManagement {
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@Data
public static class Plugin {
@Nullable
String groupId;
String artifactId;

Expand Down Expand Up @@ -510,9 +512,9 @@ private List<org.openrewrite.maven.tree.Plugin> mapPlugins(@Nullable List<Plugin
if (rawPlugins != null) {
plugins = new ArrayList<>(rawPlugins.size());
for (Plugin rawPlugin : rawPlugins) {

String pluginGroupId = rawPlugin.getGroupId();
plugins.add(new org.openrewrite.maven.tree.Plugin(
rawPlugin.getGroupId(),
pluginGroupId == null ? PLUGIN_DEFAULT_GROUPID : pluginGroupId,
rawPlugin.getArtifactId(),
rawPlugin.getVersion(),
rawPlugin.getExtensions(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
@Value
public class Plugin {
// default value as per https://maven.apache.org/xsd/maven-4.0.0.xsd
public static final String PLUGIN_DEFAULT_GROUPID = "org.apache.maven.plugins";

String groupId;
String artifactId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpec;

import java.util.Collections;

Expand Down Expand Up @@ -1290,6 +1291,170 @@ void removeRedundantVersionsFromPluginsManagedByParent() {
);
}

@Test
void removeRedundantVersionsFromPluginsManagedByParentNotSpecifyingGroupId() {
rewriteRun(
spec -> spec.recipe(new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration><whatever/></configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
""", SourceSpec::skip),
mavenProject("child", pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>sample</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<something-else/>
</configuration>
</plugin>
</plugins>
</build>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>sample</artifactId>
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<something-else/>
</configuration>
</plugin>
</plugins>
</build>
</project>
"""
)
)
);
}

@Test
void removeRedundantDmVersionsFromPluginsManagedByParentNotSpecifyingGroupId() {
rewriteRun(
spec -> spec.recipe(new RemoveRedundantDependencyVersions(null, null, RemoveRedundantDependencyVersions.Comparator.GTE, null)),
pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration><whatever/></configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
""", SourceSpec::skip),
mavenProject("child", pomXml(
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>sample</artifactId>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<something-else/>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
""",
"""
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<artifactId>sample</artifactId>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<something-else/>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
"""
)
)
);
}

@Test
void keepPluginIfParentInheritanceDisabled() {
rewriteRun(
Expand Down

0 comments on commit 20c44d0

Please sign in to comment.