1 package org.eclipse.aether.util.graph.transformer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37 public final class JavaScopeSelector
38 extends ScopeSelector
39 {
40
41
42
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 }