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 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(
58                      request.getRepositories() != null ? request.getRepositories() : session.getRemoteRepositories());
59              List<ArtifactRequest> requests = new ArrayList<>();
60              for (ArtifactCoordinates coords : request.getCoordinates()) {
61                  org.eclipse.aether.artifact.Artifact aetherArtifact = session.toArtifact(coords);
62                  Artifact artifact = session.getArtifact(aetherArtifact);
63                  Path path = artifactManager.getPath(artifact).orElse(null);
64                  if (path != null) {
65                      if (aetherArtifact.getPath() == null) {
66                          aetherArtifact = aetherArtifact.setPath(path);
67                      }
68                      DownloadedArtifact resolved = session.getArtifact(DownloadedArtifact.class, aetherArtifact);
69                      paths.put(resolved, path);
70                  } else {
71                      requests.add(new ArtifactRequest(aetherArtifact, repositories, null));
72                  }
73              }
74              if (!requests.isEmpty()) {
75                  List<ArtifactResult> results =
76                          session.getRepositorySystem().resolveArtifacts(session.getSession(), requests);
77                  for (ArtifactResult result : results) {
78                      DownloadedArtifact artifact = session.getArtifact(DownloadedArtifact.class, result.getArtifact());
79                      Path path = result.getArtifact().getPath();
80                      paths.put(artifact, path);
81                  }
82              }
83              return new DefaultArtifactResolverResult(paths);
84          } catch (ArtifactResolutionException e) {
85              throw new ArtifactResolverException("Unable to resolve artifact: " + e.getMessage(), e);
86          }
87      }
88  
89      static class DefaultArtifactResolverResult implements ArtifactResolverResult {
90          final Map<DownloadedArtifact, Path> paths;
91  
92          DefaultArtifactResolverResult(Map<DownloadedArtifact, Path> paths) {
93              this.paths = paths;
94          }
95  
96          @Override
97          public Collection<DownloadedArtifact> getArtifacts() {
98              return paths.keySet();
99          }
100 
101         @Override
102         public Path getPath(Artifact artifact) {
103             return paths.get(artifact);
104         }
105     }
106 }