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