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