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