diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java index 14651cb0d..6d23deb7a 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java @@ -18,6 +18,8 @@ */ package org.eclipse.aether.repository; +import java.nio.file.Path; + import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.metadata.Metadata; @@ -37,6 +39,18 @@ public interface LocalRepositoryManager { */ LocalRepository getRepository(); + /** + * Gets the absolute path for a locally installed artifact. Note that the artifact need not actually exist yet at + * the returned location, the path merely indicates where the artifact would eventually be stored. + * + * @param artifact The artifact for which to determine the path, must not be {@code null}. + * @return The path, relative to the local repository's base directory. + * @since 2.0.5 + */ + default Path getAbsolutePathForLocalArtifact(Artifact artifact) { + return getRepository().getBasePath().resolve(getPathForLocalArtifact(artifact)); + } + /** * Gets the relative path for a locally installed artifact. Note that the artifact need not actually exist yet at * the returned location, the path merely indicates where the artifact would eventually be stored. The path uses the @@ -44,9 +58,25 @@ public interface LocalRepositoryManager { * * @param artifact The artifact for which to determine the path, must not be {@code null}. * @return The path, relative to the local repository's base directory. + * @deprecated See {@link #getAbsolutePathForLocalArtifact(Artifact)} */ + @Deprecated String getPathForLocalArtifact(Artifact artifact); + /** + * Gets the absolute path for an artifact cached from a remote repository. Note that the artifact need not actually + * exist yet at the returned location, the path merely indicates where the artifact would eventually be stored. + * + * @param artifact The artifact for which to determine the path, must not be {@code null}. + * @param repository The source repository of the artifact, must not be {@code null}. + * @param context The resolution context in which the artifact is being requested, may be {@code null}. + * @return The path, relative to the local repository's base directory. + * @since 2.0.5 + */ + default Path getAbsolutePathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) { + return getRepository().getBasePath().resolve(getPathForRemoteArtifact(artifact, repository, context)); + } + /** * Gets the relative path for an artifact cached from a remote repository. Note that the artifact need not actually * exist yet at the returned location, the path merely indicates where the artifact would eventually be stored. The @@ -56,9 +86,23 @@ public interface LocalRepositoryManager { * @param repository The source repository of the artifact, must not be {@code null}. * @param context The resolution context in which the artifact is being requested, may be {@code null}. * @return The path, relative to the local repository's base directory. + * @deprecated See {@link #getAbsolutePathForRemoteArtifact(Artifact, RemoteRepository, String)} */ + @Deprecated String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context); + /** + * Gets the absolute path for locally installed metadata. Note that the metadata need not actually exist yet at the + * returned location, the path merely indicates where the metadata would eventually be stored. + * + * @param metadata The metadata for which to determine the path, must not be {@code null}. + * @return The path, relative to the local repository's base directory. + * @since 2.0.5 + */ + default Path getAbsolutePathForLocalMetadata(Metadata metadata) { + return getRepository().getBasePath().resolve(getPathForLocalMetadata(metadata)); + } + /** * Gets the relative path for locally installed metadata. Note that the metadata need not actually exist yet at the * returned location, the path merely indicates where the metadata would eventually be stored. The path uses the @@ -66,9 +110,25 @@ public interface LocalRepositoryManager { * * @param metadata The metadata for which to determine the path, must not be {@code null}. * @return The path, relative to the local repository's base directory. + * @deprecated See {@link #getAbsolutePathForLocalMetadata(Metadata)} */ + @Deprecated String getPathForLocalMetadata(Metadata metadata); + /** + * Gets the absolute path for metadata cached from a remote repository. Note that the metadata need not actually + * exist yet at the returned location, the path merely indicates where the metadata would eventually be stored. + * + * @param metadata The metadata for which to determine the path, must not be {@code null}. + * @param repository The source repository of the metadata, must not be {@code null}. + * @param context The resolution context in which the metadata is being requested, may be {@code null}. + * @return The path, relative to the local repository's base directory. + * @since 2.0.5 + */ + default Path getAbsolutePathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) { + return getRepository().getBasePath().resolve(getPathForRemoteMetadata(metadata, repository, context)); + } + /** * Gets the relative path for metadata cached from a remote repository. Note that the metadata need not actually * exist yet at the returned location, the path merely indicates where the metadata would eventually be stored. The @@ -78,7 +138,9 @@ public interface LocalRepositoryManager { * @param repository The source repository of the metadata, must not be {@code null}. * @param context The resolution context in which the metadata is being requested, may be {@code null}. * @return The path, relative to the local repository's base directory. + * @deprecated See {@link #getAbsolutePathForRemoteMetadata(Metadata, RemoteRepository, String)} */ + @Deprecated String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context); /** diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java index f4312691b..6c614d923 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java @@ -519,9 +519,8 @@ private List gatherDownloads(RepositorySystemSession session, download.setPath(item.local.getPath()); download.setExistenceCheck(true); } else { - String path = - lrm.getPathForRemoteArtifact(artifact, group.repository, item.request.getRequestContext()); - download.setPath(lrm.getRepository().getBasePath().resolve(path)); + download.setPath(lrm.getAbsolutePathForRemoteArtifact( + artifact, group.repository, item.request.getRequestContext())); } boolean snapshot = artifact.isSnapshot(); diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java index 873d121d2..361e809c1 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java @@ -285,9 +285,7 @@ private void upload( EventCatapult catapult) throws DeploymentException { LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - Path basePath = lrm.getRepository().getBasePath(); - - Path dstPath = basePath.resolve(lrm.getPathForRemoteMetadata(metadata, repository, "")); + Path dstPath = lrm.getAbsolutePathForRemoteMetadata(metadata, repository, ""); if (metadata instanceof MergeableMetadata) { if (!((MergeableMetadata) metadata).isMerged()) { diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java index fba229286..5533c480a 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java @@ -195,7 +195,7 @@ private void install(RepositorySystemSession session, RequestTrace trace, Artifa throws InstallationException { final LocalRepositoryManager lrm = session.getLocalRepositoryManager(); final Path srcPath = artifact.getPath(); - final Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalArtifact(artifact)); + final Path dstPath = lrm.getAbsolutePathForLocalArtifact(artifact); artifactInstalling(session, trace, artifact, dstPath); @@ -219,7 +219,7 @@ private void install(RepositorySystemSession session, RequestTrace trace, Metada throws InstallationException { LocalRepositoryManager lrm = session.getLocalRepositoryManager(); - Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalMetadata(metadata)); + Path dstPath = lrm.getAbsolutePathForLocalMetadata(metadata); metadataInstalling(session, trace, metadata, dstPath); diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java index 28a43ae17..305655b21 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java @@ -257,10 +257,8 @@ private List resolve( check.setItem(metadata); // use 'main' installation file for the check (-> use requested repository) - Path checkPath = session.getLocalRepository() - .getBasePath() - .resolve(session.getLocalRepositoryManager() - .getPathForRemoteMetadata(metadata, repository, request.getRequestContext())); + Path checkPath = session.getLocalRepositoryManager() + .getAbsolutePathForRemoteMetadata(metadata, repository, request.getRequestContext()); check.setPath(checkPath); check.setRepository(repository); check.setAuthoritativeRepository(repo); @@ -283,11 +281,9 @@ private List resolve( RepositoryPolicy policy = getPolicy(session, repository, metadata.getNature()); // install path may be different from lookup path - Path installPath = session.getLocalRepository() - .getBasePath() - .resolve(session.getLocalRepositoryManager() - .getPathForRemoteMetadata( - metadata, request.getRepository(), request.getRequestContext())); + Path installPath = session.getLocalRepositoryManager() + .getAbsolutePathForRemoteMetadata( + metadata, request.getRepository(), request.getRequestContext()); ResolveTask task = new ResolveTask( session, trace, result, installPath, checks, policy.getChecksumPolicy()); 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 a414c8588..4fd5ec29c 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 @@ -119,20 +119,17 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe Artifact artifact = request.getArtifact(); LocalArtifactResult result = new LocalArtifactResult(request); - String path; Path filePath; // Local repository CANNOT have timestamped installed, they are created only during deploy if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) { - path = getPathForLocalArtifact(artifact); - filePath = getRepository().getBasePath().resolve(path); + filePath = getAbsolutePathForLocalArtifact(artifact); checkFind(filePath, result); } if (!result.isAvailable()) { for (RemoteRepository repository : request.getRepositories()) { - path = getPathForRemoteArtifact(artifact, repository, request.getContext()); - filePath = getRepository().getBasePath().resolve(path); + filePath = getAbsolutePathForRemoteArtifact(artifact, repository, request.getContext()); checkFind(filePath, result); @@ -208,10 +205,9 @@ private Collection getRepositoryKeys(RemoteRepository repository, Collec private void addArtifact( Artifact artifact, Collection repositories, RemoteRepository repository, String context) { requireNonNull(artifact, "artifact cannot be null"); - String path = repository == null - ? getPathForLocalArtifact(artifact) - : getPathForRemoteArtifact(artifact, repository, context); - Path file = getRepository().getBasePath().resolve(path); + Path file = repository == null + ? getAbsolutePathForLocalArtifact(artifact) + : getAbsolutePathForRemoteArtifact(artifact, repository, context); addRepo(file, repositories); } diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java index ee393140d..6c5e2a884 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java @@ -132,13 +132,11 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe Artifact artifact = request.getArtifact(); LocalArtifactResult result = new LocalArtifactResult(request); - String path; Path filePath; // Local repository CANNOT have timestamped installed, they are created only during deploy if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) { - path = getPathForLocalArtifact(artifact); - filePath = getRepository().getBasePath().resolve(path); + filePath = getAbsolutePathForLocalArtifact(artifact); if (Files.isRegularFile(filePath)) { result.setPath(filePath); result.setAvailable(true); @@ -147,8 +145,7 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe if (!result.isAvailable()) { for (RemoteRepository repository : request.getRepositories()) { - path = getPathForRemoteArtifact(artifact, repository, request.getContext()); - filePath = getRepository().getBasePath().resolve(path); + filePath = getAbsolutePathForRemoteArtifact(artifact, repository, request.getContext()); if (Files.isRegularFile(filePath)) { result.setPath(filePath); result.setAvailable(true); diff --git a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java index 6b32ab2f4..1223c4571 100644 --- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java +++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java @@ -70,19 +70,52 @@ public final class ChainedLocalRepositoryManager implements LocalRepositoryManag private final boolean ignoreTailAvailability; + private final int installTarget; + + private final int cacheTarget; + public ChainedLocalRepositoryManager( LocalRepositoryManager head, List tail, boolean ignoreTailAvailability) { - this.head = requireNonNull(head, "head cannot be null"); - this.tail = requireNonNull(tail, "tail cannot be null"); - this.ignoreTailAvailability = ignoreTailAvailability; + this(head, tail, ignoreTailAvailability, 0, 0); } public ChainedLocalRepositoryManager( LocalRepositoryManager head, List tail, RepositorySystemSession session) { + this( + head, + tail, + ConfigUtils.getBoolean(session, DEFAULT_IGNORE_TAIL_AVAILABILITY, CONFIG_PROP_IGNORE_TAIL_AVAILABILITY), + 0, + 0); + } + + /** + * Warning: this is experimental feature of chained, is not recommended to be used/integrated into plain Maven. + * + * @param head The head LRM + * @param tail The tail LRMs + * @param ignoreTailAvailability Whether tail availability should be ignored (usually you do want this) + * @param installTarget The installation LRM index, integer from 0 to size of tail. + * @param cacheTarget The cache LRM index, integer from 0 to size of tail. + * @since 2.0.5 + */ + public ChainedLocalRepositoryManager( + LocalRepositoryManager head, + List tail, + boolean ignoreTailAvailability, + int installTarget, + int cacheTarget) { this.head = requireNonNull(head, "head cannot be null"); this.tail = requireNonNull(tail, "tail cannot be null"); - this.ignoreTailAvailability = - ConfigUtils.getBoolean(session, DEFAULT_IGNORE_TAIL_AVAILABILITY, CONFIG_PROP_IGNORE_TAIL_AVAILABILITY); + this.ignoreTailAvailability = ignoreTailAvailability; + if (installTarget < 0 || installTarget > tail.size()) { + throw new IllegalArgumentException("Illegal installTarget value"); + } + this.installTarget = installTarget; + if (cacheTarget < 0 || cacheTarget > tail.size()) { + throw new IllegalArgumentException("Illegal cacheTarget value"); + } + this.cacheTarget = cacheTarget; } @Override @@ -90,24 +123,60 @@ public LocalRepository getRepository() { return head.getRepository(); } + private LocalRepositoryManager getInstallTarget() { + if (installTarget == 0) { + return head; + } else { + return tail.get(installTarget - 1); + } + } + + private LocalRepositoryManager getCacheTarget() { + if (cacheTarget == 0) { + return head; + } else { + return tail.get(cacheTarget - 1); + } + } + + @Override + public Path getAbsolutePathForLocalArtifact(Artifact artifact) { + return getInstallTarget().getAbsolutePathForLocalArtifact(artifact); + } + + @Override + public Path getAbsolutePathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) { + return getCacheTarget().getAbsolutePathForRemoteArtifact(artifact, repository, context); + } + + @Override + public Path getAbsolutePathForLocalMetadata(Metadata metadata) { + return getInstallTarget().getAbsolutePathForLocalMetadata(metadata); + } + + @Override + public Path getAbsolutePathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) { + return getCacheTarget().getAbsolutePathForRemoteMetadata(metadata, repository, context); + } + @Override public String getPathForLocalArtifact(Artifact artifact) { - return head.getPathForLocalArtifact(artifact); + return getInstallTarget().getPathForLocalArtifact(artifact); } @Override public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) { - return head.getPathForRemoteArtifact(artifact, repository, context); + return getCacheTarget().getPathForRemoteArtifact(artifact, repository, context); } @Override public String getPathForLocalMetadata(Metadata metadata) { - return head.getPathForLocalMetadata(metadata); + return getInstallTarget().getPathForLocalMetadata(metadata); } @Override public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) { - return head.getPathForRemoteMetadata(metadata, repository, context); + return getCacheTarget().getPathForRemoteMetadata(metadata, repository, context); } @Override @@ -134,15 +203,17 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe @Override public void add(RepositorySystemSession session, LocalArtifactRegistration request) { String artifactPath; + LocalRepositoryManager target; if (request.getRepository() != null) { artifactPath = getPathForRemoteArtifact(request.getArtifact(), request.getRepository(), "check"); + target = getCacheTarget(); } else { artifactPath = getPathForLocalArtifact(request.getArtifact()); + target = getInstallTarget(); } - - Path file = head.getRepository().getBasePath().resolve(artifactPath); + Path file = target.getRepository().getBasePath().resolve(artifactPath); if (Files.isRegularFile(file)) { - head.add(session, request); + target.add(session, request); } } @@ -165,15 +236,18 @@ public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRe @Override public void add(RepositorySystemSession session, LocalMetadataRegistration request) { String metadataPath; + LocalRepositoryManager target; if (request.getRepository() != null) { metadataPath = getPathForRemoteMetadata(request.getMetadata(), request.getRepository(), "check"); + target = getCacheTarget(); } else { metadataPath = getPathForLocalMetadata(request.getMetadata()); + target = getInstallTarget(); } - Path file = head.getRepository().getBasePath().resolve(metadataPath); + Path file = target.getRepository().getBasePath().resolve(metadataPath); if (Files.isRegularFile(file)) { - head.add(session, request); + target.add(session, request); } }