1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.collection;
20
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.LinkedHashSet;
25 import java.util.List;
26
27 import org.eclipse.aether.RepositoryException;
28 import org.eclipse.aether.artifact.Artifact;
29 import org.eclipse.aether.graph.DependencyNode;
30 import org.eclipse.aether.version.VersionConstraint;
31
32 import static java.util.Objects.requireNonNull;
33
34
35
36
37 public class UnsolvableVersionConflictException extends RepositoryException {
38
39 private final transient Collection<String> versions;
40
41 private final transient Collection<? extends List<? extends DependencyNode>> paths;
42
43
44
45
46
47
48
49 @Deprecated
50 public UnsolvableVersionConflictException(Collection<? extends List<? extends DependencyNode>> paths) {
51 this("Unsolvable hard constraint combination", paths);
52 }
53
54
55
56
57
58
59
60
61
62
63 public UnsolvableVersionConflictException(
64 String message, Collection<? extends List<? extends DependencyNode>> paths) {
65 super(requireNonNull(message, "message") + "; Could not resolve version conflict among " + toPaths(paths));
66 if (paths == null) {
67 this.paths = Collections.emptyList();
68 this.versions = Collections.emptyList();
69 } else {
70 this.paths = paths;
71 this.versions = new LinkedHashSet<>();
72 for (List<? extends DependencyNode> path : paths) {
73 VersionConstraint constraint = path.get(path.size() - 1).getVersionConstraint();
74 if (constraint != null && constraint.getRange() != null) {
75 versions.add(constraint.toString());
76 }
77 }
78 }
79 }
80
81 private static String toPaths(Collection<? extends List<? extends DependencyNode>> paths) {
82 String result = "";
83
84 if (paths != null) {
85 Collection<String> strings = new LinkedHashSet<>();
86
87 for (List<? extends DependencyNode> path : paths) {
88 strings.add(toPath(path));
89 }
90
91 result = strings.toString();
92 }
93
94 return result;
95 }
96
97 private static String toPath(List<? extends DependencyNode> path) {
98 StringBuilder buffer = new StringBuilder(256);
99
100 for (Iterator<? extends DependencyNode> it = path.iterator(); it.hasNext(); ) {
101 DependencyNode node = it.next();
102 if (node.getDependency() == null) {
103 continue;
104 }
105
106 Artifact artifact = node.getDependency().getArtifact();
107 buffer.append(artifact.getGroupId());
108 buffer.append(':').append(artifact.getArtifactId());
109 buffer.append(':').append(artifact.getExtension());
110 if (!artifact.getClassifier().isEmpty()) {
111 buffer.append(':').append(artifact.getClassifier());
112 }
113 buffer.append(':').append(node.getVersionConstraint());
114
115 if (it.hasNext()) {
116 buffer.append(" -> ");
117 }
118 }
119
120 return buffer.toString();
121 }
122
123
124
125
126
127
128 public Collection<? extends List<? extends DependencyNode>> getPaths() {
129 return paths;
130 }
131
132
133
134
135
136
137 public Collection<String> getVersions() {
138 return versions;
139 }
140 }