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.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   * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
32   * appending the buildpath type to which the node belongs. For instance, a compile-time project dependency will be
33   * assigned the request context "project/compile".
34   *
35   * @see DependencyNode#getRequestContext()
36   *
37   * @deprecated This class belongs to consumer project. Resolver should have no notion of scopes.
38   */
39  @Deprecated
40  public final class JavaDependencyContextRefiner implements DependencyGraphTransformer {
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 (JavaScopes.COMPILE.equals(scope) || JavaScopes.SYSTEM.equals(scope) || JavaScopes.PROVIDED.equals(scope)) {
73              return JavaScopes.COMPILE;
74          } else if (JavaScopes.RUNTIME.equals(scope)) {
75              return JavaScopes.RUNTIME;
76          } else if (JavaScopes.TEST.equals(scope)) {
77              return JavaScopes.TEST;
78          }
79  
80          return null;
81      }
82  }