View Javadoc
1   package org.apache.maven.shared.dependency.graph.internal.maven30;
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.apache.maven.shared.dependency.graph.internal.maven30.ConflictResolver.ConflictContext;
27  import org.apache.maven.shared.dependency.graph.internal.maven30.ConflictResolver.ConflictItem;
28  import org.apache.maven.shared.dependency.graph.internal.maven30.ConflictResolver.ScopeSelector;
29  import org.sonatype.aether.RepositoryException;
30  import org.sonatype.aether.util.artifact.JavaScopes;
31  
32  /**
33   * This class is a copy of their homonymous in the Eclipse Aether library, adapted to work with Sonatype Aether.
34   * 
35   * @author Gabriel Belingueres
36   * @since 3.1.0
37   */
38  public final class JavaScopeSelector
39      extends ScopeSelector
40  {
41  
42      @Override
43      public void selectScope( ConflictContext context )
44          throws RepositoryException
45      {
46          String scope = context.getWinner().getDependency().getScope();
47          if ( !JavaScopes.SYSTEM.equals( scope ) )
48          {
49              scope = chooseEffectiveScope( context.getItems() );
50          }
51          context.setScope( scope );
52      }
53  
54      private String chooseEffectiveScope( Collection<ConflictItem> items )
55      {
56          Set<String> scopes = new HashSet<>();
57          for ( ConflictItem item : items )
58          {
59              if ( item.getDepth() <= 1 )
60              {
61                  return item.getDependency().getScope();
62              }
63              scopes.addAll( item.getScopes() );
64          }
65          return chooseEffectiveScope( scopes );
66      }
67  
68      private String chooseEffectiveScope( Set<String> scopes )
69      {
70          if ( scopes.size() > 1 )
71          {
72              scopes.remove( JavaScopes.SYSTEM );
73          }
74  
75          String effectiveScope = "";
76  
77          if ( scopes.size() == 1 )
78          {
79              effectiveScope = scopes.iterator().next();
80          }
81          else if ( scopes.contains( JavaScopes.COMPILE ) )
82          {
83              effectiveScope = JavaScopes.COMPILE;
84          }
85          else if ( scopes.contains( JavaScopes.RUNTIME ) )
86          {
87              effectiveScope = JavaScopes.RUNTIME;
88          }
89          else if ( scopes.contains( JavaScopes.PROVIDED ) )
90          {
91              effectiveScope = JavaScopes.PROVIDED;
92          }
93          else if ( scopes.contains( JavaScopes.TEST ) )
94          {
95              effectiveScope = JavaScopes.TEST;
96          }
97  
98          return effectiveScope;
99      }
100 
101 
102 }