Skip to content

Commit 22ac624

Browse files
authored
Resolve Maven property references when matching dependency tags (#7351)
Fixes #4796 — `RemoveDependency` (and other recipes using `isDependencyTag` or `findDependency`) failed to match dependencies whose groupId or artifactId was defined via a Maven property (e.g. `${junit.groupId}`). The root cause was that `isDependencyTag` and `findDependency` in `MavenVisitor` compared raw XML tag values against resolved dependency coordinates without first resolving property placeholders. Now both methods use `ResolvedPom.getValue()` to expand properties before comparing.
1 parent 6d85e4a commit 22ac624

2 files changed

Lines changed: 70 additions & 5 deletions

File tree

rewrite-maven/src/main/java/org/openrewrite/maven/MavenVisitor.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,13 @@ public boolean isDependencyTag(String groupId, String artifactId) {
146146
}
147147
}
148148
Dependency req = resolvedDependency.getRequested();
149-
String reqGroup = req.getGroupId();
150-
if ((reqGroup == null || reqGroup.equals(tag.getChildValue("groupId").orElse(null))) &&
151-
req.getArtifactId().equals(tag.getChildValue("artifactId").orElse(null)) &&
149+
ResolvedPom pom = getResolutionResult().getPom();
150+
String reqGroup = pom.getValue(req.getGroupId());
151+
String reqArtifact = pom.getValue(req.getArtifactId());
152+
String tagGroupId = pom.getValue(tag.getChildValue("groupId").orElse(null));
153+
String tagArtifactId = pom.getValue(tag.getChildValue("artifactId").orElse(null));
154+
if ((reqGroup == null || reqGroup.equals(tagGroupId)) &&
155+
reqArtifact.equals(tagArtifactId) &&
152156
scope == tagScope) {
153157
return true;
154158
}
@@ -413,11 +417,14 @@ protected boolean isProperty(@Nullable String value) {
413417
if (inClasspathOf != null && tagScope != inClasspathOf && !tagScope.isInClasspathOf(inClasspathOf)) {
414418
return null;
415419
}
420+
ResolvedPom resolvedPom = getResolutionResult().getPom();
416421
for (Map.Entry<Scope, List<ResolvedDependency>> scope : getResolutionResult().getDependencies().entrySet()) {
417422
if (inClasspathOf == null || scope.getKey() == inClasspathOf || scope.getKey().isInClasspathOf(inClasspathOf)) {
418423
for (ResolvedDependency d : scope.getValue()) {
419-
if (tag.getChildValue("groupId").orElse(getResolutionResult().getPom().getGroupId()).equals(d.getGroupId()) &&
420-
tag.getChildValue("artifactId").orElse(getResolutionResult().getPom().getArtifactId()).equals(d.getArtifactId())) {
424+
String tagGroupId = resolvedPom.getValue(tag.getChildValue("groupId").orElse(null));
425+
String tagArtifactId = resolvedPom.getValue(tag.getChildValue("artifactId").orElse(null));
426+
if ((tagGroupId != null ? tagGroupId : resolvedPom.getGroupId()).equals(d.getGroupId()) &&
427+
(tagArtifactId != null ? tagArtifactId : resolvedPom.getArtifactId()).equals(d.getArtifactId())) {
421428
return d;
422429
}
423430
}

rewrite-maven/src/test/java/org/openrewrite/maven/RemoveDependencyTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,62 @@ void removeDependencyWithTypeZip() {
312312
)
313313
);
314314
}
315+
316+
@Test
317+
@Issue("https://github.com/openrewrite/rewrite/issues/4796")
318+
void removeDependencyDefinedWithProperties() {
319+
rewriteRun(
320+
spec -> spec.recipe(new RemoveDependency("junit", "junit", null)),
321+
pomXml(
322+
"""
323+
<project>
324+
<modelVersion>4.0.0</modelVersion>
325+
326+
<groupId>com.mycompany.app</groupId>
327+
<artifactId>my-app</artifactId>
328+
<version>1</version>
329+
330+
<properties>
331+
<junit.groupId>junit</junit.groupId>
332+
</properties>
333+
334+
<dependencies>
335+
<dependency>
336+
<groupId>com.google.guava</groupId>
337+
<artifactId>guava</artifactId>
338+
<version>29.0-jre</version>
339+
</dependency>
340+
<dependency>
341+
<groupId>${junit.groupId}</groupId>
342+
<artifactId>junit</artifactId>
343+
<version>4.13.1</version>
344+
<scope>test</scope>
345+
</dependency>
346+
</dependencies>
347+
</project>
348+
""",
349+
"""
350+
<project>
351+
<modelVersion>4.0.0</modelVersion>
352+
353+
<groupId>com.mycompany.app</groupId>
354+
<artifactId>my-app</artifactId>
355+
<version>1</version>
356+
357+
<properties>
358+
<junit.groupId>junit</junit.groupId>
359+
</properties>
360+
361+
<dependencies>
362+
<dependency>
363+
<groupId>com.google.guava</groupId>
364+
<artifactId>guava</artifactId>
365+
<version>29.0-jre</version>
366+
</dependency>
367+
</dependencies>
368+
</project>
369+
"""
370+
)
371+
);
372+
}
315373
}

0 commit comments

Comments
 (0)