You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2026/09/04 21:40:25 UTC

(maven-resolver) 01/01: Qualify local repository tracking entries with the repository URL on 1.9.x

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch agent/resolver-19x-tracking-key-url
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git

commit b071e9c00a73341def6d12a745eaf46845b4e247
Author: Sylwester Lachiewicz <sl...@apache.org>
AuthorDate: Fri Sep 4 23:40:11 2026 +0200

    Qualify local repository tracking entries with the repository URL on 1.9.x
    
    The repository component of a _remote.repositories entry was the bare
    repository id, so two repositories sharing an id were the same origin to
    the enhanced local repository manager. The entry is now the id plus a
    SHA-1 of the URL, the same shape as the nid_hurl key function on the 2.x
    line, so tracking files are shared between the lines. Entries written
    under the old shape do not match and the artifact is fetched again once.
    The previous shape is available under
    aether.enhancedLocalRepository.trackingRepositoryKeyFunction=nid.
---
 .../impl/EnhancedLocalRepositoryManager.java       | 75 +++++++++++++++++++++-
 .../EnhancedLocalRepositoryManagerFactory.java     | 33 +++++++++-
 .../impl/EnhancedLocalRepositoryManagerTest.java   | 61 ++++++++++++++++++
 src/site/markdown/configuration.md                 |  1 +
 4 files changed, 167 insertions(+), 3 deletions(-)

diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
index 8191f0b09..fa11da1ce 100644
--- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
+++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
@@ -34,6 +34,8 @@ import org.eclipse.aether.repository.LocalArtifactRegistration;
 import org.eclipse.aether.repository.LocalArtifactRequest;
 import org.eclipse.aether.repository.LocalArtifactResult;
 import org.eclipse.aether.repository.RemoteRepository;
+import org.eclipse.aether.util.PathUtils;
+import org.eclipse.aether.util.StringDigestUtil;
 
 import static java.util.Objects.requireNonNull;
 
@@ -54,6 +56,10 @@ import static java.util.Objects.requireNonNull;
  * artifact-1.0.pom>my_repo_id=
  * </pre>
  *
+ * The repository component of a tracking key is produced by the tracking repository key function, which is
+ * URL-qualified by default: two repositories that merely share an id but point at different URLs are tracked as
+ * different origins (see {@link EnhancedLocalRepositoryManagerFactory}).
+ *
  * @see EnhancedLocalRepositoryManagerFactory
  */
 class EnhancedLocalRepositoryManager extends SimpleLocalRepositoryManager {
@@ -66,16 +72,81 @@ class EnhancedLocalRepositoryManager extends SimpleLocalRepositoryManager {
 
     private final LocalPathPrefixComposer localPathPrefixComposer;
 
+    /**
+     * The functions producing the repository component of a tracking entry.
+     */
+    enum TrackingKeyFunction {
+        /**
+         * The repository id alone, as written by earlier releases.
+         */
+        NID,
+        /**
+         * The repository id qualified with a hash of the repository URL, see
+         * {@link #urlQualifiedRepositoryKey(RemoteRepository, String)}.
+         */
+        NID_HURL
+    }
+
+    /**
+     * Key function used solely for the tracking entries (the repository component of the keys in the tracking
+     * file); path composition keeps using {@link #getRepositoryKey(RemoteRepository, String)}. Entries written
+     * under a different key function never match a lookup: the artifact stays tracked, so the untracked inter-op
+     * branch in {@link #checkFind} does not accept it, but unavailable, and is fetched again.
+     */
+    private final TrackingKeyFunction trackingKeyFunction;
+
     EnhancedLocalRepositoryManager(
             File basedir,
             LocalPathComposer localPathComposer,
             String trackingFilename,
             TrackingFileManager trackingFileManager,
             LocalPathPrefixComposer localPathPrefixComposer) {
+        this(
+                basedir,
+                localPathComposer,
+                trackingFilename,
+                trackingFileManager,
+                localPathPrefixComposer,
+                TrackingKeyFunction.NID_HURL);
+    }
+
+    EnhancedLocalRepositoryManager(
+            File basedir,
+            LocalPathComposer localPathComposer,
+            String trackingFilename,
+            TrackingFileManager trackingFileManager,
+            LocalPathPrefixComposer localPathPrefixComposer,
+            TrackingKeyFunction trackingKeyFunction) {
         super(basedir, "enhanced", localPathComposer);
         this.trackingFilename = requireNonNull(trackingFilename);
         this.trackingFileManager = requireNonNull(trackingFileManager);
         this.localPathPrefixComposer = requireNonNull(localPathPrefixComposer);
+        this.trackingKeyFunction = requireNonNull(trackingKeyFunction);
+    }
+
+    /**
+     * The URL-qualified tracking key: the repository id as a path segment, a dash, and the SHA-1 of the repository
+     * URL (with the context appended for repository managers). Same shape as the {@code nid_hurl} repository key
+     * function of the 2.x line, so tracking files are shared between the lines.
+     */
+    static String urlQualifiedRepositoryKey(RemoteRepository repository, String context) {
+        String seed = repository.getUrl();
+        if (repository.isRepositoryManager() && context != null && !context.isEmpty()) {
+            seed += context;
+        }
+        return PathUtils.stringToPathSegment(repository.getId()) + "-" + StringDigestUtil.sha1(seed);
+    }
+
+    /**
+     * Returns the tracking key of given repository. Deliberately distinct from
+     * {@link #getRepositoryKey(RemoteRepository, String)}, which is used for path composition: tracking binds an
+     * artifact to the full identity of its origin, while the on-disk layout stays unchanged.
+     */
+    private String getTrackingRepositoryKey(RemoteRepository repository, String context) {
+        if (trackingKeyFunction == TrackingKeyFunction.NID_HURL) {
+            return urlQualifiedRepositoryKey(repository, context);
+        }
+        return getRepositoryKey(repository, context);
     }
 
     private String concatPaths(String prefix, String artifactPath) {
@@ -156,7 +227,7 @@ class EnhancedLocalRepositoryManager extends SimpleLocalRepositoryManager {
             } else {
                 String context = result.getRequest().getContext();
                 for (RemoteRepository repository : result.getRequest().getRepositories()) {
-                    if (props.get(getKey(file, getRepositoryKey(repository, context))) != null) {
+                    if (props.get(getKey(file, getTrackingRepositoryKey(repository, context))) != null) {
                         // artifact downloaded from remote repository is accepted only downloaded from request
                         // repositories
                         result.setAvailable(true);
@@ -197,7 +268,7 @@ class EnhancedLocalRepositoryManager extends SimpleLocalRepositoryManager {
 
         if (contexts != null) {
             for (String context : contexts) {
-                keys.add(getRepositoryKey(repository, context));
+                keys.add(getTrackingRepositoryKey(repository, context));
             }
         }
 
diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerFactory.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerFactory.java
index a0e0a558d..0302a8490 100644
--- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerFactory.java
+++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerFactory.java
@@ -22,6 +22,8 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
+import java.util.Locale;
+
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.repository.LocalRepository;
 import org.eclipse.aether.repository.LocalRepositoryManager;
@@ -48,6 +50,20 @@ public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryMan
 
     private static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
 
+    /**
+     * The function producing the repository component of the tracking entries this manager writes and consults, and
+     * of nothing else: artifact and metadata paths and split local repository prefixes keep following the repository
+     * id. Accepted values: {@code "nid_hurl"} (default) qualifies the id with a hash of the repository URL, so two
+     * repositories merely sharing an id are tracked as different origins; {@code "nid"} is the id alone, the
+     * behavior of earlier releases. Entries written under a different function than the active one never match a
+     * lookup and never enable the untracked-file fallback: affected artifacts are treated as locally unavailable and
+     * fetched again once.
+     */
+    private static final String CONFIG_PROP_TRACKING_REPOSITORY_KEY_FUNCTION =
+            "aether.enhancedLocalRepository.trackingRepositoryKeyFunction";
+
+    private static final String DEFAULT_TRACKING_REPOSITORY_KEY_FUNCTION = "nid_hurl";
+
     private float priority = 10.0f;
 
     private LocalPathComposer localPathComposer;
@@ -98,12 +114,27 @@ public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryMan
                     localPathComposer,
                     trackingFilename,
                     trackingFileManager,
-                    localPathPrefixComposerFactory.createComposer(session));
+                    localPathPrefixComposerFactory.createComposer(session),
+                    trackingRepositoryKeyFunction(ConfigUtils.getString(
+                            session,
+                            DEFAULT_TRACKING_REPOSITORY_KEY_FUNCTION,
+                            CONFIG_PROP_TRACKING_REPOSITORY_KEY_FUNCTION)));
         } else {
             throw new NoLocalRepositoryManagerException(repository);
         }
     }
 
+    static EnhancedLocalRepositoryManager.TrackingKeyFunction trackingRepositoryKeyFunction(String name) {
+        try {
+            return EnhancedLocalRepositoryManager.TrackingKeyFunction.valueOf(name.toUpperCase(Locale.ENGLISH));
+        } catch (IllegalArgumentException e) {
+            throw new IllegalArgumentException(
+                    "Unknown " + CONFIG_PROP_TRACKING_REPOSITORY_KEY_FUNCTION + " value '" + name
+                            + "': expected nid_hurl or nid",
+                    e);
+        }
+    }
+
     @Override
     public float getPriority() {
         return priority;
diff --git a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerTest.java b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerTest.java
index 17d21599c..8b2055d80 100644
--- a/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerTest.java
+++ b/maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManagerTest.java
@@ -24,6 +24,7 @@ import java.net.URI;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Properties;
 
 import org.eclipse.aether.DefaultRepositorySystemSession;
 import org.eclipse.aether.artifact.Artifact;
@@ -45,6 +46,7 @@ import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
@@ -227,6 +229,65 @@ public class EnhancedLocalRepositoryManagerTest {
         assertEquals(repository, result.getRepository());
     }
 
+    @Test
+    public void testDoNotFindSameIdDifferentUrl() throws Exception {
+        addRemoteArtifact(artifact);
+
+        RemoteRepository sameIdElsewhere = new RemoteRepository.Builder(repository)
+                .setUrl("file:///elsewhere/")
+                .build();
+        LocalArtifactRequest request = new LocalArtifactRequest(artifact, Arrays.asList(sameIdElsewhere), testContext);
+        LocalArtifactResult result = manager.find(session, request);
+        assertNotNull(result.getFile());
+        assertFalse(result.isAvailable());
+    }
+
+    @Test
+    public void testTrackingKeyIsUrlQualified() throws Exception {
+        addRemoteArtifact(artifact);
+
+        File trackingFile = new File(
+                new File(basedir, manager.getPathForRemoteArtifact(artifact, repository, testContext)).getParentFile(),
+                "_remote.repositories");
+        Properties props = trackingFileManager.read(trackingFile);
+        assertNotNull(props);
+        String fileName = new File(manager.getPathForRemoteArtifact(artifact, repository, testContext)).getName();
+        assertEquals(
+                1,
+                props.keySet().stream()
+                        .filter(k -> k.toString().startsWith(fileName + ">"))
+                        .count());
+        String key = props.keySet().stream()
+                .map(Object::toString)
+                .filter(k -> k.startsWith(fileName + ">"))
+                .findFirst()
+                .orElseThrow(AssertionError::new);
+        assertEquals(
+                fileName + ">" + EnhancedLocalRepositoryManager.urlQualifiedRepositoryKey(repository, testContext),
+                key);
+        assertTrue(key, key.startsWith(fileName + ">" + repository.getId() + "-"));
+        assertNotEquals(fileName + ">" + repository.getId(), key);
+    }
+
+    @Test
+    public void testLegacyTrackingKeyFindsSameIdDifferentUrl() throws Exception {
+        manager = new EnhancedLocalRepositoryManager(
+                basedir,
+                new DefaultLocalPathComposer(),
+                "_remote.repositories",
+                trackingFileManager,
+                new DefaultLocalPathPrefixComposerFactory().createComposer(session),
+                EnhancedLocalRepositoryManager.TrackingKeyFunction.NID);
+        addRemoteArtifact(artifact);
+
+        RemoteRepository sameIdElsewhere = new RemoteRepository.Builder(repository)
+                .setUrl("file:///elsewhere/")
+                .build();
+        LocalArtifactRequest request = new LocalArtifactRequest(artifact, Arrays.asList(sameIdElsewhere), testContext);
+        LocalArtifactResult result = manager.find(session, request);
+        assertTrue(result.isAvailable());
+    }
+
     @Test
     public void testDoNotFindDifferentContext() throws Exception {
         addRemoteArtifact(artifact);
diff --git a/src/site/markdown/configuration.md b/src/site/markdown/configuration.md
index abee016a2..3f14a2f88 100644
--- a/src/site/markdown/configuration.md
+++ b/src/site/markdown/configuration.md
@@ -85,6 +85,7 @@ under the License.
 | `aether.enhancedLocalRepository.remotePrefix`                               | String                | The prefix to use for downloaded and cached artifacts.                                                                                                                                                                                                                                                                                                                                               [...]
 | `aether.enhancedLocalRepository.releasesPrefix`                             | String                | The prefix to use for release artifacts.                                                                                                                                                                                                                                                                                                                                                             [...]
 | `aether.enhancedLocalRepository.trackingFilename`                           | String                | Filename of the file in which to track the remote repositories.                                                                                                                                                                                                                                                                                                                                      [...]
+| `aether.enhancedLocalRepository.trackingRepositoryKeyFunction`              | String                | The function producing the repository component of the tracking entries: `nid_hurl` qualifies the repository id with a hash of its URL, `nid` uses the id alone as earlier releases did. Entries written under the other function do not match and the artifact is fetched again once. | `nid_hurl`                                                                                 | no             [...]
 | `aether.interactive`                                                        | boolean               | A flag indicating whether interaction with the user is allowed.                                                                                                                                                                                                                                                                                                                                      [...]
 | `aether.metadataResolver.threads`                                           | int                   | Number of threads to use in parallel for resolving metadata.                                                                                                                                                                                                                                                                                                                                         [...]
 | `aether.offline.protocols`                                                  | String                | Comma-separated list of protocols which are supposed to be resolved offline.                                                                                                                                                                                                                                                                                                                         [...]