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