-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4299aff
commit 7eb376f
Showing
3 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
rewrite-maven/src/main/java/org/openrewrite/maven/trait/MavenPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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<Xml.Tag> { | ||
static final XPathMatcher PLUGIN_MATCHER = new XPathMatcher("//plugins/plugin"); | ||
|
||
Cursor cursor; | ||
String groupId; | ||
|
||
@Nullable | ||
String artifactId; | ||
|
||
public static class Matcher extends MavenTraitMatcher<MavenPlugin> { | ||
|
||
@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<String> 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); | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
rewrite-maven/src/test/java/org/openrewrite/maven/trait/MavenPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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( | ||
""" | ||
<project> | ||
<groupId>com.mycompany.app</groupId> | ||
<artifactId>my-app</artifactId> | ||
<version>1</version> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-bootstrap-maven-plugin</artifactId> | ||
<version>3.0.0.Beta1</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
""", | ||
""" | ||
<project> | ||
<groupId>com.mycompany.app</groupId> | ||
<artifactId>my-app</artifactId> | ||
<version>1</version> | ||
<build> | ||
<plugins> | ||
<!--~~(io.quarkus:quarkus-bootstrap-maven-plugin)~~>--><plugin> | ||
<groupId>io.quarkus</groupId> | ||
<artifactId>quarkus-bootstrap-maven-plugin</artifactId> | ||
<version>3.0.0.Beta1</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> | ||
""" | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters