Skip to content

Commit

Permalink
AddPlugin now prefers to add plugins to the parent pom if it is in …
Browse files Browse the repository at this point in the history
…the same repository (#4583)

---------

Co-authored-by: Sam Snyder <[email protected]>
  • Loading branch information
Laurens-W and sambsnyd committed Oct 17, 2024
1 parent 23ca65e commit e935401
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
12 changes: 10 additions & 2 deletions rewrite-maven/src/main/java/org/openrewrite/maven/AddPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.intellij.lang.annotations.Language;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.maven.tree.MavenResolutionResult;
import org.openrewrite.xml.AddToTagVisitor;
import org.openrewrite.xml.ChangeTagValueVisitor;
import org.openrewrite.xml.XPathMatcher;
Expand Down Expand Up @@ -109,6 +110,12 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
if (filePattern != null) {
return PathUtils.matchesGlob(sourceFile.getSourcePath(), filePattern) && super.isAcceptable(sourceFile, ctx);
}

MavenResolutionResult mrr = sourceFile.getMarkers().findFirst(MavenResolutionResult.class).orElse(null);
if (mrr == null || mrr.parentPomIsProjectPom()) {
return false;
}

return super.isAcceptable(sourceFile, ctx);
}

Expand All @@ -129,7 +136,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (BUILD_MATCHER.matches(getCursor())) {
Optional<Xml.Tag> maybePlugins = t.getChild("plugins");
Xml.Tag plugins;
if(maybePlugins.isPresent()) {
if (maybePlugins.isPresent()) {
plugins = maybePlugins.get();
} else {
t = (Xml.Tag) new AddToTagVisitor<>(t, Xml.Tag.build("<plugins/>")).visitNonNull(t, ctx, getCursor().getParentOrThrow());
Expand All @@ -153,7 +160,8 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}
}
} else {
Xml.Tag pluginTag = Xml.Tag.build("<plugin>\n" +
Xml.Tag pluginTag = Xml.Tag.build(
"<plugin>\n" +
"<groupId>" + groupId + "</groupId>\n" +
"<artifactId>" + artifactId + "</artifactId>\n" +
(version != null ? "<version>" + version + "</version>\n" : "") +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Predicate;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static org.openrewrite.internal.StringUtils.matchesGlob;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -192,8 +193,24 @@ public Map<Path, Pom> getProjectPoms() {
return getProjectPomsRecursive(new HashMap<>());
}

/**
* Often recipes operating on multi-module projects will prefer to make changes to the parent pom rather than in multiple child poms.
* But if the parent isn't in the same repository as the child, the recipe would need to be applied to the child poms.
*
* @return true when the parent pom of the current pom is present in the same repository as the current pom
*/
public boolean parentPomIsProjectPom() {
if (getParent() == null) {
return false;
}
ResolvedGroupArtifactVersion parentGav = getParent().getPom().getGav();
return getProjectPoms().values().stream()
.map(Pom::getGav)
.anyMatch(gav -> gav.equals(parentGav));
}

private Map<Path, Pom> getProjectPomsRecursive(Map<Path, Pom> projectPoms) {
projectPoms.put(pom.getRequested().getSourcePath(), pom.getRequested());
projectPoms.put(requireNonNull(pom.getRequested().getSourcePath()), pom.getRequested());
if (parent != null) {
parent.getProjectPomsRecursive(projectPoms);
}
Expand Down
121 changes: 121 additions & 0 deletions rewrite-maven/src/test/java/org/openrewrite/maven/AddPluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.mavenProject;
import static org.openrewrite.maven.Assertions.pomXml;

class AddPluginTest implements RewriteTest {
Expand Down Expand Up @@ -361,4 +362,124 @@ void addPluginWithExistingPlugin() {
)
);
}

@Test
void addPluginOnlyToRootPom() {
rewriteRun(
spec -> spec.recipe(new AddPlugin("org.springframework.boot", "spring-boot-maven-plugin", "3.1.5", null, null, null, null)),
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<plugins>
</plugins>
</build>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.path("pom.xml")
),
mavenProject("my-app-child",
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app-child</artifactId>
<version>1</version>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<build>
<plugins>
</plugins>
</build>
</project>
"""))
);
}

@Test
void addPluginOnlyToRootPomWithParent() {
rewriteRun(
spec -> spec.recipe(new AddPlugin("org.springframework.boot", "spring-boot-maven-plugin", "3.1.5", null, null, null, null)),
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
</parent>
<build>
<plugins>
</plugins>
</build>
</project>
""",
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.5</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>3.1.5</version>
</plugin>
</plugins>
</build>
</project>
""",
spec -> spec.path("pom.xml")
),
mavenProject("my-app-child",
pomXml(
"""
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app-child</artifactId>
<version>1</version>
<parent>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</parent>
<build>
<plugins>
</plugins>
</build>
</project>
"""))
);
}
}

0 comments on commit e935401

Please sign in to comment.