1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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 }