1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.resolver.examples.util;
20
21 import java.io.PrintStream;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.graph.Dependency;
28 import org.eclipse.aether.graph.DependencyNode;
29 import org.eclipse.aether.graph.DependencyVisitor;
30 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
31 import org.eclipse.aether.util.graph.manager.DependencyManagerUtils;
32 import org.eclipse.aether.util.graph.transformer.ConflictResolver;
33
34
35
36
37 public class ConsoleDependencyGraphDumper implements DependencyVisitor {
38
39 private final PrintStream out;
40
41 private final List<ChildInfo> childInfos = new ArrayList<>();
42
43 public ConsoleDependencyGraphDumper() {
44 this(null);
45 }
46
47 public ConsoleDependencyGraphDumper(PrintStream out) {
48 this.out = (out != null) ? out : System.out;
49 }
50
51 public boolean visitEnter(DependencyNode node) {
52 out.println(formatIndentation() + formatNode(node));
53 childInfos.add(new ChildInfo(node.getChildren().size()));
54 return true;
55 }
56
57 private String formatIndentation() {
58 StringBuilder buffer = new StringBuilder(128);
59 for (Iterator<ChildInfo> it = childInfos.iterator(); it.hasNext(); ) {
60 buffer.append(it.next().formatIndentation(!it.hasNext()));
61 }
62 return buffer.toString();
63 }
64
65 private String formatNode(DependencyNode node) {
66 StringBuilder buffer = new StringBuilder(128);
67 Artifact a = node.getArtifact();
68 Dependency d = node.getDependency();
69 buffer.append(a);
70 if (d != null && d.getScope().length() > 0) {
71 buffer.append(" [").append(d.getScope());
72 if (d.isOptional()) {
73 buffer.append(", optional");
74 }
75 buffer.append("]");
76 }
77 String premanaged = DependencyManagerUtils.getPremanagedVersion(node);
78 if (premanaged != null && !premanaged.equals(a.getBaseVersion())) {
79 buffer.append(" (version managed from ").append(premanaged).append(")");
80 }
81
82 premanaged = DependencyManagerUtils.getPremanagedScope(node);
83 if (premanaged != null && !premanaged.equals(d.getScope())) {
84 buffer.append(" (scope managed from ").append(premanaged).append(")");
85 }
86 DependencyNode winner = (DependencyNode) node.getData().get(ConflictResolver.NODE_DATA_WINNER);
87 if (winner != null && !ArtifactIdUtils.equalsId(a, winner.getArtifact())) {
88 Artifact w = winner.getArtifact();
89 buffer.append(" (conflicts with ");
90 if (ArtifactIdUtils.toVersionlessId(a).equals(ArtifactIdUtils.toVersionlessId(w))) {
91 buffer.append(w.getVersion());
92 } else {
93 buffer.append(w);
94 }
95 buffer.append(")");
96 }
97 return buffer.toString();
98 }
99
100 public boolean visitLeave(DependencyNode node) {
101 if (!childInfos.isEmpty()) {
102 childInfos.remove(childInfos.size() - 1);
103 }
104 if (!childInfos.isEmpty()) {
105 childInfos.get(childInfos.size() - 1).index++;
106 }
107 return true;
108 }
109
110 private static class ChildInfo {
111
112 final int count;
113
114 int index;
115
116 ChildInfo(int count) {
117 this.count = count;
118 }
119
120 public String formatIndentation(boolean end) {
121 boolean last = index + 1 >= count;
122 if (end) {
123 return last ? "\\- " : "+- ";
124 }
125 return last ? " " : "| ";
126 }
127 }
128 }