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 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  }