diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenPlugin.java b/rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenPlugin.java new file mode 100644 index 00000000000..95be7e1f920 --- /dev/null +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenPlugin.java @@ -0,0 +1,71 @@ +/* + * Copyright 2024 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 + *

+ * https://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 org.openrewrite.maven.trait; + +import lombok.Value; +import org.openrewrite.Cursor; +import org.openrewrite.internal.lang.Nullable; +import org.openrewrite.trait.Trait; +import org.openrewrite.xml.XPathMatcher; +import org.openrewrite.xml.tree.Xml; + +import java.util.Optional; + +@Value +public class MavenPlugin implements Trait { + static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin"); + + Cursor cursor; + String groupId; + + @Nullable + String artifactId; + + public static class Matcher extends MavenTraitMatcher { + + @Override + protected @Nullable MavenPlugin test(Cursor cursor) { + Object value = cursor.getValue(); + if (value instanceof Xml.Tag) { + Xml.Tag tag = (Xml.Tag) value; + + // `XPathMatcher` is still a bit expensive + if (!"plugin".equals(tag.getName()) || + (!PLUGIN_MATCHER.matches(cursor))) { + return null; + } + + return new MavenPlugin( + cursor, + getProperty(cursor, "groupId").orElse("org.apache.maven.plugins"), + getProperty(cursor, "artifactId").orElse(null) + ); + } + return null; + } + + private Optional getProperty(Cursor cursor, String property) { + Xml.Tag tag = cursor.getValue(); + if (getResolutionResult(cursor).getPom().getProperties() != null) { + if (tag.getChildValue(property).isPresent() && tag.getChildValue(property).get().trim().startsWith("${")) { + String propertyKey = tag.getChildValue(property).get().trim(); + return Optional.ofNullable(getResolutionResult(cursor).getPom().getValue(propertyKey)); + } + } + return tag.getChildValue(property); + } + } +} diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java new file mode 100644 index 00000000000..876394715f2 --- /dev/null +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2024 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 + *

+ * https://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 org.openrewrite.maven.trait; + +import org.junit.jupiter.api.Test; +import org.openrewrite.marker.SearchResult; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.maven.Assertions.pomXml; +import static org.openrewrite.maven.trait.Traits.mavenPlugin; + +public class MavenPluginTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipe(RewriteTest.toRecipe(() -> mavenPlugin().asVisitor(plugin -> + SearchResult.found(plugin.getTree(), plugin.getGroupId() + ":" + plugin.getArtifactId())))); + } + + @Test + void findMavenPlugin() { + rewriteRun( + pomXml( + """ + + com.mycompany.app + my-app + 1 + + + + io.quarkus + quarkus-bootstrap-maven-plugin + 3.0.0.Beta1 + + + + + """, + """ + + com.mycompany.app + my-app + 1 + + + + io.quarkus + quarkus-bootstrap-maven-plugin + 3.0.0.Beta1 + + + + + """ + ) + ); + } +} diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/trait/Traits.java b/rewrite-maven/src/test/java/org/openrewrite/maven/trait/Traits.java index 36fd5fe372a..b62fa1e8251 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/trait/Traits.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/trait/Traits.java @@ -22,4 +22,8 @@ private Traits() { public static MavenDependency.Matcher mavenDependency() { return new MavenDependency.Matcher(); } + + public static MavenPlugin.Matcher mavenPlugin() { + return new MavenPlugin.Matcher(); + } }