1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.nuxeo.build.maven.graph;
20
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.util.List;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 import org.apache.tools.ant.Project;
29 import org.eclipse.aether.artifact.Artifact;
30 import org.eclipse.aether.graph.Dependency;
31 import org.eclipse.aether.graph.DependencyNode;
32 import org.eclipse.aether.util.artifact.ArtifactIdUtils;
33 import org.eclipse.aether.util.graph.transformer.ConflictResolver;
34 import org.nuxeo.build.ant.AntClient;
35 import org.nuxeo.build.ant.artifact.PrintGraphTask;
36 import org.nuxeo.build.maven.AntBuildMojo;
37
38
39
40
41
42
43
44 public class FlatPrinterDependencyVisitor extends AbstractDependencyVisitor {
45
46 protected int format;
47
48 protected OutputStream output;
49
50 protected Set<String> lines = new TreeSet<>();
51
52
53
54
55 public FlatPrinterDependencyVisitor(OutputStream output, int format,
56 List<String> scopes) {
57 super(scopes);
58 this.output = output;
59 this.format = format;
60 }
61
62 @Override
63 protected void doVisit(DependencyNode node, boolean newNode) {
64 if (newNode) {
65 print(node);
66 }
67 }
68
69
70
71
72 protected void print(DependencyNode node) {
73 if ("pom".equals(node.getArtifact().getExtension())) {
74 return;
75 }
76 Artifact artifact = node.getArtifact();
77 DependencyNode winner = (DependencyNode) node.getData().get(
78 ConflictResolver.NODE_DATA_WINNER);
79 if (winner != null
80 && !ArtifactIdUtils.equalsId(artifact, winner.getArtifact())) {
81 Artifact w = winner.getArtifact();
82 if (ArtifactIdUtils.toVersionlessId(artifact).equals(
83 ArtifactIdUtils.toVersionlessId(w))) {
84 AntClient.getInstance().log(
85 String.format("Ignored conflicting node %s with %s",
86 node, w.getVersion()), Project.MSG_DEBUG);
87 } else {
88 AntClient.getInstance().log(
89 String.format("Ignored conflicting node %s with %s",
90 node, w), Project.MSG_DEBUG);
91 }
92 } else {
93 String toString = toString(node)
94 + System.getProperty("line.separator");
95 lines.add(toString);
96 }
97 }
98
99 public void print() throws UnsupportedEncodingException, IOException {
100 for (String line : lines) {
101 output.write(line.getBytes(AntBuildMojo.getInstance().getEncoding()));
102 }
103 }
104
105
106
107
108
109
110 public String toString(DependencyNode node) {
111 Artifact artifact = node.getArtifact();
112 Dependency dependency = node.getDependency();
113 StringBuilder sb = new StringBuilder();
114 switch (format) {
115 case 1:
116 sb.append(artifact.getFile().getName());
117 sb.append('=');
118
119 case 0:
120 sb.append(artifact.getGroupId());
121 sb.append(':').append(artifact.getArtifactId());
122 sb.append(':').append(artifact.getVersion());
123 sb.append(':').append(artifact.getExtension());
124 sb.append(':').append(artifact.getClassifier());
125 sb.append(':').append(dependency.getScope());
126 break;
127 default:
128 return "Unknown format: " + format + "!";
129 }
130 return sb.toString();
131 }
132
133 }