View Javadoc
1   package org.eclipse.aether.util.graph.transformer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.eclipse.aether.RepositoryException;
23  import org.eclipse.aether.collection.DependencyGraphTransformationContext;
24  import org.eclipse.aether.collection.DependencyGraphTransformer;
25  import org.eclipse.aether.graph.Dependency;
26  import org.eclipse.aether.graph.DependencyNode;
27  import org.eclipse.aether.util.artifact.JavaScopes;
28  
29  /**
30   * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by
31   * appending the classpath type to which the node belongs. For instance, a compile-time project dependency will be
32   * assigned the request context "project/compile".
33   * 
34   * @see DependencyNode#getRequestContext()
35   */
36  public final class JavaDependencyContextRefiner
37      implements DependencyGraphTransformer
38  {
39  
40      public DependencyNode transformGraph( DependencyNode node, DependencyGraphTransformationContext context )
41          throws RepositoryException
42      {
43          String ctx = node.getRequestContext();
44  
45          if ( "project".equals( ctx ) )
46          {
47              String scope = getClasspathScope( node );
48              if ( scope != null )
49              {
50                  ctx += '/' + scope;
51                  node.setRequestContext( ctx );
52              }
53          }
54  
55          for ( DependencyNode child : node.getChildren() )
56          {
57              transformGraph( child, context );
58          }
59  
60          return node;
61      }
62  
63      private String getClasspathScope( DependencyNode node )
64      {
65          Dependency dependency = node.getDependency();
66          if ( dependency == null )
67          {
68              return null;
69          }
70  
71          String scope = dependency.getScope();
72  
73          if ( JavaScopes.COMPILE.equals( scope ) || JavaScopes.SYSTEM.equals( scope )
74              || JavaScopes.PROVIDED.equals( scope ) )
75          {
76              return JavaScopes.COMPILE;
77          }
78          else if ( JavaScopes.RUNTIME.equals( scope ) )
79          {
80              return JavaScopes.RUNTIME;
81          }
82          else if ( JavaScopes.TEST.equals( scope ) )
83          {
84              return JavaScopes.TEST;
85          }
86  
87          return null;
88      }
89  
90  }