001    package org.apache.maven.project;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.FileNotFoundException;
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.List;
027    
028    import org.codehaus.plexus.component.annotations.Component;
029    import org.sonatype.aether.RepositorySystemSession;
030    import org.sonatype.aether.artifact.Artifact;
031    import org.sonatype.aether.impl.ArtifactResolver;
032    import org.sonatype.aether.resolution.ArtifactRequest;
033    import org.sonatype.aether.resolution.ArtifactResolutionException;
034    import org.sonatype.aether.resolution.ArtifactResult;
035    import org.sonatype.aether.transfer.ArtifactNotFoundException;
036    
037    /**
038     * @author Benjamin Bentmann
039     */
040    @Component( role = ArtifactResolver.class, hint = "classpath" )
041    public class ClasspathArtifactResolver
042        implements ArtifactResolver
043    {
044    
045        public List<ArtifactResult> resolveArtifacts( RepositorySystemSession session,
046                                                      Collection<? extends ArtifactRequest> requests )
047            throws ArtifactResolutionException
048        {
049            List<ArtifactResult> results = new ArrayList<ArtifactResult>();
050    
051            for ( ArtifactRequest request : requests )
052            {
053                ArtifactResult result = new ArtifactResult( request );
054                results.add( result );
055    
056                Artifact artifact = request.getArtifact();
057                if ( "maven-test".equals( artifact.getGroupId() ) )
058                {
059                    String scope = artifact.getArtifactId().substring( "scope-".length() );
060    
061                    try
062                    {
063                        artifact =
064                            artifact.setFile( ProjectClasspathTest.getFileForClasspathResource( ProjectClasspathTest.dir
065                                + "transitive-" + scope + "-dep.xml" ) );
066                        result.setArtifact( artifact );
067                    }
068                    catch ( FileNotFoundException e )
069                    {
070                        throw new IllegalStateException( "Missing test POM for " + artifact );
071                    }
072                }
073                else
074                {
075                    result.addException( new ArtifactNotFoundException( artifact, null ) );
076                    throw new ArtifactResolutionException( results );
077                }
078            }
079    
080            return results;
081        }
082    
083        public ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request )
084            throws ArtifactResolutionException
085        {
086            return resolveArtifacts( session, Collections.singleton( request ) ).get( 0 );
087        }
088    
089    }