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