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 javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.nio.file.Path;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.stream.Collectors;
30
31 import org.apache.maven.api.Artifact;
32 import org.apache.maven.api.annotations.Nonnull;
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.RepositorySystem;
39 import org.eclipse.aether.repository.RemoteRepository;
40 import org.eclipse.aether.resolution.ArtifactRequest;
41 import org.eclipse.aether.resolution.ArtifactResolutionException;
42 import org.eclipse.aether.resolution.ArtifactResult;
43
44 import static org.apache.maven.internal.impl.Utils.cast;
45 import static org.apache.maven.internal.impl.Utils.nonNull;
46
47 @Named
48 @Singleton
49 public class DefaultArtifactResolver implements ArtifactResolver {
50 private final RepositorySystem repositorySystem;
51
52 @Inject
53 DefaultArtifactResolver(@Nonnull RepositorySystem repositorySystem) {
54 this.repositorySystem = nonNull(repositorySystem, "repositorySystem can not be null");
55 }
56
57 @Override
58 public ArtifactResolverResult resolve(ArtifactResolverRequest request)
59 throws ArtifactResolverException, IllegalArgumentException {
60 nonNull(request, "request can not be null");
61 DefaultSession session =
62 cast(DefaultSession.class, request.getSession(), "request.session should be a " + DefaultSession.class);
63 try {
64 List<RemoteRepository> repositories = session.toRepositories(session.getRemoteRepositories());
65 List<ArtifactRequest> requests = request.getCoordinates().stream()
66 .map(coord -> new ArtifactRequest(session.toArtifact(coord), repositories, null))
67 .collect(Collectors.toList());
68 List<ArtifactResult> results = repositorySystem.resolveArtifacts(session.getSession(), requests);
69 Map<Artifact, Path> paths = new HashMap<>();
70 for (ArtifactResult result : results) {
71 Artifact artifact = session.getArtifact(result.getArtifact());
72 Path path = result.getArtifact().getFile().toPath();
73 session.getService(ArtifactManager.class).setPath(artifact, path);
74 paths.put(artifact, path);
75 }
76 return new ArtifactResolverResult() {
77 @Override
78 public Map<Artifact, Path> getArtifacts() {
79 return paths;
80 }
81 };
82 } catch (ArtifactResolutionException e) {
83 throw new ArtifactResolverException("Unable to resolve artifact", e);
84 }
85 }
86 }