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.Arrays;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  import org.apache.maven.shared.dependency.graph.internal.maven30.ConflictResolver.ConflictContext;
27  import org.apache.maven.shared.dependency.graph.internal.maven30.ConflictResolver.ScopeSelector;
28  import org.sonatype.aether.RepositoryException;
29  
30  /**
31   * A JavaScopeSelector that keeps track of reduced scopes
32   * 
33   */
34  public class VerboseJavaScopeSelector extends ScopeSelector
35  {
36      public static final String REDUCED_SCOPE = "REDUCED_SCOPE";
37      
38      private final ScopeSelector scopeSelector = new JavaScopeSelector();
39  
40      @Override
41      public void selectScope( ConflictContext context )
42          throws RepositoryException
43      {
44          scopeSelector.selectScope( context );
45          
46          context.getItems().stream()
47              .flatMap( i -> i.getScopes().stream() )
48              .distinct()
49              .max( new ScopeComparator() )
50              .filter( s -> s != context.getScope() )
51              .ifPresent( s -> context.getWinner().getNode().setData( REDUCED_SCOPE, s ) );
52      }
53      
54      static class ScopeComparator implements Comparator<String>
55      {
56          List<String> orderedScopes = Arrays.asList( "compile", "runtime", "provided", "test" );
57  
58          @Override
59          public int compare( String lhs, String rhs )
60          {
61              return orderedScopes.indexOf( rhs ) - orderedScopes.indexOf( lhs );
62          }
63      }    
64  }