View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.LinkedHashSet;
30  import java.util.Set;
31  
32  import org.apache.maven.artifact.Artifact;
33  import org.apache.maven.artifact.ArtifactUtils;
34  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
35  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
36  import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
37  import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
38  import org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException;
39  import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
40  import org.apache.maven.artifact.resolver.filter.CumulativeScopeArtifactFilter;
41  import org.apache.maven.execution.MavenSession;
42  import org.apache.maven.project.MavenProject;
43  import org.apache.maven.project.artifact.ProjectArtifact;
44  import org.apache.maven.repository.RepositorySystem;
45  
46  /**
47   * @deprecated As of 3.2.2, and there is no direct replacement. This is an internal class which was not marked as such,
48   *             but should have been.
49   *
50   */
51  @Named
52  @Singleton
53  @Deprecated
54  public class DefaultProjectDependenciesResolver implements ProjectDependenciesResolver {
55  
56      private final RepositorySystem repositorySystem;
57  
58      private final ResolutionErrorHandler resolutionErrorHandler;
59  
60      @Inject
61      public DefaultProjectDependenciesResolver(
62              RepositorySystem repositorySystem, ResolutionErrorHandler resolutionErrorHandler) {
63          this.repositorySystem = repositorySystem;
64          this.resolutionErrorHandler = resolutionErrorHandler;
65      }
66  
67      public Set<Artifact> resolve(MavenProject project, Collection<String> scopesToResolve, MavenSession session)
68              throws ArtifactResolutionException, ArtifactNotFoundException {
69          return resolve(Collections.singleton(project), scopesToResolve, session);
70      }
71  
72      public Set<Artifact> resolve(
73              MavenProject project,
74              Collection<String> scopesToCollect,
75              Collection<String> scopesToResolve,
76              MavenSession session)
77              throws ArtifactResolutionException, ArtifactNotFoundException {
78          Set<MavenProject> mavenProjects = Collections.singleton(project);
79          return resolveImpl(
80                  mavenProjects, scopesToCollect, scopesToResolve, session, getIgnorableArtifacts(mavenProjects));
81      }
82  
83      public Set<Artifact> resolve(
84              Collection<? extends MavenProject> projects, Collection<String> scopesToResolve, MavenSession session)
85              throws ArtifactResolutionException, ArtifactNotFoundException {
86          return resolveImpl(projects, null, scopesToResolve, session, getIgnorableArtifacts(projects));
87      }
88  
89      public Set<Artifact> resolve(
90              MavenProject project,
91              Collection<String> scopesToCollect,
92              Collection<String> scopesToResolve,
93              MavenSession session,
94              Set<Artifact> ignorableArtifacts)
95              throws ArtifactResolutionException, ArtifactNotFoundException {
96          return resolveImpl(
97                  Collections.singleton(project),
98                  scopesToCollect,
99                  scopesToResolve,
100                 session,
101                 getIgnorableArtifacts(ignorableArtifacts));
102     }
103 
104     private Set<Artifact> resolveImpl(
105             Collection<? extends MavenProject> projects,
106             Collection<String> scopesToCollect,
107             Collection<String> scopesToResolve,
108             MavenSession session,
109             Set<String> projectIds)
110             throws ArtifactResolutionException, ArtifactNotFoundException {
111         Set<Artifact> resolved = new LinkedHashSet<>();
112 
113         if (projects == null || projects.isEmpty()) {
114             return resolved;
115         }
116 
117         if ((scopesToCollect == null || scopesToCollect.isEmpty())
118                 && (scopesToResolve == null || scopesToResolve.isEmpty())) {
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 = new ArtifactResolutionRequest()
160                 .setResolveRoot(false)
161                 .setResolveTransitively(true)
162                 .setCollectionFilter(collectionScopeFilter)
163                 .setResolutionFilter(resolutionScopeFilter)
164                 .setLocalRepository(session.getLocalRepository())
165                 .setOffline(session.isOffline())
166                 .setForceUpdate(session.getRequest().isUpdateSnapshots());
167         request.setServers(session.getRequest().getServers());
168         request.setMirrors(session.getRequest().getMirrors());
169         request.setProxies(session.getRequest().getProxies());
170 
171         for (MavenProject project : projects) {
172             request.setArtifact(new ProjectArtifact(project));
173             request.setArtifactDependencies(project.getDependencyArtifacts());
174             request.setManagedVersionMap(project.getManagedVersionMap());
175             request.setRemoteRepositories(project.getRemoteArtifactRepositories());
176 
177             ArtifactResolutionResult result = repositorySystem.resolve(request);
178 
179             try {
180                 resolutionErrorHandler.throwErrors(request, result);
181             } catch (MultipleArtifactsNotFoundException e) {
182 
183                 Collection<Artifact> missing = new HashSet<>(e.getMissingArtifacts());
184 
185                 for (Iterator<Artifact> it = missing.iterator(); it.hasNext(); ) {
186                     String key = ArtifactUtils.key(it.next());
187                     if (projectIds.contains(key)) {
188                         it.remove();
189                     }
190                 }
191 
192                 if (!missing.isEmpty()) {
193                     throw e;
194                 }
195             }
196 
197             resolved.addAll(result.getArtifacts());
198         }
199 
200         return resolved;
201     }
202 
203     private Set<String> getIgnorableArtifacts(Collection<? extends MavenProject> projects) {
204         Set<String> projectIds = new HashSet<>(projects.size() * 2);
205 
206         for (MavenProject p : projects) {
207             String key = ArtifactUtils.key(p.getGroupId(), p.getArtifactId(), p.getVersion());
208             projectIds.add(key);
209         }
210         return projectIds;
211     }
212 
213     private Set<String> getIgnorableArtifacts(Iterable<Artifact> artifactIterable) {
214         Set<String> projectIds = new HashSet<>();
215 
216         for (Artifact artifact : artifactIterable) {
217             String key = ArtifactUtils.key(artifact);
218             projectIds.add(key);
219         }
220         return projectIds;
221     }
222 }