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