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