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.apache.maven.repository.internal.scopes;
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.graph.transformer.ConflictResolver;
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 MavenDependencyScopes}.
33   * In general, this selector picks the widest scope present among conflicting dependencies where e.g. "compile" is
34   * wider than "runtime" which is wider than "test". If however a direct dependency is involved, its scope is selected.
35   *
36   * @since 4.0.0
37   */
38  public final class MavenScopeSelector extends ScopeSelector {
39  
40      public MavenScopeSelector() {}
41  
42      @Override
43      public void selectScope(ConflictContext context) throws RepositoryException {
44          String scope = context.getWinner().getDependency().getScope();
45          if (!MavenDependencyScopes.SYSTEM.equals(scope)) {
46              scope = chooseEffectiveScope(context.getItems());
47          }
48          context.setScope(scope);
49      }
50  
51      private String chooseEffectiveScope(Collection<ConflictItem> items) {
52          Set<String> scopes = new HashSet<>();
53          for (ConflictItem item : items) {
54              if (item.getDepth() <= 1) {
55                  return item.getDependency().getScope();
56              }
57              scopes.addAll(item.getScopes());
58          }
59          return chooseEffectiveScope(scopes);
60      }
61  
62      private String chooseEffectiveScope(Set<String> scopes) {
63          if (scopes.size() > 1) {
64              scopes.remove(MavenDependencyScopes.SYSTEM);
65          }
66  
67          String effectiveScope = "";
68  
69          if (scopes.size() == 1) {
70              effectiveScope = scopes.iterator().next();
71          } else if (scopes.contains(MavenDependencyScopes.COMPILE)) {
72              effectiveScope = MavenDependencyScopes.COMPILE;
73          } else if (scopes.contains(MavenDependencyScopes.RUNTIME)) {
74              effectiveScope = MavenDependencyScopes.RUNTIME;
75          } else if (scopes.contains(MavenDependencyScopes.PROVIDED)) {
76              effectiveScope = MavenDependencyScopes.PROVIDED;
77          } else if (scopes.contains(MavenDependencyScopes.TEST)) {
78              effectiveScope = MavenDependencyScopes.TEST;
79          }
80  
81          return effectiveScope;
82      }
83  }