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