1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.internal.impl;
20
21 import java.nio.file.Path;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.maven.api.Artifact;
29 import org.apache.maven.api.ArtifactCoordinates;
30 import org.apache.maven.api.DownloadedArtifact;
31 import org.apache.maven.api.di.Named;
32 import org.apache.maven.api.di.Singleton;
33 import org.apache.maven.api.services.ArtifactManager;
34 import org.apache.maven.api.services.ArtifactResolver;
35 import org.apache.maven.api.services.ArtifactResolverException;
36 import org.apache.maven.api.services.ArtifactResolverRequest;
37 import org.apache.maven.api.services.ArtifactResolverResult;
38 import org.eclipse.aether.repository.RemoteRepository;
39 import org.eclipse.aether.resolution.ArtifactRequest;
40 import org.eclipse.aether.resolution.ArtifactResolutionException;
41 import org.eclipse.aether.resolution.ArtifactResult;
42
43 import static org.apache.maven.internal.impl.Utils.nonNull;
44
45 @Named
46 @Singleton
47 public class DefaultArtifactResolver implements ArtifactResolver {
48
49 @Override
50 public ArtifactResolverResult resolve(ArtifactResolverRequest request)
51 throws ArtifactResolverException, IllegalArgumentException {
52 nonNull(request, "request");
53 InternalSession session = InternalSession.from(request.getSession());
54 try {
55 Map<DownloadedArtifact, Path> paths = new HashMap<>();
56 ArtifactManager artifactManager = session.getService(ArtifactManager.class);
57 List<RemoteRepository> repositories = session.toRepositories(session.getRemoteRepositories());
58 List<ArtifactRequest> requests = new ArrayList<>();
59 for (ArtifactCoordinates coords : request.getCoordinates()) {
60 org.eclipse.aether.artifact.Artifact aetherArtifact = session.toArtifact(coords);
61 Artifact artifact = session.getArtifact(aetherArtifact);
62 Path path = artifactManager.getPath(artifact).orElse(null);
63 if (path != null) {
64 if (aetherArtifact.getPath() == null) {
65 aetherArtifact = aetherArtifact.setPath(path);
66 }
67 DownloadedArtifact resolved = session.getArtifact(DownloadedArtifact.class, aetherArtifact);
68 paths.put(resolved, path);
69 } else {
70 requests.add(new ArtifactRequest(aetherArtifact, repositories, null));
71 }
72 }
73 if (!requests.isEmpty()) {
74 List<ArtifactResult> results =
75 session.getRepositorySystem().resolveArtifacts(session.getSession(), requests);
76 for (ArtifactResult result : results) {
77 DownloadedArtifact artifact = session.getArtifact(DownloadedArtifact.class, result.getArtifact());
78 Path path = result.getArtifact().getPath();
79 paths.put(artifact, path);
80 }
81 }
82 return new DefaultArtifactResolverResult(paths);
83 } catch (ArtifactResolutionException e) {
84 throw new ArtifactResolverException("Unable to resolve artifact: " + e.getMessage(), e);
85 }
86 }
87
88 static class DefaultArtifactResolverResult implements ArtifactResolverResult {
89 final Map<DownloadedArtifact, Path> paths;
90
91 DefaultArtifactResolverResult(Map<DownloadedArtifact, Path> paths) {
92 this.paths = paths;
93 }
94
95 @Override
96 public Collection<DownloadedArtifact> getArtifacts() {
97 return paths.keySet();
98 }
99
100 @Override
101 public Path getPath(Artifact artifact) {
102 return paths.get(artifact);
103 }
104 }
105 }