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  @Deprecated
46  @Component( role = ProjectDependenciesResolver.class )
47  public class DefaultProjectDependenciesResolver
48      implements ProjectDependenciesResolver
49  {
50  
51      @Requirement
52      private RepositorySystem repositorySystem;
53  
54      @Requirement
55      private ResolutionErrorHandler resolutionErrorHandler;
56  
57      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToResolve, MavenSession session )
58          throws ArtifactResolutionException, ArtifactNotFoundException
59      {
60          return resolve( Collections.singleton( project ), scopesToResolve, session );
61      }
62  
63      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
64                                    Collection<String> scopesToResolve, MavenSession session )
65          throws ArtifactResolutionException, ArtifactNotFoundException
66      {
67          Set<MavenProject> mavenProjects = Collections.singleton( project );
68          return resolveImpl( mavenProjects, scopesToCollect, scopesToResolve, session,
69                              getIgnorableArtifacts( mavenProjects ) );
70      }
71  
72      public Set<Artifact> resolve( Collection<? extends MavenProject> projects, Collection<String> scopesToResolve,
73                                    MavenSession session )
74          throws ArtifactResolutionException, ArtifactNotFoundException
75      {
76          return resolveImpl( projects, null, scopesToResolve, session, getIgnorableArtifacts( projects ) );
77      }
78  
79      public Set<Artifact> resolve( MavenProject project, Collection<String> scopesToCollect,
80                                    Collection<String> scopesToResolve, MavenSession session,
81                                    Set<Artifact> ignoreableArtifacts )
82          throws ArtifactResolutionException, ArtifactNotFoundException
83      {
84          return resolveImpl( Collections.singleton( project ), scopesToCollect, scopesToResolve, session,
85                              getIgnorableArtifacts( ignoreableArtifacts ) );
86      }
87  
88  
89      private Set<Artifact> resolveImpl( Collection<? extends MavenProject> projects, Collection<String> scopesToCollect,
90                                         Collection<String> scopesToResolve, MavenSession session,
91                                         Set<String> projectIds )
92          throws ArtifactResolutionException, ArtifactNotFoundException
93      {
94          Set<Artifact> resolved = new LinkedHashSet<Artifact>();
95  
96          if ( projects == null || projects.isEmpty() )
97          {
98              return resolved;
99          }
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[]{ new ExcludesArtifactFilter( exclusions ), scopeFilter } ) );
131         }
132         else
133         {
134             filter = scopeFilter;
135         }
136         */
137 
138         CumulativeScopeArtifactFilter resolutionScopeFilter = new CumulativeScopeArtifactFilter( scopesToResolve );
139 
140         CumulativeScopeArtifactFilter collectionScopeFilter = new CumulativeScopeArtifactFilter( scopesToCollect );
141         collectionScopeFilter = new CumulativeScopeArtifactFilter( collectionScopeFilter, resolutionScopeFilter );
142 
143         ArtifactResolutionRequest request =
144             new ArtifactResolutionRequest().setResolveRoot( false ).setResolveTransitively( true ).setCollectionFilter(
145                 collectionScopeFilter ).setResolutionFilter( resolutionScopeFilter ).setLocalRepository(
146                 session.getLocalRepository() ).setOffline( session.isOffline() ).setForceUpdate(
147                 session.getRequest().isUpdateSnapshots() );
148         request.setServers( session.getRequest().getServers() );
149         request.setMirrors( session.getRequest().getMirrors() );
150         request.setProxies( session.getRequest().getProxies() );
151 
152         for ( MavenProject project : projects )
153         {
154             request.setArtifact( new ProjectArtifact( project ) );
155             request.setArtifactDependencies( project.getDependencyArtifacts() );
156             request.setManagedVersionMap( project.getManagedVersionMap() );
157             request.setRemoteRepositories( project.getRemoteArtifactRepositories() );
158 
159             ArtifactResolutionResult result = repositorySystem.resolve( request );
160 
161             try
162             {
163                 resolutionErrorHandler.throwErrors( request, result );
164             }
165             catch ( MultipleArtifactsNotFoundException e )
166             {
167 
168                 Collection<Artifact> missing = new HashSet<Artifact>( e.getMissingArtifacts() );
169 
170                 for ( Iterator<Artifact> it = missing.iterator(); it.hasNext(); )
171                 {
172                     String key = ArtifactUtils.key( it.next() );
173                     if ( projectIds.contains( key ) )
174                     {
175                         it.remove();
176                     }
177                 }
178 
179                 if ( !missing.isEmpty() )
180                 {
181                     throw e;
182                 }
183             }
184 
185             resolved.addAll( result.getArtifacts() );
186         }
187 
188         return resolved;
189     }
190 
191 
192     private Set<String> getIgnorableArtifacts( Collection<? extends MavenProject> projects )
193     {
194         Set<String> projectIds = new HashSet<String>( projects.size() * 2 );
195 
196         for ( MavenProject p : projects )
197         {
198             String key = ArtifactUtils.key( p.getGroupId(), p.getArtifactId(), p.getVersion() );
199             projectIds.add( key );
200         }
201         return projectIds;
202     }
203 
204     private Set<String> getIgnorableArtifacts( Iterable<Artifact> artifactIterable )
205     {
206         Set<String> projectIds = new HashSet<String>();
207 
208         for ( Artifact artifact : artifactIterable )
209         {
210             String key = ArtifactUtils.key( artifact );
211             projectIds.add( key );
212         }
213         return projectIds;
214     }
215 
216 }