From ad5e085ebe72d5799122d7da575fd1fce36a6965 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 18 Dec 2023 16:31:24 +0100 Subject: [PATCH] [MNG-7959] User controlled rewrite (#1351) Extra handling if redirection happens within same GAV. --- https://issues.apache.org/jira/browse/MNG-7959 --- .../internal/ArtifactDescriptorUtils.java | 11 ++++++++++ .../DefaultArtifactDescriptorReader.java | 22 ++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java index 244bd6b984ef..d01265f43d1e 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/ArtifactDescriptorUtils.java @@ -43,6 +43,17 @@ public static Artifact toPomArtifact(Artifact artifact) { return pomArtifact; } + /** + * Creates POM artifact out of passed in artifact by dropping classifier (if exists) and rewriting extension to + * "pom". Unconditionally, unlike {@link #toPomArtifact(Artifact)} that does this only for artifacts without + * classifiers. + * + * @since 4.0.0 + */ + public static Artifact toPomArtifactUnconditionally(Artifact artifact) { + return new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion()); + } + public static RemoteRepository toRemoteRepository(Repository repository) { RemoteRepository.Builder builder = new RemoteRepository.Builder(repository.getId(), repository.getLayout(), repository.getUrl()); diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java index 31660e279928..a8c61693fa64 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java @@ -26,7 +26,6 @@ import java.util.Map; import java.util.Objects; import java.util.Properties; -import java.util.Set; import org.apache.maven.model.Model; import org.apache.maven.model.building.ArtifactModelSource; @@ -129,9 +128,9 @@ private Model loadPom( throws ArtifactDescriptorException { RequestTrace trace = RequestTrace.newChild(request.getTrace(), request); - Set visited = new LinkedHashSet<>(); + LinkedHashSet visited = new LinkedHashSet<>(); for (Artifact a = request.getArtifact(); ; ) { - Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifact(a); + Artifact pomArtifact = ArtifactDescriptorUtils.toPomArtifactUnconditionally(a); try { VersionRequest versionRequest = new VersionRequest(a, request.getRepositories(), request.getRequestContext()); @@ -239,15 +238,26 @@ private Model loadPom( Artifact relocatedArtifact = getRelocation(session, result, model); if (relocatedArtifact != null) { - result.addRelocation(a); - a = relocatedArtifact; - result.setArtifact(a); + if (withinSameGav(relocatedArtifact, a)) { + result.setArtifact(relocatedArtifact); + return model; // they share same model + } else { + result.addRelocation(a); + a = relocatedArtifact; + result.setArtifact(a); + } } else { return model; } } } + private boolean withinSameGav(Artifact a1, Artifact a2) { + return Objects.equals(a1.getGroupId(), a2.getGroupId()) + && Objects.equals(a1.getArtifactId(), a2.getArtifactId()) + && Objects.equals(a1.getVersion(), a2.getVersion()); + } + private Properties toProperties(Map dominant, Map recessive) { Properties props = new Properties(); if (recessive != null) {