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.project;
20  
21  import java.io.FileNotFoundException;
22  import java.net.URISyntaxException;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.codehaus.plexus.component.annotations.Component;
29  import org.eclipse.aether.RepositorySystemSession;
30  import org.eclipse.aether.artifact.Artifact;
31  import org.eclipse.aether.impl.ArtifactResolver;
32  import org.eclipse.aether.resolution.ArtifactRequest;
33  import org.eclipse.aether.resolution.ArtifactResolutionException;
34  import org.eclipse.aether.resolution.ArtifactResult;
35  import org.eclipse.aether.transfer.ArtifactNotFoundException;
36  
37  /**
38   * @author Benjamin Bentmann
39   */
40  @Component(role = ArtifactResolver.class, hint = "classpath")
41  public class ClasspathArtifactResolver implements ArtifactResolver {
42  
43      public List<ArtifactResult> resolveArtifacts(
44              RepositorySystemSession session, Collection<? extends ArtifactRequest> requests)
45              throws ArtifactResolutionException {
46          List<ArtifactResult> results = new ArrayList<>();
47  
48          for (ArtifactRequest request : requests) {
49              ArtifactResult result = new ArtifactResult(request);
50              results.add(result);
51  
52              Artifact artifact = request.getArtifact();
53              if ("maven-test".equals(artifact.getGroupId())) {
54                  String scope = artifact.getArtifactId().substring("scope-".length());
55  
56                  try {
57                      artifact = artifact.setFile(ProjectClasspathTest.getFileForClasspathResource(
58                              ProjectClasspathTest.dir + "transitive-" + scope + "-dep.xml"));
59                      result.setArtifact(artifact);
60                  } catch (FileNotFoundException | URISyntaxException e) {
61                      throw new IllegalStateException("Missing test POM for " + artifact, e);
62                  }
63              } else {
64                  result.addException(new ArtifactNotFoundException(artifact, null));
65                  throw new ArtifactResolutionException(results);
66              }
67          }
68  
69          return results;
70      }
71  
72      public ArtifactResult resolveArtifact(RepositorySystemSession session, ArtifactRequest request)
73              throws ArtifactResolutionException {
74          return resolveArtifacts(session, Collections.singleton(request)).get(0);
75      }
76  }