1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.internal;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23
24 import java.io.File;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.LinkedHashSet;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34
35 import org.apache.maven.RepositoryUtils;
36 import org.apache.maven.api.services.MessageBuilderFactory;
37 import org.apache.maven.artifact.Artifact;
38 import org.apache.maven.artifact.ArtifactUtils;
39 import org.apache.maven.eventspy.internal.EventSpyDispatcher;
40 import org.apache.maven.execution.MavenSession;
41 import org.apache.maven.lifecycle.LifecycleExecutionException;
42 import org.apache.maven.project.DefaultDependencyResolutionRequest;
43 import org.apache.maven.project.DependencyResolutionException;
44 import org.apache.maven.project.DependencyResolutionResult;
45 import org.apache.maven.project.MavenProject;
46 import org.apache.maven.project.ProjectDependenciesResolver;
47 import org.apache.maven.project.artifact.InvalidDependencyVersionException;
48 import org.apache.maven.project.artifact.ProjectArtifactsCache;
49 import org.eclipse.aether.graph.Dependency;
50 import org.eclipse.aether.graph.DependencyFilter;
51 import org.eclipse.aether.graph.DependencyNode;
52 import org.eclipse.aether.util.filter.AndDependencyFilter;
53 import org.eclipse.aether.util.filter.ScopeDependencyFilter;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57
58
59
60
61
62
63
64 @Named
65 public class LifecycleDependencyResolver {
66 private final Logger logger = LoggerFactory.getLogger(getClass());
67
68 private final ProjectDependenciesResolver dependenciesResolver;
69
70 private final ProjectArtifactFactory artifactFactory;
71
72 private final EventSpyDispatcher eventSpyDispatcher;
73
74 private final ProjectArtifactsCache projectArtifactsCache;
75
76 private final MessageBuilderFactory messageBuilderFactory;
77
78 @Inject
79 public LifecycleDependencyResolver(
80 ProjectDependenciesResolver dependenciesResolver,
81 ProjectArtifactFactory artifactFactory,
82 EventSpyDispatcher eventSpyDispatcher,
83 ProjectArtifactsCache projectArtifactsCache,
84 MessageBuilderFactory messageBuilderFactory) {
85 this.dependenciesResolver = dependenciesResolver;
86 this.artifactFactory = artifactFactory;
87 this.eventSpyDispatcher = eventSpyDispatcher;
88 this.projectArtifactsCache = projectArtifactsCache;
89 this.messageBuilderFactory = messageBuilderFactory;
90 }
91
92 public static List<MavenProject> getProjects(MavenProject project, MavenSession session, boolean aggregator) {
93 if (aggregator && project.getCollectedProjects() != null) {
94
95 Set<MavenProject> projectAndSubmodules = new HashSet<>(project.getCollectedProjects());
96 projectAndSubmodules.add(project);
97 return session.getProjects().stream()
98 .filter(projectAndSubmodules::contains)
99 .collect(Collectors.toList());
100 } else {
101 return Collections.singletonList(project);
102 }
103 }
104
105 public void resolveProjectDependencies(
106 MavenProject project,
107 Collection<String> scopesToCollect,
108 Collection<String> scopesToResolve,
109 MavenSession session,
110 boolean aggregating,
111 Set<Artifact> projectArtifacts)
112 throws LifecycleExecutionException {
113 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
114 try {
115 ClassLoader projectRealm = project.getClassRealm();
116 if (projectRealm != null && projectRealm != tccl) {
117 Thread.currentThread().setContextClassLoader(projectRealm);
118 }
119
120 if (project.getDependencyArtifacts() == null) {
121 try {
122 project.setDependencyArtifacts(artifactFactory.createArtifacts(project));
123 } catch (InvalidDependencyVersionException e) {
124 throw new LifecycleExecutionException(e);
125 }
126 }
127
128 Set<Artifact> resolvedArtifacts = resolveProjectArtifacts(
129 project, scopesToCollect, scopesToResolve, session, aggregating, projectArtifacts);
130
131 Map<Artifact, File> reactorProjects =
132 new HashMap<>(session.getProjects().size());
133 for (MavenProject reactorProject : session.getProjects()) {
134 reactorProjects.put(
135 reactorProject.getArtifact(),
136 reactorProject.getArtifact().getFile());
137 }
138
139 Map<String, Artifact> map = new HashMap<>();
140 for (Artifact artifact : resolvedArtifacts) {
141
142
143
144
145
146 File reactorProjectFile = reactorProjects.get(artifact);
147 if (reactorProjectFile != null) {
148 artifact.setFile(reactorProjectFile);
149 }
150
151 map.put(artifact.getDependencyConflictId(), artifact);
152 }
153
154 project.setResolvedArtifacts(resolvedArtifacts);
155
156 for (Artifact artifact : project.getDependencyArtifacts()) {
157 if (artifact.getFile() == null) {
158 Artifact resolved = map.get(artifact.getDependencyConflictId());
159 if (resolved != null) {
160 artifact.setFile(resolved.getFile());
161 artifact.setDependencyTrail(resolved.getDependencyTrail());
162 artifact.setResolvedVersion(resolved.getVersion());
163 artifact.setResolved(true);
164 }
165 }
166 }
167 } finally {
168 Thread.currentThread().setContextClassLoader(tccl);
169 }
170 }
171
172 public DependencyResolutionResult getProjectDependencyResolutionResult(
173 MavenProject project,
174 Collection<String> scopesToCollect,
175 Collection<String> scopesToResolve,
176 MavenSession session,
177 boolean aggregating,
178 Set<Artifact> projectArtifacts)
179 throws LifecycleExecutionException {
180
181 Set<Artifact> resolvedArtifacts = resolveProjectArtifacts(
182 project, scopesToCollect, scopesToResolve, session, aggregating, projectArtifacts);
183 if (resolvedArtifacts instanceof ProjectArtifactsCache.ArtifactsSetWithResult) {
184 return ((ProjectArtifactsCache.ArtifactsSetWithResult) resolvedArtifacts).getResult();
185 } else {
186 throw new IllegalStateException();
187 }
188 }
189
190 public Set<Artifact> resolveProjectArtifacts(
191 MavenProject project,
192 Collection<String> scopesToCollect,
193 Collection<String> scopesToResolve,
194 MavenSession session,
195 boolean aggregating,
196 Set<Artifact> projectArtifacts)
197 throws LifecycleExecutionException {
198 Set<Artifact> resolvedArtifacts;
199 ProjectArtifactsCache.Key cacheKey = projectArtifactsCache.createKey(
200 project, scopesToCollect, scopesToResolve, aggregating, session.getRepositorySession());
201 ProjectArtifactsCache.CacheRecord recordArtifacts;
202 recordArtifacts = projectArtifactsCache.get(cacheKey);
203
204 if (recordArtifacts != null) {
205 resolvedArtifacts = recordArtifacts.getArtifacts();
206 } else {
207 try {
208 resolvedArtifacts = getDependencies(
209 project, scopesToCollect, scopesToResolve, session, aggregating, projectArtifacts);
210 recordArtifacts = projectArtifactsCache.put(cacheKey, resolvedArtifacts);
211 } catch (LifecycleExecutionException e) {
212 projectArtifactsCache.put(cacheKey, e);
213 projectArtifactsCache.register(project, cacheKey, recordArtifacts);
214 throw e;
215 }
216 }
217 projectArtifactsCache.register(project, cacheKey, recordArtifacts);
218 return resolvedArtifacts;
219 }
220
221 private Set<Artifact> getDependencies(
222 MavenProject project,
223 Collection<String> scopesToCollect,
224 Collection<String> scopesToResolve,
225 MavenSession session,
226 boolean aggregating,
227 Set<Artifact> projectArtifacts)
228 throws LifecycleExecutionException {
229 if (scopesToCollect == null) {
230 scopesToCollect = Collections.emptySet();
231 }
232 if (scopesToResolve == null) {
233 scopesToResolve = Collections.emptySet();
234 }
235
236 if (scopesToCollect.isEmpty() && scopesToResolve.isEmpty()) {
237 return new SetWithResolutionResult(null, new LinkedHashSet<>());
238 }
239
240 scopesToCollect = new HashSet<>(scopesToCollect);
241 scopesToCollect.addAll(scopesToResolve);
242
243 DependencyFilter collectionFilter = new ScopeDependencyFilter(null, negate(scopesToCollect));
244 DependencyFilter resolutionFilter = new ScopeDependencyFilter(null, negate(scopesToResolve));
245 resolutionFilter = AndDependencyFilter.newInstance(collectionFilter, resolutionFilter);
246 resolutionFilter =
247 AndDependencyFilter.newInstance(resolutionFilter, new ReactorDependencyFilter(projectArtifacts));
248
249 DependencyResolutionResult result;
250 try {
251 DefaultDependencyResolutionRequest request =
252 new DefaultDependencyResolutionRequest(project, session.getRepositorySession());
253 request.setResolutionFilter(resolutionFilter);
254
255 eventSpyDispatcher.onEvent(request);
256
257 result = dependenciesResolver.resolve(request);
258 } catch (DependencyResolutionException e) {
259 result = e.getResult();
260
261
262
263
264
265
266 if (aggregating && areAllDependenciesInReactor(session.getProjects(), result.getUnresolvedDependencies())) {
267 logger.warn("The following dependencies could not be resolved at this point of the build"
268 + " but seem to be part of the reactor:");
269
270 for (Dependency dependency : result.getUnresolvedDependencies()) {
271 logger.warn("o {}", dependency);
272 }
273
274 logger.warn("Try running the build up to the lifecycle phase \"package\"");
275 } else {
276 throw new LifecycleExecutionException(messageBuilderFactory, null, project, e);
277 }
278 }
279
280 eventSpyDispatcher.onEvent(result);
281
282 Set<Artifact> artifacts = new LinkedHashSet<>();
283 if (result.getDependencyGraph() != null
284 && !result.getDependencyGraph().getChildren().isEmpty()) {
285 RepositoryUtils.toArtifacts(
286 artifacts,
287 result.getDependencyGraph().getChildren(),
288 Collections.singletonList(project.getArtifact().getId()),
289 collectionFilter);
290 }
291 return new SetWithResolutionResult(result, artifacts);
292 }
293
294 private boolean areAllDependenciesInReactor(
295 Collection<MavenProject> projects, Collection<Dependency> dependencies) {
296 Set<String> projectKeys = getReactorProjectKeys(projects);
297
298 for (Dependency dependency : dependencies) {
299 org.eclipse.aether.artifact.Artifact a = dependency.getArtifact();
300 String key = ArtifactUtils.key(a.getGroupId(), a.getArtifactId(), a.getVersion());
301 if (!projectKeys.contains(key)) {
302 return false;
303 }
304 }
305
306 return true;
307 }
308
309 private Set<String> getReactorProjectKeys(Collection<MavenProject> projects) {
310 Set<String> projectKeys = new HashSet<>(projects.size() * 2);
311 for (MavenProject project : projects) {
312 String key = ArtifactUtils.key(project.getGroupId(), project.getArtifactId(), project.getVersion());
313 projectKeys.add(key);
314 }
315 return projectKeys;
316 }
317
318 private Collection<String> negate(Collection<String> scopes) {
319 Collection<String> result = new HashSet<>();
320 Collections.addAll(result, "system", "compile", "provided", "runtime", "test");
321
322 for (String scope : scopes) {
323 if ("compile".equals(scope)) {
324 result.remove("compile");
325 result.remove("system");
326 result.remove("provided");
327 } else if ("runtime".equals(scope)) {
328 result.remove("compile");
329 result.remove("runtime");
330 } else if ("compile+runtime".equals(scope)) {
331 result.remove("compile");
332 result.remove("system");
333 result.remove("provided");
334 result.remove("runtime");
335 } else if ("runtime+system".equals(scope)) {
336 result.remove("compile");
337 result.remove("system");
338 result.remove("runtime");
339 } else if ("test".equals(scope)) {
340 result.clear();
341 }
342 }
343
344 return result;
345 }
346
347 private static class ReactorDependencyFilter implements DependencyFilter {
348
349 private Set<String> keys = new HashSet<>();
350
351 ReactorDependencyFilter(Collection<Artifact> artifacts) {
352 for (Artifact artifact : artifacts) {
353 String key = ArtifactUtils.key(artifact);
354 keys.add(key);
355 }
356 }
357
358 public boolean accept(DependencyNode node, List<DependencyNode> parents) {
359 Dependency dependency = node.getDependency();
360 if (dependency != null) {
361 org.eclipse.aether.artifact.Artifact a = dependency.getArtifact();
362 String key = ArtifactUtils.key(a.getGroupId(), a.getArtifactId(), a.getVersion());
363 return !keys.contains(key);
364 }
365 return false;
366 }
367 }
368 }