1 package com.wakaleo.schemaspy;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6 import java.util.Locale;
7 import org.apache.maven.plugins.annotations.LifecyclePhase;
8 import org.apache.maven.plugins.annotations.Mojo;
9 import org.apache.maven.plugins.annotations.Parameter;
10 import org.apache.maven.reporting.AbstractMavenReport;
11 import org.apache.maven.reporting.MavenReportException;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 @Mojo(name = "schemaspy", defaultPhase = LifecyclePhase.SITE)
27 public class SchemaSpyReport extends AbstractMavenReport {
28
29 @Parameter(property = "vizjs", defaultValue = "true")
30 protected boolean vizjs = false;
31
32
33
34
35
36 @Parameter(property = "degree", defaultValue = "2")
37 protected int degree = 2;
38
39
40
41
42
43
44 @Parameter(property = "runOnExecutionRoot", defaultValue = "false")
45 protected boolean runOnExecutionRoot = false;
46
47
48 @Parameter(property = "targetDirectory", defaultValue = "${project.build.directory}")
49 private File targetDirectory;
50
51
52 @Parameter(property = "database", required = true)
53 private String database;
54
55
56 @Parameter(property = "host")
57 private String host;
58
59
60 @Parameter(property = "port")
61 private String port;
62
63
64 @Parameter(property = "databaseType")
65 private String databaseType;
66
67
68 @Parameter(property = "user")
69 private String user;
70
71
72 @Parameter(property = "schema")
73 private String schema;
74
75
76 @Parameter(property = "password")
77 private String password;
78
79
80
81
82
83
84 @Parameter(property = "pathToDrivers")
85 private String pathToDrivers;
86
87
88
89
90
91
92 @Parameter(property = "schemaDescription")
93 private String schemaDescription;
94
95
96
97
98
99
100
101 @Parameter(property = "includeTableNamesRegex")
102 private String includeTableNamesRegex;
103
104
105
106
107
108
109
110
111 @Parameter(property = "excludeColumnNamesRegex")
112 private String excludeColumnNamesRegex;
113
114
115
116
117
118 @Parameter(property = "allowHtmlInComments")
119 private Boolean allowHtmlInComments;
120
121
122
123
124
125
126
127 @Deprecated
128 @Parameter(property = "commentsInitiallyDisplayed")
129 private Boolean commentsInitiallyDisplayed;
130
131
132 @Parameter(property = "noImplied")
133 private Boolean noImplied;
134
135
136 @Parameter(property = "noHtml")
137 private Boolean noHtml;
138
139
140 @Parameter(property = "logLevel")
141 private String logLevel;
142
143
144
145
146
147
148
149
150 @Parameter(property = "useDriverManager")
151 private Boolean useDriverManager;
152
153
154 @Parameter(property = "cssStylesheet")
155 private String cssStylesheet;
156
157
158
159
160
161 @Parameter(property = "singleSignOn")
162 private Boolean singleSignOn;
163
164
165
166
167
168
169
170 @Parameter(property = "lowQuality")
171 private Boolean lowQuality;
172
173
174
175
176
177
178
179 @Parameter(property = "highQuality")
180 private Boolean highQuality;
181
182
183
184
185
186
187 @Parameter(property = "showAllSchemas")
188 private Boolean showAllSchemas;
189
190
191
192
193
194
195 @Parameter(property = "schemas")
196 private String schemas;
197
198
199 @Parameter(property = "noSchema")
200 private Boolean noSchema;
201
202
203 @Parameter(property = "noRows")
204 private Boolean noRows;
205
206
207 @Parameter(property = "noViews")
208 private Boolean noViews;
209
210
211
212
213
214
215
216 @Parameter(property = "connprops")
217 private String connprops;
218
219
220
221
222
223
224 @Deprecated
225 @Parameter(property = "noAds", defaultValue = "true")
226 private Boolean noAds;
227
228
229
230
231
232
233 @Deprecated
234 @Parameter(property = "noLogo", defaultValue = "true")
235 private Boolean noLogo;
236
237 @Parameter(property = "catalog", defaultValue = "%")
238 private String catalog;
239
240
241
242
243
244 private MavenSchemaAnalyzer analyzer;
245
246 protected void setSchemaAnalyzer(final MavenSchemaAnalyzer analyzer) {
247 this.analyzer = analyzer;
248 }
249
250
251
252
253
254
255
256
257 private void addToArguments(
258 final List<String> argList, final String parameter, final Boolean value) {
259 if (value != null && value) {
260 argList.add(parameter);
261 argList.add(String.valueOf(true));
262 }
263 }
264
265
266
267
268
269
270
271
272 private void addFlagToArguments(
273 final List<String> argList, final String parameter, final Boolean value) {
274 if (value != null && value) {
275 argList.add(parameter);
276 }
277 }
278
279
280
281
282
283
284
285
286 private void addToArguments(
287 final List<String> argList, final String parameter, final String value) {
288 if (value != null) {
289 argList.add(parameter);
290 argList.add(value);
291 }
292 }
293
294
295
296
297
298
299
300
301 private void addToArguments(
302 final List<String> argList, final String parameter, final int value, final int defaultValue) {
303 if (value != defaultValue) {
304 argList.add(parameter);
305 argList.add(String.valueOf(value));
306 }
307 }
308
309
310
311
312
313
314
315 @Override
316 protected void executeReport(final Locale locale) throws MavenReportException {
317 File outputDir;
318 if (outputDirectory == null) {
319
320
321 if (targetDirectory == null) {
322 targetDirectory = new File("target");
323 }
324 File siteDir = new File(targetDirectory, "site");
325 outputDir = new File(siteDir, "schemaspy");
326 } else {
327 outputDir = new File(outputDirectory, "schemaspy");
328 }
329 outputDir.mkdirs();
330
331 String schemaSpyDirectory = outputDir.getAbsolutePath();
332 getLog().debug("SchemaSpy output directory: " + schemaSpyDirectory);
333
334 List<String> argList = new ArrayList<>();
335
336 addToArguments(argList, "-dp", pathToDrivers);
337 addToArguments(argList, "-db", database);
338 addToArguments(argList, "-host", host);
339 addToArguments(argList, "-port", port);
340 addToArguments(argList, "-t", databaseType);
341 addToArguments(argList, "-u", user);
342 addToArguments(argList, "-p", password);
343 if (null != schema && schema.contains(",")) {
344 addToArguments(argList, "-schemas", schema);
345 } else {
346 addToArguments(argList, "-s", schema);
347 }
348 addToArguments(argList, "-o", schemaSpyDirectory);
349 addToArguments(argList, "-desc", schemaDescription);
350 addToArguments(argList, "-i", includeTableNamesRegex);
351 addToArguments(argList, "-x", excludeColumnNamesRegex);
352 addFlagToArguments(argList, "-ahic", allowHtmlInComments);
353 addFlagToArguments(argList, "-noimplied", noImplied);
354 addFlagToArguments(argList, "-nohtml", noHtml);
355 addToArguments(argList, "-loglevel", logLevel);
356 addFlagToArguments(argList, "-norows", noRows);
357 addFlagToArguments(argList, "-noviews", noViews);
358 addFlagToArguments(argList, "-noschema", noSchema);
359 addFlagToArguments(argList, "-all", showAllSchemas);
360 addToArguments(argList, "-schemas", schemas);
361 addToArguments(argList, "-useDriverManager", useDriverManager);
362 addToArguments(argList, "-css", cssStylesheet);
363 addFlagToArguments(argList, "-sso", singleSignOn);
364 addFlagToArguments(argList, "-lq", lowQuality);
365 addFlagToArguments(argList, "-hq", highQuality);
366 addToArguments(argList, "-connprops", connprops);
367 addFlagToArguments(argList, "-cid", commentsInitiallyDisplayed);
368 addFlagToArguments(argList, "-noads", noAds);
369 addFlagToArguments(argList, "-nologo", noLogo);
370 addToArguments(argList, "-cat", catalog);
371 addFlagToArguments(argList, "-vizjs", vizjs);
372 addToArguments(argList, "-degree", degree, 2);
373 if (getLog().isDebugEnabled()) {
374 addFlagToArguments(argList, "-debug", true);
375 }
376 getLog().debug("SchemaSpy arguments: " + argList);
377
378 try {
379 if (analyzer == null) {
380 analyzer = new MavenSchemaAnalyzer();
381 }
382 analyzer.applyConfiguration(argList);
383 analyzer.analyze();
384 } catch (Exception e) {
385 throw new MavenReportException(e.getMessage(), e);
386 }
387 }
388
389 @Override
390 public boolean canGenerateReport() {
391 return !runOnExecutionRoot || project.isExecutionRoot();
392 }
393
394 @Override
395 public String getDescription(final Locale locale) {
396 return "SchemaSpy database documentation";
397 }
398
399 @Override
400 public String getName(final Locale locale) {
401 return "SchemaSpy";
402 }
403
404
405
406
407
408 @Override
409 @Deprecated (since = "4.0.0")
410 public String getOutputName() {
411 return this.getOutputPath();
412 }
413
414 @Override
415 public String getOutputPath() {
416 return "schemaspy/index";
417 }
418
419
420
421
422
423
424
425 @Override
426 public boolean isExternalReport() {
427 return true;
428 }
429 }