1   package org.apache.maven.project;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.FileNotFoundException;
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.sonatype.aether.RepositorySystemSession;
30  import org.sonatype.aether.artifact.Artifact;
31  import org.sonatype.aether.impl.ArtifactResolver;
32  import org.sonatype.aether.resolution.ArtifactRequest;
33  import org.sonatype.aether.resolution.ArtifactResolutionException;
34  import org.sonatype.aether.resolution.ArtifactResult;
35  import org.sonatype.aether.transfer.ArtifactNotFoundException;
36  
37  /**
38   * @author Benjamin Bentmann
39   */
40  @Component( role = ArtifactResolver.class, hint = "classpath" )
41  public class ClasspathArtifactResolver
42      implements ArtifactResolver
43  {
44  
45      public List<ArtifactResult> resolveArtifacts( RepositorySystemSession session,
46                                                    Collection<? extends ArtifactRequest> requests )
47          throws ArtifactResolutionException
48      {
49          List<ArtifactResult> results = new ArrayList<ArtifactResult>();
50  
51          for ( ArtifactRequest request : requests )
52          {
53              ArtifactResult result = new ArtifactResult( request );
54              results.add( result );
55  
56              Artifact artifact = request.getArtifact();
57              if ( "maven-test".equals( artifact.getGroupId() ) )
58              {
59                  String scope = artifact.getArtifactId().substring( "scope-".length() );
60  
61                  try
62                  {
63                      artifact =
64                          artifact.setFile( ProjectClasspathTest.getFileForClasspathResource( ProjectClasspathTest.dir
65                              + "transitive-" + scope + "-dep.xml" ) );
66                      result.setArtifact( artifact );
67                  }
68                  catch ( FileNotFoundException e )
69                  {
70                      throw new IllegalStateException( "Missing test POM for " + artifact );
71                  }
72              }
73              else
74              {
75                  result.addException( new ArtifactNotFoundException( artifact, null ) );
76                  throw new ArtifactResolutionException( results );
77              }
78          }
79  
80          return results;
81      }
82  
83      public ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request )
84          throws ArtifactResolutionException
85      {
86          return resolveArtifacts( session, Collections.singleton( request ) ).get( 0 );
87      }
88  
89  }