View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.transformer;
20  
21  import java.util.Collection;
22  import java.util.HashSet;
23  import java.util.Set;
24  
25  import org.eclipse.aether.RepositoryException;
26  import org.eclipse.aether.util.artifact.JavaScopes;
27  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext;
28  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem;
29  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeSelector;
30  
31  /**
32   * A scope selector for use with {@link ConflictResolver} that supports the scopes from {@link JavaScopes}. In general,
33   * this selector picks the widest scope present among conflicting dependencies where e.g. "compile" is wider than
34   * "runtime" which is wider than "test". If however a direct dependency is involved, its scope is selected.
35   */
36  public final class JavaScopeSelector extends ScopeSelector {
37  
38      /**
39       * Creates a new instance of this scope selector.
40       */
41      public JavaScopeSelector() {}
42  
43      @Override
44      public void selectScope(ConflictContext context) throws RepositoryException {
45          String scope = context.getWinner().getDependency().getScope();
46          if (!JavaScopes.SYSTEM.equals(scope)) {
47              scope = chooseEffectiveScope(context.getItems());
48          }
49          context.setScope(scope);
50      }
51  
52      private String chooseEffectiveScope(Collection<ConflictItem> items) {
53          Set<String> scopes = new HashSet<>();
54          for (ConflictItem item : items) {
55              if (item.getDepth() <= 1) {
56                  return item.getDependency().getScope();
57              }
58              scopes.addAll(item.getScopes());
59          }
60          return chooseEffectiveScope(scopes);
61      }
62  
63      private String chooseEffectiveScope(Set<String> scopes) {
64          if (scopes.size() > 1) {
65              scopes.remove(JavaScopes.SYSTEM);
66          }
67  
68          String effectiveScope = "";
69  
70          if (scopes.size() == 1) {
71              effectiveScope = scopes.iterator().next();
72          } else if (scopes.contains(JavaScopes.COMPILE)) {
73              effectiveScope = JavaScopes.COMPILE;
74          } else if (scopes.contains(JavaScopes.RUNTIME)) {
75              effectiveScope = JavaScopes.RUNTIME;
76          } else if (scopes.contains(JavaScopes.PROVIDED)) {
77              effectiveScope = JavaScopes.PROVIDED;
78          } else if (scopes.contains(JavaScopes.TEST)) {
79              effectiveScope = JavaScopes.TEST;
80          }
81  
82          return effectiveScope;
83      }
84  }