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.DependencyScopes;
27  import org.eclipse.aether.util.artifact.JavaScopes;
28  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext;
29  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem;
30  import org.eclipse.aether.util.graph.transformer.ConflictResolver.ScopeSelector;
31  
32  /**
33   * A scope selector for use with {@link ConflictResolver} that supports the scopes from {@link JavaScopes}. In general,
34   * this selector picks the widest scope present among conflicting dependencies where e.g. "compile" is wider than
35   * "runtime" which is wider than "test". If however a direct dependency is involved, its scope is selected.
36   *
37   * @deprecated This class belongs to consumer project. Resolver have no notion of scopes other than those defined
38   * in {@link DependencyScopes} class, moreover it has no knowledge about scope transformation
39   * of dependencies to build path scopes.
40   */
41  @Deprecated
42  public final class JavaScopeSelector extends ScopeSelector {
43  
44      /**
45       * Creates a new instance of this scope selector.
46       */
47      public JavaScopeSelector() {}
48  
49      @Override
50      public void selectScope(ConflictContext context) throws RepositoryException {
51          String scope = context.getWinner().getDependency().getScope();
52          if (!JavaScopes.SYSTEM.equals(scope)) {
53              scope = chooseEffectiveScope(context.getItems());
54          }
55          context.setScope(scope);
56      }
57  
58      private String chooseEffectiveScope(Collection<ConflictItem> items) {
59          Set<String> scopes = new HashSet<>();
60          for (ConflictItem item : items) {
61              if (item.getDepth() <= 1) {
62                  return item.getDependency().getScope();
63              }
64              scopes.addAll(item.getScopes());
65          }
66          return chooseEffectiveScope(scopes);
67      }
68  
69      private String chooseEffectiveScope(Set<String> scopes) {
70          if (scopes.size() > 1) {
71              scopes.remove(JavaScopes.SYSTEM);
72          }
73  
74          String effectiveScope = "";
75  
76          if (scopes.size() == 1) {
77              effectiveScope = scopes.iterator().next();
78          } else if (scopes.contains(JavaScopes.COMPILE)) {
79              effectiveScope = JavaScopes.COMPILE;
80          } else if (scopes.contains(JavaScopes.RUNTIME)) {
81              effectiveScope = JavaScopes.RUNTIME;
82          } else if (scopes.contains(JavaScopes.PROVIDED)) {
83              effectiveScope = JavaScopes.PROVIDED;
84          } else if (scopes.contains(JavaScopes.TEST)) {
85              effectiveScope = JavaScopes.TEST;
86          }
87  
88          return effectiveScope;
89      }
90  }