1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.maven.repository.internal.scopes;
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  
27  import static java.util.Objects.requireNonNull;
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  public final class MavenDependencyContextRefiner implements DependencyGraphTransformer {
39  
40      public MavenDependencyContextRefiner() {}
41  
42      @Override
43      public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context)
44              throws RepositoryException {
45          requireNonNull(node, "node cannot be null");
46          requireNonNull(context, "context cannot be null");
47          String ctx = node.getRequestContext();
48  
49          if ("project".equals(ctx)) {
50              String scope = getBuildpathScope(node);
51              if (scope != null) {
52                  ctx += '/' + scope;
53                  node.setRequestContext(ctx);
54              }
55          }
56  
57          for (DependencyNode child : node.getChildren()) {
58              transformGraph(child, context);
59          }
60  
61          return node;
62      }
63  
64      private String getBuildpathScope(DependencyNode node) {
65          Dependency dependency = node.getDependency();
66          if (dependency == null) {
67              return null;
68          }
69  
70          String scope = dependency.getScope();
71  
72          if (MavenDependencyScopes.COMPILE.equals(scope)
73                  || MavenDependencyScopes.SYSTEM.equals(scope)
74                  || MavenDependencyScopes.PROVIDED.equals(scope)) {
75              return MavenDependencyScopes.COMPILE;
76          } else if (MavenDependencyScopes.RUNTIME.equals(scope)) {
77              return MavenDependencyScopes.RUNTIME;
78          } else if (MavenDependencyScopes.TEST.equals(scope)) {
79              return MavenDependencyScopes.TEST;
80          }
81  
82          return null;
83      }
84  }