001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.graph.transformer; 020 021import org.eclipse.aether.RepositoryException; 022import org.eclipse.aether.collection.DependencyGraphTransformationContext; 023import org.eclipse.aether.collection.DependencyGraphTransformer; 024import org.eclipse.aether.graph.Dependency; 025import org.eclipse.aether.graph.DependencyNode; 026import org.eclipse.aether.util.artifact.JavaScopes; 027 028import static java.util.Objects.requireNonNull; 029 030/** 031 * A dependency graph transformer that refines the request context for nodes that belong to the "project" context by 032 * appending the classpath type to which the node belongs. For instance, a compile-time project dependency will be 033 * assigned the request context "project/compile". 034 * 035 * @see DependencyNode#getRequestContext() 036 */ 037public final class JavaDependencyContextRefiner implements DependencyGraphTransformer { 038 039 public DependencyNode transformGraph(DependencyNode node, DependencyGraphTransformationContext context) 040 throws RepositoryException { 041 requireNonNull(node, "node cannot be null"); 042 requireNonNull(context, "context cannot be null"); 043 String ctx = node.getRequestContext(); 044 045 if ("project".equals(ctx)) { 046 String scope = getClasspathScope(node); 047 if (scope != null) { 048 ctx += '/' + scope; 049 node.setRequestContext(ctx); 050 } 051 } 052 053 for (DependencyNode child : node.getChildren()) { 054 transformGraph(child, context); 055 } 056 057 return node; 058 } 059 060 private String getClasspathScope(DependencyNode node) { 061 Dependency dependency = node.getDependency(); 062 if (dependency == null) { 063 return null; 064 } 065 066 String scope = dependency.getScope(); 067 068 if (JavaScopes.COMPILE.equals(scope) || JavaScopes.SYSTEM.equals(scope) || JavaScopes.PROVIDED.equals(scope)) { 069 return JavaScopes.COMPILE; 070 } else if (JavaScopes.RUNTIME.equals(scope)) { 071 return JavaScopes.RUNTIME; 072 } else if (JavaScopes.TEST.equals(scope)) { 073 return JavaScopes.TEST; 074 } 075 076 return null; 077 } 078}