1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.graph.transformer;
20
21 import org.eclipse.aether.RepositoryException;
22 import org.eclipse.aether.collection.DependencyGraphTransformationContext;
23 import org.eclipse.aether.collection.DependencyGraphTransformer;
24 import org.eclipse.aether.graph.Dependency;
25 import org.eclipse.aether.graph.DependencyNode;
26 import org.eclipse.aether.util.artifact.JavaScopes;
27
28 import static java.util.Objects.requireNonNull;
29
30
31
32
33
34
35
36
37 public final class JavaDependencyContextRefiner implements DependencyGraphTransformer {
38
39 public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
40 throws RepositoryException {
41 requireNonNull(node, "node cannot be null");
42 requireNonNull(context, "context cannot be null");
43 String ctx = node.getRequestContext();
44
45 if ("project".equals(ctx)) {
46 String scope = getClasspathScope(node);
47 if (scope != null) {
48 ctx += '/' + scope;
49 node.setRequestContext(ctx);
50 }
51 }
52
53 for (DependencyNode child : node.getChildren()) {
54 transformGraph(child, context);
55 }
56
57 return node;
58 }
59
60 private String getClasspathScope(DependencyNode node) {
61 Dependency dependency = node.getDependency();
62 if (dependency == null) {
63 return null;
64 }
65
66 String scope = dependency.getScope();
67
68 if (JavaScopes.COMPILE.equals(scope) || JavaScopes.SYSTEM.equals(scope) || JavaScopes.PROVIDED.equals(scope)) {
69 return JavaScopes.COMPILE;
70 } else if (JavaScopes.RUNTIME.equals(scope)) {
71 return JavaScopes.RUNTIME;
72 } else if (JavaScopes.TEST.equals(scope)) {
73 return JavaScopes.TEST;
74 }
75
76 return null;
77 }
78 }