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 java.util.Collection; 022import java.util.HashSet; 023import java.util.Set; 024 025import org.eclipse.aether.RepositoryException; 026import org.eclipse.aether.util.artifact.DependencyScopes; 027import org.eclipse.aether.util.artifact.JavaScopes; 028import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext; 029import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem; 030import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeSelector; 031 032/** 033 * A scope selector for use with {@link ConflictResolver} that supports the scopes from {@link JavaScopes}. In general, 034 * this selector picks the widest scope present among conflicting dependencies where e.g. "compile" is wider than 035 * "runtime" which is wider than "test". If however a direct dependency is involved, its scope is selected. 036 * 037 * @deprecated This class belongs to consumer project. Resolver have no notion of scopes other than those defined 038 * in {@link DependencyScopes} class, moreover it has no knowledge about scope transformation 039 * of dependencies to build path scopes. 040 */ 041@Deprecated 042public final class JavaScopeSelector extends ScopeSelector { 043 044 /** 045 * Creates a new instance of this scope selector. 046 */ 047 public JavaScopeSelector() {} 048 049 @Override 050 public void selectScope(ConflictContext context) throws RepositoryException { 051 String scope = context.getWinner().getDependency().getScope(); 052 if (!JavaScopes.SYSTEM.equals(scope)) { 053 scope = chooseEffectiveScope(context.getItems()); 054 } 055 context.setScope(scope); 056 } 057 058 private String chooseEffectiveScope(Collection<ConflictItem> items) { 059 Set<String> scopes = new HashSet<>(); 060 for (ConflictItem item : items) { 061 if (item.getDepth() <= 1) { 062 return item.getDependency().getScope(); 063 } 064 scopes.addAll(item.getScopes()); 065 } 066 return chooseEffectiveScope(scopes); 067 } 068 069 private String chooseEffectiveScope(Set<String> scopes) { 070 if (scopes.size() > 1) { 071 scopes.remove(JavaScopes.SYSTEM); 072 } 073 074 String effectiveScope = ""; 075 076 if (scopes.size() == 1) { 077 effectiveScope = scopes.iterator().next(); 078 } else if (scopes.contains(JavaScopes.COMPILE)) { 079 effectiveScope = JavaScopes.COMPILE; 080 } else if (scopes.contains(JavaScopes.RUNTIME)) { 081 effectiveScope = JavaScopes.RUNTIME; 082 } else if (scopes.contains(JavaScopes.PROVIDED)) { 083 effectiveScope = JavaScopes.PROVIDED; 084 } else if (scopes.contains(JavaScopes.TEST)) { 085 effectiveScope = JavaScopes.TEST; 086 } 087 088 return effectiveScope; 089 } 090}