View Javadoc
1   package org.eclipse.aether.util.graph.transformer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Collection;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.eclipse.aether.RepositoryException;
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  public final class JavaScopeSelector
38      extends ScopeSelector
39  {
40  
41      /**
42       * Creates a new instance of this scope selector.
43       */
44      public JavaScopeSelector()
45      {
46      }
47  
48      @Override
49      public void selectScope( ConflictContext context )
50          throws RepositoryException
51      {
52          String scope = context.getWinner().getDependency().getScope();
53          if ( !JavaScopes.SYSTEM.equals( scope ) )
54          {
55              scope = chooseEffectiveScope( context.getItems() );
56          }
57          context.setScope( scope );
58      }
59  
60      private String chooseEffectiveScope( Collection<ConflictItem> items )
61      {
62          Set<String> scopes = new HashSet<>();
63          for ( ConflictItem item : items )
64          {
65              if ( item.getDepth() <= 1 )
66              {
67                  return item.getDependency().getScope();
68              }
69              scopes.addAll( item.getScopes() );
70          }
71          return chooseEffectiveScope( scopes );
72      }
73  
74      private String chooseEffectiveScope( Set<String> scopes )
75      {
76          if ( scopes.size() > 1 )
77          {
78              scopes.remove( JavaScopes.SYSTEM );
79          }
80  
81          String effectiveScope = "";
82  
83          if ( scopes.size() == 1 )
84          {
85              effectiveScope = scopes.iterator().next();
86          }
87          else if ( scopes.contains( JavaScopes.COMPILE ) )
88          {
89              effectiveScope = JavaScopes.COMPILE;
90          }
91          else if ( scopes.contains( JavaScopes.RUNTIME ) )
92          {
93              effectiveScope = JavaScopes.RUNTIME;
94          }
95          else if ( scopes.contains( JavaScopes.PROVIDED ) )
96          {
97              effectiveScope = JavaScopes.PROVIDED;
98          }
99          else if ( scopes.contains( JavaScopes.TEST ) )
100         {
101             effectiveScope = JavaScopes.TEST;
102         }
103 
104         return effectiveScope;
105     }
106 
107 }