Skip to content

Commit 77e9b91

Browse files
authored
[MRESOLVER-645] Remove repeated LRM interaction (#611)
Pull in repeated snippets under LRM. --- https://issues.apache.org/jira/browse/MRESOLVER-645
1 parent 6f70e52 commit 77e9b91

File tree

8 files changed

+167
-45
lines changed

8 files changed

+167
-45
lines changed

maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java

+62
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919
package org.eclipse.aether.repository;
2020

21+
import java.nio.file.Path;
22+
2123
import org.eclipse.aether.RepositorySystemSession;
2224
import org.eclipse.aether.artifact.Artifact;
2325
import org.eclipse.aether.metadata.Metadata;
@@ -37,16 +39,44 @@ public interface LocalRepositoryManager {
3739
*/
3840
LocalRepository getRepository();
3941

42+
/**
43+
* Gets the absolute path for a locally installed artifact. Note that the artifact need not actually exist yet at
44+
* the returned location, the path merely indicates where the artifact would eventually be stored.
45+
*
46+
* @param artifact The artifact for which to determine the path, must not be {@code null}.
47+
* @return The path, relative to the local repository's base directory.
48+
* @since 2.0.5
49+
*/
50+
default Path getAbsolutePathForLocalArtifact(Artifact artifact) {
51+
return getRepository().getBasePath().resolve(getPathForLocalArtifact(artifact));
52+
}
53+
4054
/**
4155
* Gets the relative path for a locally installed artifact. Note that the artifact need not actually exist yet at
4256
* the returned location, the path merely indicates where the artifact would eventually be stored. The path uses the
4357
* forward slash as directory separator regardless of the underlying file system.
4458
*
4559
* @param artifact The artifact for which to determine the path, must not be {@code null}.
4660
* @return The path, relative to the local repository's base directory.
61+
* @deprecated See {@link #getAbsolutePathForLocalArtifact(Artifact)}
4762
*/
63+
@Deprecated
4864
String getPathForLocalArtifact(Artifact artifact);
4965

66+
/**
67+
* Gets the absolute path for an artifact cached from a remote repository. Note that the artifact need not actually
68+
* exist yet at the returned location, the path merely indicates where the artifact would eventually be stored.
69+
*
70+
* @param artifact The artifact for which to determine the path, must not be {@code null}.
71+
* @param repository The source repository of the artifact, must not be {@code null}.
72+
* @param context The resolution context in which the artifact is being requested, may be {@code null}.
73+
* @return The path, relative to the local repository's base directory.
74+
* @since 2.0.5
75+
*/
76+
default Path getAbsolutePathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
77+
return getRepository().getBasePath().resolve(getPathForRemoteArtifact(artifact, repository, context));
78+
}
79+
5080
/**
5181
* Gets the relative path for an artifact cached from a remote repository. Note that the artifact need not actually
5282
* exist yet at the returned location, the path merely indicates where the artifact would eventually be stored. The
@@ -56,19 +86,49 @@ public interface LocalRepositoryManager {
5686
* @param repository The source repository of the artifact, must not be {@code null}.
5787
* @param context The resolution context in which the artifact is being requested, may be {@code null}.
5888
* @return The path, relative to the local repository's base directory.
89+
* @deprecated See {@link #getAbsolutePathForRemoteArtifact(Artifact, RemoteRepository, String)}
5990
*/
91+
@Deprecated
6092
String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context);
6193

94+
/**
95+
* Gets the absolute path for locally installed metadata. Note that the metadata need not actually exist yet at the
96+
* returned location, the path merely indicates where the metadata would eventually be stored.
97+
*
98+
* @param metadata The metadata for which to determine the path, must not be {@code null}.
99+
* @return The path, relative to the local repository's base directory.
100+
* @since 2.0.5
101+
*/
102+
default Path getAbsolutePathForLocalMetadata(Metadata metadata) {
103+
return getRepository().getBasePath().resolve(getPathForLocalMetadata(metadata));
104+
}
105+
62106
/**
63107
* Gets the relative path for locally installed metadata. Note that the metadata need not actually exist yet at the
64108
* returned location, the path merely indicates where the metadata would eventually be stored. The path uses the
65109
* forward slash as directory separator regardless of the underlying file system.
66110
*
67111
* @param metadata The metadata for which to determine the path, must not be {@code null}.
68112
* @return The path, relative to the local repository's base directory.
113+
* @deprecated See {@link #getAbsolutePathForLocalMetadata(Metadata)}
69114
*/
115+
@Deprecated
70116
String getPathForLocalMetadata(Metadata metadata);
71117

118+
/**
119+
* Gets the absolute path for metadata cached from a remote repository. Note that the metadata need not actually
120+
* exist yet at the returned location, the path merely indicates where the metadata would eventually be stored.
121+
*
122+
* @param metadata The metadata for which to determine the path, must not be {@code null}.
123+
* @param repository The source repository of the metadata, must not be {@code null}.
124+
* @param context The resolution context in which the metadata is being requested, may be {@code null}.
125+
* @return The path, relative to the local repository's base directory.
126+
* @since 2.0.5
127+
*/
128+
default Path getAbsolutePathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
129+
return getRepository().getBasePath().resolve(getPathForRemoteMetadata(metadata, repository, context));
130+
}
131+
72132
/**
73133
* Gets the relative path for metadata cached from a remote repository. Note that the metadata need not actually
74134
* 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 {
78138
* @param repository The source repository of the metadata, must not be {@code null}.
79139
* @param context The resolution context in which the metadata is being requested, may be {@code null}.
80140
* @return The path, relative to the local repository's base directory.
141+
* @deprecated See {@link #getAbsolutePathForRemoteMetadata(Metadata, RemoteRepository, String)}
81142
*/
143+
@Deprecated
82144
String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context);
83145

84146
/**

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,8 @@ private List<ArtifactDownload> gatherDownloads(RepositorySystemSession session,
519519
download.setPath(item.local.getPath());
520520
download.setExistenceCheck(true);
521521
} else {
522-
String path =
523-
lrm.getPathForRemoteArtifact(artifact, group.repository, item.request.getRequestContext());
524-
download.setPath(lrm.getRepository().getBasePath().resolve(path));
522+
download.setPath(lrm.getAbsolutePathForRemoteArtifact(
523+
artifact, group.repository, item.request.getRequestContext()));
525524
}
526525

527526
boolean snapshot = artifact.isSnapshot();

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,7 @@ private void upload(
285285
EventCatapult catapult)
286286
throws DeploymentException {
287287
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
288-
Path basePath = lrm.getRepository().getBasePath();
289-
290-
Path dstPath = basePath.resolve(lrm.getPathForRemoteMetadata(metadata, repository, ""));
288+
Path dstPath = lrm.getAbsolutePathForRemoteMetadata(metadata, repository, "");
291289

292290
if (metadata instanceof MergeableMetadata) {
293291
if (!((MergeableMetadata) metadata).isMerged()) {

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private void install(RepositorySystemSession session, RequestTrace trace, Artifa
195195
throws InstallationException {
196196
final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
197197
final Path srcPath = artifact.getPath();
198-
final Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalArtifact(artifact));
198+
final Path dstPath = lrm.getAbsolutePathForLocalArtifact(artifact);
199199

200200
artifactInstalling(session, trace, artifact, dstPath);
201201

@@ -219,7 +219,7 @@ private void install(RepositorySystemSession session, RequestTrace trace, Metada
219219
throws InstallationException {
220220
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
221221

222-
Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalMetadata(metadata));
222+
Path dstPath = lrm.getAbsolutePathForLocalMetadata(metadata);
223223

224224
metadataInstalling(session, trace, metadata, dstPath);
225225

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,8 @@ private List<MetadataResult> resolve(
257257
check.setItem(metadata);
258258

259259
// use 'main' installation file for the check (-> use requested repository)
260-
Path checkPath = session.getLocalRepository()
261-
.getBasePath()
262-
.resolve(session.getLocalRepositoryManager()
263-
.getPathForRemoteMetadata(metadata, repository, request.getRequestContext()));
260+
Path checkPath = session.getLocalRepositoryManager()
261+
.getAbsolutePathForRemoteMetadata(metadata, repository, request.getRequestContext());
264262
check.setPath(checkPath);
265263
check.setRepository(repository);
266264
check.setAuthoritativeRepository(repo);
@@ -283,11 +281,9 @@ private List<MetadataResult> resolve(
283281
RepositoryPolicy policy = getPolicy(session, repository, metadata.getNature());
284282

285283
// install path may be different from lookup path
286-
Path installPath = session.getLocalRepository()
287-
.getBasePath()
288-
.resolve(session.getLocalRepositoryManager()
289-
.getPathForRemoteMetadata(
290-
metadata, request.getRepository(), request.getRequestContext()));
284+
Path installPath = session.getLocalRepositoryManager()
285+
.getAbsolutePathForRemoteMetadata(
286+
metadata, request.getRepository(), request.getRequestContext());
291287

292288
ResolveTask task = new ResolveTask(
293289
session, trace, result, installPath, checks, policy.getChecksumPolicy());

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,17 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe
119119
Artifact artifact = request.getArtifact();
120120
LocalArtifactResult result = new LocalArtifactResult(request);
121121

122-
String path;
123122
Path filePath;
124123

125124
// Local repository CANNOT have timestamped installed, they are created only during deploy
126125
if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) {
127-
path = getPathForLocalArtifact(artifact);
128-
filePath = getRepository().getBasePath().resolve(path);
126+
filePath = getAbsolutePathForLocalArtifact(artifact);
129127
checkFind(filePath, result);
130128
}
131129

132130
if (!result.isAvailable()) {
133131
for (RemoteRepository repository : request.getRepositories()) {
134-
path = getPathForRemoteArtifact(artifact, repository, request.getContext());
135-
filePath = getRepository().getBasePath().resolve(path);
132+
filePath = getAbsolutePathForRemoteArtifact(artifact, repository, request.getContext());
136133

137134
checkFind(filePath, result);
138135

@@ -208,10 +205,9 @@ private Collection<String> getRepositoryKeys(RemoteRepository repository, Collec
208205
private void addArtifact(
209206
Artifact artifact, Collection<String> repositories, RemoteRepository repository, String context) {
210207
requireNonNull(artifact, "artifact cannot be null");
211-
String path = repository == null
212-
? getPathForLocalArtifact(artifact)
213-
: getPathForRemoteArtifact(artifact, repository, context);
214-
Path file = getRepository().getBasePath().resolve(path);
208+
Path file = repository == null
209+
? getAbsolutePathForLocalArtifact(artifact)
210+
: getAbsolutePathForRemoteArtifact(artifact, repository, context);
215211
addRepo(file, repositories);
216212
}
217213

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe
132132
Artifact artifact = request.getArtifact();
133133
LocalArtifactResult result = new LocalArtifactResult(request);
134134

135-
String path;
136135
Path filePath;
137136

138137
// Local repository CANNOT have timestamped installed, they are created only during deploy
139138
if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) {
140-
path = getPathForLocalArtifact(artifact);
141-
filePath = getRepository().getBasePath().resolve(path);
139+
filePath = getAbsolutePathForLocalArtifact(artifact);
142140
if (Files.isRegularFile(filePath)) {
143141
result.setPath(filePath);
144142
result.setAvailable(true);
@@ -147,8 +145,7 @@ public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRe
147145

148146
if (!result.isAvailable()) {
149147
for (RemoteRepository repository : request.getRepositories()) {
150-
path = getPathForRemoteArtifact(artifact, repository, request.getContext());
151-
filePath = getRepository().getBasePath().resolve(path);
148+
filePath = getAbsolutePathForRemoteArtifact(artifact, repository, request.getContext());
152149
if (Files.isRegularFile(filePath)) {
153150
result.setPath(filePath);
154151
result.setAvailable(true);

0 commit comments

Comments
 (0)