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   * <p>
36   * To be used with Maven 3 (without scope manager).
37   */
38  public final class JavaScopeSelector extends ScopeSelector {
39  
40      /**
41       * Creates a new instance of this scope selector.
42       */
43      public JavaScopeSelector() {}
44  
45      @Override
46      public void selectScope(ConflictContext context) throws RepositoryException {
47          String scope = context.getWinner().getDependency().getScope();
48          if (!JavaScopes.SYSTEM.equals(scope)) {
49              scope = chooseEffectiveScope(context.getItems());
50          }
51          context.setScope(scope);
52      }
53  
54      private String chooseEffectiveScope(Collection<ConflictItem> items) {
55          Set<String> scopes = new HashSet<>();
56          for (ConflictItem item : items) {
57              if (item.getDepth() <= 1) {
58                  return item.getDependency().getScope();
59              }
60              scopes.addAll(item.getScopes());
61          }
62          return chooseEffectiveScope(scopes);
63      }
64  
65      private String chooseEffectiveScope(Set<String> scopes) {
66          if (scopes.size() > 1) {
67              scopes.remove(JavaScopes.SYSTEM);
68          }
69  
70          String effectiveScope = "";
71  
72          if (scopes.size() == 1) {
73              effectiveScope = scopes.iterator().next();
74          } else if (scopes.contains(JavaScopes.COMPILE)) {
75              effectiveScope = JavaScopes.COMPILE;
76          } else if (scopes.contains(JavaScopes.RUNTIME)) {
77              effectiveScope = JavaScopes.RUNTIME;
78          } else if (scopes.contains(JavaScopes.PROVIDED)) {
79              effectiveScope = JavaScopes.PROVIDED;
80          } else if (scopes.contains(JavaScopes.TEST)) {
81              effectiveScope = JavaScopes.TEST;
82          }
83  
84          return effectiveScope;
85      }
86  }