Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support positional elements in XPathMatcher #5028

Open
alvarosanchez opened this issue Feb 12, 2025 · 1 comment
Open

Support positional elements in XPathMatcher #5028

alvarosanchez opened this issue Feb 12, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@alvarosanchez
Copy link

What problem are you trying to solve?

In a pom.xml, remove a duplicate jacoco-maven-plugin build plugin definition with org.openrewrite.xml.RemoveXmlTag.

Describe the solution you'd like

Use an XPath expression like /project/build/plugins/plugin[artifactId='jacoco-maven-plugin'][2] or /project/build/plugins/plugin[artifactId='jacoco-maven-plugin'][last()].

/project/build/plugins/plugin[artifactId='jacoco-maven-plugin'] works and matches both definitions. I want to match just one of them.

Have you considered any alternatives or workarounds?

There aren't, to the best of my knowledge

Are you interested in contributing this feature to OpenRewrite?

XPathMatcher has some pretty complicated logic, but if pointed to the appropriate place, I can try.

@alvarosanchez alvarosanchez added the enhancement New feature or request label Feb 12, 2025
@timtebeek
Copy link
Contributor

Thanks for the suggestion @alvarosanchez ! We had indeed only implemented a subset of functionality for our XPathMatcher, mostly since we're quite a bit performance sensitive there. While these matcher features would be nice, I do wonder if we'll use them often enough in isolation to be worth the added complexity.

For the specific case it's hard to say whether you always can or should remove the second case. I'd expect a visitor that matches both plugins, evaluates their configuration, potentially merges them, and only then removes one of the other. That means a more specific XPath match might not help a lot just yet. What are your thoughts there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants