View Javadoc
1   package org.apache.maven;
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.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Iterator;
26  import java.util.LinkedHashSet;
27  import java.util.Set;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.ArtifactUtils;
31  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
34  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
35  import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
36  import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
37  import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
38  import org.apache.maven.execution.MavenSession;
39  import org.apache.maven.project.MavenProject;
40  import org.apache.maven.project.artifact.ProjectArtifact;
41  import org.apache.maven.repository.RepositorySystem;
42  import org.codehaus.plexus.component.annotations.Component;
43  import org.codehaus.plexus.component.annotations.Requirement;
44  
45  @Component( role = ProjectDependenciesResolver.class )
46  public class DefaultProjectDependenciesResolver
47      implements ProjectDependenciesResolver
48  {
49  
50      @Requirement
51      private RepositorySystem repositorySystem;
52  
53      @Requirement
54      private ResolutionErrorHandler resolutionErrorHandler;
55  
56      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
57          throws ArtifactResolutionException, ArtifactNotFoundException
58      {
59          return resolve( Collections.singleton( project ), scopesToResolve, session );
60      }
61  
62      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
63                                    Collection<String> scopesToResolve, MavenSession session )
64          throws ArtifactResolutionException, ArtifactNotFoundException
65      {
66          Set<MavenProject> mavenProjects = Collections.singleton( project );
67          return resolveImpl( mavenProjects, scopesToCollect, scopesToResolve, session,
68                              getIgnorableArtifacts( mavenProjects ) );
69      }
70  
71      public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve,
72                                    MavenSession session )
73          throws ArtifactResolutionException, ArtifactNotFoundException
74      {
75          return resolveImpl( projects, null, scopesToResolve, session, getIgnorableArtifacts( projects ) );
76      }
77  
78      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
79                                    Collection<String> scopesToResolve, MavenSession session,
80                                    Set<Artifact> ignoreableArtifacts )
81          throws ArtifactResolutionException, ArtifactNotFoundException
82      {
83          return resolveImpl( Collections.singleton( project ), scopesToCollect, scopesToResolve, session,
84                              getIgnorableArtifacts( ignoreableArtifacts ) );
85      }
86  
87  
88      private Set<Artifact> resolveImpl( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect,
89                                         Collection<String> scopesToResolve, MavenSession session,
90                                         Set<String> projectIds )
91          throws ArtifactResolutionException, ArtifactNotFoundException
92      {
93          Set<Artifact> resolved = new LinkedHashSet<Artifact>();
94  
95          if ( projects == null || projects.isEmpty() )
96          {
97              return resolved;
98          }
99  
100         if ( ( scopesToCollect == null || scopesToCollect.isEmpty() )
101             && ( scopesToResolve == null || scopesToResolve.isEmpty() ) )
102         {
103             return resolved;
104         }
105 
106         /*
107 
108         Logic for transitive global exclusions
109 
110         List<String> exclusions = new ArrayList<String>();
111 
112         for ( Dependency d : project.getDependencies() )
113         {
114             if ( d.getExclusions() != null )
115             {
116                 for ( Exclusion e : d.getExclusions() )
117                 {
118                     exclusions.add(  e.getGroupId() + ":" + e.getArtifactId() );
119                 }
120             }
121         }
122 
123         ArtifactFilter scopeFilter = new ScopeArtifactFilter( scope );
124 
125         ArtifactFilter filter;
126 
127         if ( ! exclusions.isEmpty() )
128         {
129             filter = new AndArtifactFilter( Arrays.asList( new ArtifactFilter[]{ new ExcludesArtifactFilter( exclusions ), scopeFilter } ) );
130         }
131         else
132         {
133             filter = scopeFilter;
134         }
135         */
136 
137         CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve );
138 
139         CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect );
140         collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter );
141 
142         ArtifactResolutionRequest request =
143             new ArtifactResolutionRequest().setResolveRoot( false ).setResolveTransitively( true ).setCollectionFilter(
144                 collectionScopeFilter ).setResolutionFilter( resolutionScopeFilter ).setLocalRepository(
145                 session.getLocalRepository() ).setOffline( session.isOffline() ).setForceUpdate(
146                 session.getRequest().isUpdateSnapshots() );
147         request.setServers( session.getRequest().getServers() );
148         request.setMirrors( session.getRequest().getMirrors() );
149         request.setProxies( session.getRequest().getProxies() );
150 
151         for ( MavenProject project : projects )
152         {
153             request.setArtifact( new ProjectArtifact( project ) );
154             request.setArtifactDependencies( project.getDependencyArtifacts() );
155             request.setManagedVersionMap( project.getManagedVersionMap() );
156             request.setRemoteRepositories( project.getRemoteArtifactRepositories() );
157 
158             ArtifactResolutionResult result = repositorySystem.resolve( request );
159 
160             try
161             {
162                 resolutionErrorHandler.throwErrors( request, result );
163             }
164             catch ( MultipleArtifactsNotFoundException e )
165             {
166 
167                 Collection<Artifact> missing = new HashSet<Artifact>( e.getMissingArtifacts() );
168 
169                 for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); )
170                 {
171                     String key = ArtifactUtils.key( it.next() );
172                     if ( projectIds.contains( key ) )
173                     {
174                         it.remove();
175                     }
176                 }
177 
178                 if ( !missing.isEmpty() )
179                 {
180                     throw e;
181                 }
182             }
183 
184             resolved.addAll( result.getArtifacts() );
185         }
186 
187         return resolved;
188     }
189 
190 
191     private Set<String> getIgnorableArtifacts( Collection<? extends MavenProject> projects )
192     {
193         Set<String> projectIds = new HashSet<String>( projects.size() * 2 );
194 
195         for ( MavenProject p : projects )
196         {
197             String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() );
198             projectIds.add( key );
199         }
200         return projectIds;
201     }
202 
203     private Set<String> getIgnorableArtifacts( Iterable<Artifact> artifactIterable )
204     {
205         Set<String> projectIds = new HashSet<String>();
206 
207         for ( Artifact artifact : artifactIterable )
208         {
209             String key = ArtifactUtils.key( artifact );
210             projectIds.add( key );
211         }
212         return projectIds;
213     }
214 
215 }