View Javadoc
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.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.impl.ArtifactResolver;
31  import org.eclipse.aether.resolution.ArtifactRequest;
32  import org.eclipse.aether.resolution.ArtifactResolutionException;
33  import org.eclipse.aether.resolution.ArtifactResult;
34  import org.eclipse.aether.transfer.ArtifactNotFoundException;
35  
36  import javax.inject.Named;
37  import javax.inject.Singleton;
38  
39  /**
40   * @author Benjamin Bentmann
41   */
42  @Named( "classpath" )
43  @Singleton
44  public class ClasspathArtifactResolver
45      implements ArtifactResolver
46  {
47  
48      public List<ArtifactResult> resolveArtifacts( RepositorySystemSession session,
49                                                    Collection<? extends ArtifactRequest> requests )
50          throws ArtifactResolutionException
51      {
52          List<ArtifactResult> results = new ArrayList<>();
53  
54          for ( ArtifactRequest request : requests )
55          {
56              ArtifactResult result = new ArtifactResult( request );
57              results.add( result );
58  
59              Artifact artifact = request.getArtifact();
60              if ( "maven-test".equals( artifact.getGroupId() ) )
61              {
62                  String scope = artifact.getArtifactId().substring( "scope-".length() );
63  
64                  try
65                  {
66                      artifact =
67                          artifact.setFile( ProjectClasspathTest.getFileForClasspathResource( ProjectClasspathTest.dir
68                              + "transitive-" + scope + "-dep.xml" ) );
69                      result.setArtifact( artifact );
70                  }
71                  catch ( FileNotFoundException e )
72                  {
73                      throw new IllegalStateException( "Missing test POM for " + artifact );
74                  }
75              }
76              else
77              {
78                  result.addException( new ArtifactNotFoundException( artifact, null ) );
79                  throw new ArtifactResolutionException( results );
80              }
81          }
82  
83          return results;
84      }
85  
86      public ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request )
87          throws ArtifactResolutionException
88      {
89          return resolveArtifacts( session, Collections.singleton( request ) ).get( 0 );
90      }
91  
92  }