001package org.apache.maven;
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
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.Iterator;
026import java.util.LinkedHashSet;
027import java.util.Set;
028
029import org.apache.maven.artifact.Artifact;
030import org.apache.maven.artifact.ArtifactUtils;
031import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
032import org.apache.maven.artifact.resolver.ArtifactResolutionException;
033import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
034import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
035import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
036import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
037import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
038import org.apache.maven.execution.MavenSession;
039import org.apache.maven.project.MavenProject;
040import org.apache.maven.project.artifact.ProjectArtifact;
041import org.apache.maven.repository.RepositorySystem;
042import org.codehaus.plexus.component.annotations.Component;
043import org.codehaus.plexus.component.annotations.Requirement;
044
045@Deprecated
046@Component( role = ProjectDependenciesResolver.class )
047public class DefaultProjectDependenciesResolver
048    implements ProjectDependenciesResolver
049{
050
051    @Requirement
052    private RepositorySystem repositorySystem;
053
054    @Requirement
055    private ResolutionErrorHandler resolutionErrorHandler;
056
057    public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
058        throws ArtifactResolutionException, ArtifactNotFoundException
059    {
060        return resolve( Collections.singleton( project ), scopesToResolve, session );
061    }
062
063    public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
064                                  Collection<String> scopesToResolve, MavenSession session )
065        throws ArtifactResolutionException, ArtifactNotFoundException
066    {
067        Set<MavenProject> mavenProjects = Collections.singleton( project );
068        return resolveImpl( mavenProjects, scopesToCollect, scopesToResolve, session,
069                            getIgnorableArtifacts( mavenProjects ) );
070    }
071
072    public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve,
073                                  MavenSession session )
074        throws ArtifactResolutionException, ArtifactNotFoundException
075    {
076        return resolveImpl( projects, null, scopesToResolve, session, getIgnorableArtifacts( projects ) );
077    }
078
079    public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
080                                  Collection<String> scopesToResolve, MavenSession session,
081                                  Set<Artifact> ignoreableArtifacts )
082        throws ArtifactResolutionException, ArtifactNotFoundException
083    {
084        return resolveImpl( Collections.singleton( project ), scopesToCollect, scopesToResolve, session,
085                            getIgnorableArtifacts( ignoreableArtifacts ) );
086    }
087
088
089    private Set<Artifact> resolveImpl( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect,
090                                       Collection<String> scopesToResolve, MavenSession session,
091                                       Set<String> projectIds )
092        throws ArtifactResolutionException, ArtifactNotFoundException
093    {
094        Set<Artifact> resolved = new LinkedHashSet<Artifact>();
095
096        if ( projects == null || projects.isEmpty() )
097        {
098            return resolved;
099        }
100
101        if ( ( scopesToCollect == null || scopesToCollect.isEmpty() )
102            && ( scopesToResolve == null || scopesToResolve.isEmpty() ) )
103        {
104            return resolved;
105        }
106
107        /*
108
109        Logic for transitive global exclusions
110
111        List<String> exclusions = new ArrayList<String>();
112
113        for ( Dependency d : project.getDependencies() )
114        {
115            if ( d.getExclusions() != null )
116            {
117                for ( Exclusion e : d.getExclusions() )
118                {
119                    exclusions.add(  e.getGroupId() + ":" + e.getArtifactId() );
120                }
121            }
122        }
123
124        ArtifactFilter scopeFilter = new ScopeArtifactFilter( scope );
125
126        ArtifactFilter filter;
127
128        if ( ! exclusions.isEmpty() )
129        {
130            filter = new AndArtifactFilter( Arrays.asList( new ArtifactFilter[]{ 
131                new ExcludesArtifactFilter( exclusions ), scopeFilter } ) );
132        }
133        else
134        {
135            filter = scopeFilter;
136        }
137        */
138
139        CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve );
140
141        CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect );
142        collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter );
143
144        ArtifactResolutionRequest request =
145            new ArtifactResolutionRequest().setResolveRoot( false ).setResolveTransitively( true ).setCollectionFilter(
146                collectionScopeFilter ).setResolutionFilter( resolutionScopeFilter ).setLocalRepository(
147                session.getLocalRepository() ).setOffline( session.isOffline() ).setForceUpdate(
148                session.getRequest().isUpdateSnapshots() );
149        request.setServers( session.getRequest().getServers() );
150        request.setMirrors( session.getRequest().getMirrors() );
151        request.setProxies( session.getRequest().getProxies() );
152
153        for ( MavenProject project : projects )
154        {
155            request.setArtifact( new ProjectArtifact( project ) );
156            request.setArtifactDependencies( project.getDependencyArtifacts() );
157            request.setManagedVersionMap( project.getManagedVersionMap() );
158            request.setRemoteRepositories( project.getRemoteArtifactRepositories() );
159
160            ArtifactResolutionResult result = repositorySystem.resolve( request );
161
162            try
163            {
164                resolutionErrorHandler.throwErrors( request, result );
165            }
166            catch ( MultipleArtifactsNotFoundException e )
167            {
168
169                Collection<Artifact> missing = new HashSet<Artifact>( e.getMissingArtifacts() );
170
171                for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); )
172                {
173                    String key = ArtifactUtils.key( it.next() );
174                    if ( projectIds.contains( key ) )
175                    {
176                        it.remove();
177                    }
178                }
179
180                if ( !missing.isEmpty() )
181                {
182                    throw e;
183                }
184            }
185
186            resolved.addAll( result.getArtifacts() );
187        }
188
189        return resolved;
190    }
191
192
193    private Set<String> getIgnorableArtifacts( Collection<? extends MavenProject> projects )
194    {
195        Set<String> projectIds = new HashSet<String>( projects.size() * 2 );
196
197        for ( MavenProject p : projects )
198        {
199            String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() );
200            projectIds.add( key );
201        }
202        return projectIds;
203    }
204
205    private Set<String> getIgnorableArtifacts( Iterable<Artifact> artifactIterable )
206    {
207        Set<String> projectIds = new HashSet<String>();
208
209        for ( Artifact artifact : artifactIterable )
210        {
211            String key = ArtifactUtils.key( artifact );
212            projectIds.add( key );
213        }
214        return projectIds;
215    }
216
217}