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