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.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.maven.api.Artifact;
32  import org.apache.maven.api.ArtifactCoordinate;
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  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");
55      }
56  
57      @Override
58      public ArtifactResolverResult resolve(ArtifactResolverRequest request)
59              throws ArtifactResolverException, IllegalArgumentException {
60          nonNull(request, "request");
61          InternalSession session = InternalSession.from(request.getSession());
62          try {
63              Map<Artifact, Path> paths = new HashMap<>();
64              ArtifactManager artifactManager = session.getService(ArtifactManager.class);
65              List<RemoteRepository> repositories = session.toRepositories(session.getRemoteRepositories());
66              List<ArtifactRequest> requests = new ArrayList<>();
67              for (ArtifactCoordinate coord : request.getCoordinates()) {
68                  org.eclipse.aether.artifact.Artifact aetherArtifact = session.toArtifact(coord);
69                  Artifact artifact = session.getArtifact(aetherArtifact);
70                  Path path = artifactManager.getPath(artifact).orElse(null);
71                  if (path != null) {
72                      paths.put(artifact, path);
73                  } else {
74                      requests.add(new ArtifactRequest(aetherArtifact, repositories, null));
75                  }
76              }
77              if (!requests.isEmpty()) {
78                  List<ArtifactResult> results = repositorySystem.resolveArtifacts(session.getSession(), requests);
79                  for (ArtifactResult result : results) {
80                      Artifact artifact = session.getArtifact(result.getArtifact());
81                      Path path = result.getArtifact().getFile().toPath();
82                      artifactManager.setPath(artifact, path);
83                      paths.put(artifact, path);
84                  }
85              }
86              return () -> paths;
87          } catch (ArtifactResolutionException e) {
88              throw new ArtifactResolverException("Unable to resolve artifact", e);
89          }
90      }
91  }