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