From 57effdd16e0c46888b41b250d9a31937b96f8ddf Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Mon, 18 Dec 2023 13:01:04 +0100 Subject: [PATCH] [MNG-7959] User controlled relocations Addendum (#1349) The "ban" was unfinished and wrong: what is actually needed is to have exception with proper explanation instead. --- https://issues.apache.org/jira/browse/MNG-7959 --- .../DefaultArtifactDescriptorReader.java | 8 ++++-- .../MavenArtifactRelocationSource.java | 8 ++++-- ...ionManagementArtifactRelocationSource.java | 9 +++--- ...serPropertiesArtifactRelocationSource.java | 28 ++++++++++++++----- 4 files changed, 36 insertions(+), 17 deletions(-) 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 d45ebeba1d..31660e2799 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 @@ -237,7 +237,7 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader throw new ArtifactDescriptorException(result); } - Artifact relocatedArtifact = getRelocation(session, request, model); + Artifact relocatedArtifact = getRelocation(session, result, model); if (relocatedArtifact != null) { result.addRelocation(a); a = relocatedArtifact; @@ -259,10 +259,12 @@ public class DefaultArtifactDescriptorReader implements ArtifactDescriptorReader return props; } - private Artifact getRelocation(RepositorySystemSession session, ArtifactDescriptorRequest request, Model model) { + private Artifact getRelocation( + RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model) + throws ArtifactDescriptorException { Artifact result = null; for (MavenArtifactRelocationSource source : artifactRelocationSources.values()) { - result = source.relocatedTarget(session, request, model); + result = source.relocatedTarget(session, artifactDescriptorResult, model); if (result != null) { break; } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenArtifactRelocationSource.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenArtifactRelocationSource.java index 1f6328f3f3..be4490248f 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenArtifactRelocationSource.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/MavenArtifactRelocationSource.java @@ -21,7 +21,8 @@ package org.apache.maven.repository.internal; import org.apache.maven.model.Model; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.resolution.ArtifactDescriptorRequest; +import org.eclipse.aether.resolution.ArtifactDescriptorException; +import org.eclipse.aether.resolution.ArtifactDescriptorResult; /** * Maven relocation source. @@ -33,9 +34,10 @@ public interface MavenArtifactRelocationSource { * Returns {@link Artifact} instance where to relocate to, or {@code null}. * * @param session The session, never {@code null}. - * @param request The artifact descriptor request, never {@code null}. + * @param result The artifact descriptor result, never {@code null}. * @param model The artifact model, never {@code null}. * @return The {@link Artifact} to relocate to, or {@code null} if no relocation wanted. */ - Artifact relocatedTarget(RepositorySystemSession session, ArtifactDescriptorRequest request, Model model); + Artifact relocatedTarget(RepositorySystemSession session, ArtifactDescriptorResult result, Model model) + throws ArtifactDescriptorException; } diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/DistributionManagementArtifactRelocationSource.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/DistributionManagementArtifactRelocationSource.java index e4033c9dec..c68e7a2c11 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/DistributionManagementArtifactRelocationSource.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/DistributionManagementArtifactRelocationSource.java @@ -28,7 +28,7 @@ import org.apache.maven.repository.internal.MavenArtifactRelocationSource; import org.apache.maven.repository.internal.RelocatedArtifact; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; -import org.eclipse.aether.resolution.ArtifactDescriptorRequest; +import org.eclipse.aether.resolution.ArtifactDescriptorResult; import org.eclipse.sisu.Priority; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,13 +49,14 @@ public final class DistributionManagementArtifactRelocationSource implements Mav private static final Logger LOGGER = LoggerFactory.getLogger(DistributionManagementArtifactRelocationSource.class); @Override - public Artifact relocatedTarget(RepositorySystemSession session, ArtifactDescriptorRequest request, Model model) { + public Artifact relocatedTarget( + RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model) { DistributionManagement distMgmt = model.getDistributionManagement(); if (distMgmt != null) { Relocation relocation = distMgmt.getRelocation(); if (relocation != null) { Artifact result = new RelocatedArtifact( - request.getArtifact(), + artifactDescriptorResult.getRequest().getArtifact(), relocation.getGroupId(), relocation.getArtifactId(), null, @@ -64,7 +65,7 @@ public final class DistributionManagementArtifactRelocationSource implements Mav relocation.getMessage()); LOGGER.debug( "The artifact {} has been relocated to {}: {}", - request.getArtifact(), + artifactDescriptorResult.getRequest().getArtifact(), result, relocation.getMessage()); return result; diff --git a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java index fd3710b9e9..0ea2497e09 100644 --- a/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java +++ b/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/relocation/UserPropertiesArtifactRelocationSource.java @@ -34,7 +34,8 @@ import org.apache.maven.repository.internal.RelocatedArtifact; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; -import org.eclipse.aether.resolution.ArtifactDescriptorRequest; +import org.eclipse.aether.resolution.ArtifactDescriptorException; +import org.eclipse.aether.resolution.ArtifactDescriptorResult; import org.eclipse.sisu.Priority; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,15 +54,28 @@ public final class UserPropertiesArtifactRelocationSource implements MavenArtifa private static final String CONFIG_PROP_RELOCATIONS_ENTRIES = "maven.relocations.entries"; + private static final Artifact SENTINEL = new DefaultArtifact("org.apache.maven.banned:user-relocation:1.0"); + @Override - public Artifact relocatedTarget(RepositorySystemSession session, ArtifactDescriptorRequest request, Model model) { + public Artifact relocatedTarget( + RepositorySystemSession session, ArtifactDescriptorResult artifactDescriptorResult, Model model) + throws ArtifactDescriptorException { Relocations relocations = (Relocations) session.getData() .computeIfAbsent(getClass().getName() + ".relocations", () -> parseRelocations(session)); if (relocations != null) { - Relocation relocation = relocations.getRelocation(request.getArtifact()); - if (relocation != null && (isProjectContext(request.getRequestContext()) || relocation.global)) { + Artifact original = artifactDescriptorResult.getRequest().getArtifact(); + Relocation relocation = relocations.getRelocation(original); + if (relocation != null + && (isProjectContext(artifactDescriptorResult.getRequest().getRequestContext()) + || relocation.global)) { + if (relocation.target == SENTINEL) { + String message = "The artifact " + original + " has been banned from resolution: " + + (relocation.global ? "User global ban" : "User project ban"); + LOGGER.debug(message); + throw new ArtifactDescriptorException(artifactDescriptorResult, message); + } Artifact result = new RelocatedArtifact( - request.getArtifact(), + original, isAny(relocation.target.getGroupId()) ? null : relocation.target.getGroupId(), isAny(relocation.target.getArtifactId()) ? null : relocation.target.getArtifactId(), isAny(relocation.target.getClassifier()) ? null : relocation.target.getClassifier(), @@ -70,7 +84,7 @@ public final class UserPropertiesArtifactRelocationSource implements MavenArtifa relocation.global ? "User global relocation" : "User project relocation"); LOGGER.debug( "The artifact {} has been relocated to {}: {}", - request.getArtifact(), + original, result, relocation.global ? "User global relocation" : "User project relocation"); return result; @@ -169,7 +183,7 @@ public final class UserPropertiesArtifactRelocationSource implements MavenArtifa if (parts.length > 1) { t = parseArtifact(parts[1]); } else { - t = new DefaultArtifact("org.apache.maven.banned:user-relocation:1.0"); + t = SENTINEL; } return new Relocation(global, s, t); })