From ecfe2914e0a0ee6ee6e0fbbb577b163b8ac3fb4d Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Sat, 9 Dec 2023 22:46:01 +0100 Subject: [PATCH] Fix `StackOverflowError` in `MavenPomDownloader` (#3801) When building gridgain/gridgain@main there is a `StackOverflowError` because the root project references `parent/pom.xml` as its parent and the latter contains ``. But that actually means that Maven should *not* look for the parent in the current project. --- .../maven/internal/MavenPomDownloader.java | 4 +- .../openrewrite/maven/MavenParserTest.java | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java index d4c1df8b89c..3f3076eddd9 100644 --- a/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java +++ b/rewrite-maven/src/main/java/org/openrewrite/maven/internal/MavenPomDownloader.java @@ -208,7 +208,9 @@ private Pom getParentWithinProject(Pom projectPom, Map projectPoms) { .resolve("..") .resolve(Paths.get(relativePath)) .normalize(); - return projectPoms.get(parentPath); + Pom parentPom = projectPoms.get(parentPath); + return parentPom != null && parentPom.getGav().getGroupId().equals(parent.getGav().getGroupId()) && + parentPom.getGav().getArtifactId().equals(parent.getGav().getArtifactId()) ? parentPom : null; } public MavenMetadata downloadMetadata(GroupArtifact groupArtifact, @Nullable ResolvedPom containingPom, List repositories) throws MavenDownloadingException { diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java index cf032d7b45b..3a292392647 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/MavenParserTest.java @@ -1027,6 +1027,44 @@ void recursivePropertyFromParentPoms() { ); } + @Test + void nestedParentWithDownloadedParent() { + rewriteRun( + mavenProject("root", + pomXml( + """ + + + org.apache + apache + 16 + + + org.openrewrite.maven + parent + 0.1.0-SNAPSHOT + + """, + spec -> spec.path("parent/pom.xml") + ), + pomXml( + """ + + + org.openrewrite.maven + parent + 0.1.0-SNAPSHOT + parent/pom.xml + + root + + """, + spec -> spec.path("pom.xml") + ) + ) + ); + } + // a depends on d without specifying version number. a's parent is b // b imports c into its dependencyManagement section // c's dependencyManagement specifies the version of d to use