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 */ 036public final class JavaScopeSelector extends ScopeSelector { 037 038 /** 039 * Creates a new instance of this scope selector. 040 */ 041 public JavaScopeSelector() {} 042 043 @Override 044 public void selectScope(ConflictContext context) throws RepositoryException { 045 String scope = context.getWinner().getDependency().getScope(); 046 if (!JavaScopes.SYSTEM.equals(scope)) { 047 scope = chooseEffectiveScope(context.getItems()); 048 } 049 context.setScope(scope); 050 } 051 052 private String chooseEffectiveScope(Collection<ConflictItem> items) { 053 Set<String> scopes = new HashSet<>(); 054 for (ConflictItem item : items) { 055 if (item.getDepth() <= 1) { 056 return item.getDependency().getScope(); 057 } 058 scopes.addAll(item.getScopes()); 059 } 060 return chooseEffectiveScope(scopes); 061 } 062 063 private String chooseEffectiveScope(Set<String> scopes) { 064 if (scopes.size() > 1) { 065 scopes.remove(JavaScopes.SYSTEM); 066 } 067 068 String effectiveScope = ""; 069 070 if (scopes.size() == 1) { 071 effectiveScope = scopes.iterator().next(); 072 } else if (scopes.contains(JavaScopes.COMPILE)) { 073 effectiveScope = JavaScopes.COMPILE; 074 } else if (scopes.contains(JavaScopes.RUNTIME)) { 075 effectiveScope = JavaScopes.RUNTIME; 076 } else if (scopes.contains(JavaScopes.PROVIDED)) { 077 effectiveScope = JavaScopes.PROVIDED; 078 } else if (scopes.contains(JavaScopes.TEST)) { 079 effectiveScope = JavaScopes.TEST; 080 } 081 082 return effectiveScope; 083 } 084}