View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }