1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.nuxeo.build.ant.artifact;
18
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.StringTokenizer;
26
27 import org.apache.commons.io.IOUtils;
28 import org.apache.tools.ant.BuildException;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.Task;
31 import org.eclipse.aether.util.artifact.JavaScopes;
32 import org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor;
33 import org.nuxeo.build.maven.AntBuildMojo;
34 import org.nuxeo.build.maven.filter.TrueFilter;
35 import org.nuxeo.build.maven.graph.FlatPrinterDependencyVisitor;
36 import org.nuxeo.build.maven.graph.Graph;
37 import org.nuxeo.build.maven.graph.Node;
38 import org.nuxeo.build.maven.graph.TreePrinterDependencyVisitor;
39
40
41
42
43 public class PrintGraphTask extends Task {
44
45 private String output;
46
47 private String mode = PrintGraphTask.MODE_TREE;
48
49
50
51
52 public static final int FORMAT_GAV = 0;
53
54
55
56
57 public static final int FORMAT_KV_F_GAV = 1;
58
59 private int format = FORMAT_GAV;
60
61 private boolean append = false;
62
63 private String source;
64
65 private List<String> scopes = null;
66
67
68
69
70
71
72 public static final String MODE_SDK = "sdk";
73
74
75
76
77
78
79 public static final String MODE_FLAT = "flat";
80
81
82
83
84 public static final String MODE_TREE = "tree";
85
86 @Override
87 public void execute() throws BuildException {
88
89 if (scopes == null) {
90
91 if (MODE_TREE.equals(mode)) {
92 scopes = Arrays.asList(new String[] { JavaScopes.COMPILE,
93 JavaScopes.PROVIDED, JavaScopes.RUNTIME,
94 JavaScopes.SYSTEM, JavaScopes.TEST });
95 } else {
96 scopes = Arrays.asList(new String[] { JavaScopes.COMPILE,
97 JavaScopes.RUNTIME, JavaScopes.SYSTEM });
98 }
99 }
100 List<Node> roots;
101 if (source != null) {
102
103
104
105
106
107 roots = new ArrayList<>();
108 Graph graph = new Graph();
109 roots.add(graph.addRootNode(source));
110 graph.resolveDependencies(new TrueFilter(), 0);
111 } else {
112 roots = AntBuildMojo.getInstance().getGraph().getRoots();
113 }
114 OutputStream out = System.out;
115 try {
116 if (output != null) {
117 out = new FileOutputStream(output, append);
118 }
119
120
121
122
123 if (MODE_TREE.equals(mode)) {
124 TreePrinterDependencyVisitor pdv = new TreePrinterDependencyVisitor(
125 out, format, scopes, roots);
126 for (Node node : roots) {
127 log("Visiting " + node, Project.MSG_DEBUG);
128
129 CloningDependencyVisitor cdv = new CloningDependencyVisitor();
130 node.accept(cdv);
131 cdv.getRootNode().accept(pdv);
132 }
133 } else {
134 FlatPrinterDependencyVisitor pdv = new FlatPrinterDependencyVisitor(
135 out, format, scopes);
136
137 pdv.addIgnores(roots);
138 for (Node node : roots) {
139 log("Visiting " + node, Project.MSG_DEBUG);
140 node.accept(pdv);
141 }
142 pdv.print();
143 }
144 } catch (IOException e) {
145 throw new BuildException(e);
146 } finally {
147 IOUtils.closeQuietly(out);
148 }
149 }
150
151 public void setOutput(String output) {
152 this.output = output;
153 }
154
155
156
157
158 public void setMode(String mode) {
159 if (MODE_SDK.equals(mode)) {
160 this.format = FORMAT_KV_F_GAV;
161 }
162 this.mode = mode;
163 }
164
165
166
167
168
169
170
171 public void setFormat(int format) {
172 this.format = format;
173 }
174
175
176
177
178
179
180
181 public void setAppend(boolean append) {
182 this.append = append;
183 }
184
185
186
187
188
189
190
191 public void setSource(String source) {
192 this.source = source;
193 }
194
195
196
197
198
199
200 public void setScopes(String scopes) {
201 StringTokenizer st = new StringTokenizer(scopes, ",");
202 this.scopes = new ArrayList<>();
203 while (st.hasMoreTokens()) {
204 this.scopes.add(st.nextToken());
205 }
206 }
207
208 }