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 244bd6b984e..d01265f43d1 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 31660e27992..a8c61693fa6 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) {