Skip to content

[MRESOLVER-645] Remove repeated LRM interaction #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,16 +39,44 @@ 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
* forward slash as directory separator regardless of the underlying file system.
*
* @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
Expand All @@ -56,19 +86,49 @@ 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
* forward slash as directory separator regardless of the underlying file system.
*
* @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
Expand All @@ -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);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,8 @@ private List<ArtifactDownload> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ private List<MetadataResult> 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);
Expand All @@ -283,11 +281,9 @@ private List<MetadataResult> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -208,10 +205,9 @@ private Collection<String> getRepositoryKeys(RemoteRepository repository, Collec
private void addArtifact(
Artifact artifact, Collection<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,44 +70,93 @@ public final class ChainedLocalRepositoryManager implements LocalRepositoryManag

private final boolean ignoreTailAvailability;

private final int installTarget;

private final int cacheTarget;

public ChainedLocalRepositoryManager(
LocalRepositoryManager head, List<LocalRepositoryManager> 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<LocalRepositoryManager> 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<LocalRepositoryManager> 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
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 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
Expand All @@ -134,15 +183,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);
}
}

Expand All @@ -165,15 +216,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);
}
}

Expand Down
Loading