1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36
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 }