1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.rat.plugin;
21
22 import org.apache.commons.cli.Option;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.plugin.AbstractMojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26 import org.apache.rat.commandline.Arg;
27 import org.apache.rat.DeprecationReporter;
28 import org.apache.rat.utils.CasedString;
29 import org.apache.rat.utils.DefaultLog;
30 import org.apache.rat.utils.Log;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.stream.Collectors;
40
41
42
43
44
45
46 public abstract class BaseRatMojo extends AbstractMojo {
47
48 private static final Map<String, String> xlateName = new HashMap<>();
49
50 private static final List<String> unsupportedArgs = new ArrayList<>();
51
52 private static final Map<String, String> deprecatedArgs = new HashMap<>();
53
54 static {
55 xlateName.put("addLicense", "add-license");
56 unsupportedArgs.add("help");
57 unsupportedArgs.add("dir");
58 unsupportedArgs.add("log-level");
59 deprecatedArgs.put("copyright", "Use of deprecated option 'copyright'. Deprecated for removal since 0.17: Use <editCopyright> instead.");
60 deprecatedArgs.put("force", "Use of deprecated option 'force'. Deprecated for removal since 0.17: Use <editOverwrite> instead.");
61 deprecatedArgs.put("addLicense", "Use of deprecated option 'addLicense'. Deprecated for removal since 0.17: Use <editLicense> instead.");
62 deprecatedArgs.put("licenses", "Use of deprecated option 'licenses'. Deprecated for removal since 0.17: Use <config> instead.");
63 deprecatedArgs.put("no-default-licenses", "Use of deprecated option 'noDefaultLicenses'. Deprecated for removal since 0.17: Use <configurationNoDefaults> instead.");
64 deprecatedArgs.put("exclude", "Use of deprecated option 'exclude'. Deprecated for removal since 0.17: Use <inputExclude> instead.");
65 deprecatedArgs.put("exclude-file", "Use of deprecated option 'excludeFile'. Deprecated for removal since 0.17: Use <inputExcludeFile> instead.");
66 deprecatedArgs.put("include", "Use of deprecated option 'include'. Deprecated for removal since 0.17: Use <inputInclude> instead.");
67 deprecatedArgs.put("includes-file", "Use of deprecated option 'includesFile'. Deprecated for removal since 0.17: Use <inputIncludeFile> instead.");
68 deprecatedArgs.put("scan-hidden-directories", "Use of deprecated option 'scanHiddenDirectories'. Deprecated for removal since 0.17: Use <inputIncludeStd> with 'HIDDEN_DIR' argument instead.");
69 deprecatedArgs.put("stylesheet", "Use of deprecated option 'stylesheet'. Deprecated for removal since 0.17: Use <outputStyle> instead.");
70 deprecatedArgs.put("xml", "Use of deprecated option 'xml'. Deprecated for removal since 0.17: Use <outputStyle> with the 'xml' argument instead.");
71 deprecatedArgs.put("list-licenses", "Use of deprecated option 'listLicenses'. Deprecated for removal since 0.17: Use <outputLicenses> instead.");
72 deprecatedArgs.put("list-families", "Use of deprecated option 'listFamilies'. Deprecated for removal since 0.17: Use <outputFamilies> instead.");
73 deprecatedArgs.put("out", "Use of deprecated option 'out'. Deprecated for removal since 0.17: Use <outputFile> instead.");
74 }
75
76
77
78
79
80
81
82 public static String createName(String longOpt) {
83 String name = xlateName.get(longOpt);
84 return name != null ? name : new CasedString(CasedString.StringCase.KEBAB, longOpt).toCase(CasedString.StringCase.CAMEL);
85 }
86
87
88
89
90
91
92 public static String toKebabForm(String camelCase) {
93 return new CasedString(CasedString.StringCase.CAMEL, camelCase).toCase(CasedString.StringCase.KEBAB);
94 }
95
96
97
98
99
100 public static List<String> unsupportedArgs() {
101 return Collections.unmodifiableList(unsupportedArgs);
102 }
103
104
105
106
107
108
109 private static void setDeprecationReporter() {
110 DeprecationReporter.setLogReporter(opt -> {
111 String msg = deprecatedArgs.get(argsKey(opt));
112 if (msg == null) {
113 DeprecationReporter.getDefault().accept(opt);
114 } else {
115 DefaultLog.getInstance().warn(msg);
116 }
117 });
118 }
119
120 private static String argsKey(Option opt) {
121 return StringUtils.defaultIfEmpty(opt.getLongOpt(), opt.getKey());
122 }
123
124
125
126
127 protected final Map<String, List<String>> args = new HashMap<>();
128
129
130
131
132
133 protected List<String> args() {
134 List<String> result = new ArrayList<>();
135 for (Map.Entry<String, List<String>> entry : args.entrySet()) {
136 result.add("--" + entry.getKey());
137 result.addAll(entry.getValue().stream().filter(Objects::nonNull).collect(Collectors.toList()));
138 }
139 return result;
140 }
141
142 private boolean validateSet(String key) {
143 Arg arg = Arg.findArg(key);
144 if (arg != null) {
145 Option opt = arg.find(key);
146 Option main = arg.option();
147 if (opt.isDeprecated()) {
148 args.remove(argsKey(main));
149
150 return true;
151 }
152
153 for (Option o : arg.group().getOptions()) {
154 if (!o.equals(main)) {
155 if (args.containsKey(argsKey(o))) {
156 return false;
157 }
158 }
159 }
160 return true;
161 }
162 return false;
163 }
164
165
166
167
168
169
170
171 protected void setArg(String key, String value) {
172 if (value == null || StringUtils.isNotBlank(value)) {
173 if (validateSet(key)) {
174 List<String> values = new ArrayList<>();
175 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
176 DefaultLog.getInstance().debug(String.format("Setting %s to '%s'", key, value));
177 }
178 values.add(value);
179 args.put(key, values);
180 }
181 }
182 }
183
184
185
186
187
188
189 public List<String> getArg(String key) {
190 return args.get(key);
191 }
192
193
194
195
196
197
198
199
200 protected void addArg(String key, String[] value) {
201 List<String> newValues = Arrays.stream(value).filter(StringUtils::isNotBlank).collect(Collectors.toList());
202 if (!newValues.isEmpty()) {
203 if (validateSet(key)) {
204 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
205 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
206 }
207 List<String> values = args.get(key);
208 if (values == null) {
209 values = new ArrayList<>();
210 args.put(key, values);
211 }
212 values.addAll(newValues);
213 }
214 }
215 }
216
217
218
219
220
221
222
223 protected void addArg(String key, String value) {
224 if (StringUtils.isNotBlank(value)) {
225 if (validateSet(key)) {
226 List<String> values = args.get(key);
227 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
228 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
229 }
230 if (values == null) {
231 values = new ArrayList<>();
232 args.put(key, values);
233 }
234 values.add(value);
235 }
236 }
237 }
238
239
240
241
242
243 protected void removeArg(String key) {
244 args.remove(key);
245 }
246
247
248
249 protected BaseRatMojo() {
250 setDeprecationReporter();
251 }
252
253
254
255
256
257
258
259
260
261 @Deprecated
262 @Parameter(property = "rat.Copyright")
263 public void setCopyright(String copyright) {
264 setArg("copyright", copyright);
265 }
266
267
268
269
270 @Parameter(property = "rat.EditCopyright")
271 public void setEditCopyright(String editCopyright) {
272 setArg("edit-copyright", editCopyright);
273 }
274
275
276
277
278
279 @Deprecated
280 @Parameter(property = "rat.Force")
281 public void setForce(boolean force) {
282 if (force) {
283 setArg("force", null);
284 } else {
285 removeArg("force");
286 }
287 }
288
289
290
291
292 @Parameter(property = "rat.EditOverwrite")
293 public void setEditOverwrite(boolean editOverwrite) {
294 if (editOverwrite) {
295 setArg("edit-overwrite", null);
296 } else {
297 removeArg("edit-overwrite");
298 }
299 }
300
301
302
303
304
305 @Deprecated
306 @Parameter(property = "rat.AddLicense")
307 public void setAddLicense(boolean addLicense) {
308 if (addLicense) {
309 setArg("addLicense", null);
310 } else {
311 removeArg("addLicense");
312 }
313 }
314
315
316
317
318 @Parameter(property = "rat.EditLicense")
319 public void setEditLicense(boolean editLicense) {
320 if (editLicense) {
321 setArg("edit-license", null);
322 } else {
323 removeArg("edit-license");
324 }
325 }
326
327
328
329
330 @Parameter
331 public void setConfigs(String[] config) {
332 addArg("config", config);
333 }
334
335
336
337
338 @Parameter
339 public void setConfig(String config) {
340 addArg("config", config);
341 }
342
343
344
345
346
347 @Deprecated
348 @Parameter
349 public void setLicenses(String[] licenses) {
350 addArg("licenses", licenses);
351 }
352
353
354
355
356
357 @Deprecated
358 @Parameter
359 public void setLicenses(String licenses) {
360 addArg("licenses", licenses);
361 }
362
363
364
365
366 @Parameter(property = "rat.ConfigurationNoDefaults")
367 public void setConfigurationNoDefaults(boolean configurationNoDefaults) {
368 if (configurationNoDefaults) {
369 setArg("configuration-no-defaults", null);
370 } else {
371 removeArg("configuration-no-defaults");
372 }
373 }
374
375
376
377
378
379 @Deprecated
380 @Parameter(property = "rat.NoDefaultLicenses")
381 public void setNoDefaultLicenses(boolean noDefaultLicenses) {
382 if (noDefaultLicenses) {
383 setArg("no-default-licenses", null);
384 } else {
385 removeArg("no-default-licenses");
386 }
387 }
388
389
390
391
392 @Parameter(property = "rat.LicensesApproved")
393 public void setLicensesApproved(String licensesApproved) {
394 setArg("licenses-approved", licensesApproved);
395 }
396
397
398
399
400 @Parameter(property = "rat.LicensesApprovedFile")
401 public void setLicensesApprovedFile(String licensesApprovedFile) {
402 setArg("licenses-approved-file", licensesApprovedFile);
403 }
404
405
406
407
408 @Parameter(property = "rat.LicenseFamiliesApproved")
409 public void setLicenseFamiliesApproved(String licenseFamiliesApproved) {
410 setArg("license-families-approved", licenseFamiliesApproved);
411 }
412
413
414
415
416 @Parameter(property = "rat.LicenseFamiliesApprovedFile")
417 public void setLicenseFamiliesApprovedFile(String licenseFamiliesApprovedFile) {
418 setArg("license-families-approved-file", licenseFamiliesApprovedFile);
419 }
420
421
422
423
424 @Parameter(property = "rat.LicensesDenied")
425 public void setLicensesDenied(String licensesDenied) {
426 setArg("licenses-denied", licensesDenied);
427 }
428
429
430
431
432 @Parameter(property = "rat.LicensesDeniedFile")
433 public void setLicensesDeniedFile(String licensesDeniedFile) {
434 setArg("licenses-denied-file", licensesDeniedFile);
435 }
436
437
438
439
440 @Parameter(property = "rat.LicenseFamiliesDenied")
441 public void setLicenseFamiliesDenied(String licenseFamiliesDenied) {
442 setArg("license-families-denied", licenseFamiliesDenied);
443 }
444
445
446
447
448 @Parameter(property = "rat.LicenseFamiliesDeniedFile")
449 public void setLicenseFamiliesDeniedFile(String licenseFamiliesDeniedFile) {
450 setArg("license-families-denied-file", licenseFamiliesDeniedFile);
451 }
452
453
454
455
456 @Parameter
457 public void setCounterMaxs(String[] counterMax) {
458 addArg("counter-max", counterMax);
459 }
460
461
462
463
464 @Parameter
465 public void setCounterMax(String counterMax) {
466 addArg("counter-max", counterMax);
467 }
468
469
470
471
472 @Parameter
473 public void setCounterMins(String[] counterMin) {
474 addArg("counter-min", counterMin);
475 }
476
477
478
479
480 @Parameter
481 public void setCounterMin(String counterMin) {
482 addArg("counter-min", counterMin);
483 }
484
485
486
487
488 @Parameter
489 public void setInputSources(String[] inputSource) {
490 addArg("input-source", inputSource);
491 }
492
493
494
495
496 @Parameter
497 public void setInputSource(String inputSource) {
498 addArg("input-source", inputSource);
499 }
500
501
502
503
504
505 @Deprecated
506 @Parameter
507 public void setExcludes(String[] exclude) {
508 addArg("exclude", exclude);
509 }
510
511
512
513
514
515 @Deprecated
516 @Parameter
517 public void setExclude(String exclude) {
518 addArg("exclude", exclude);
519 }
520
521
522
523
524 @Parameter
525 public void setInputExcludes(String[] inputExclude) {
526 addArg("input-exclude", inputExclude);
527 }
528
529
530
531
532 @Parameter
533 public void setInputExclude(String inputExclude) {
534 addArg("input-exclude", inputExclude);
535 }
536
537
538
539
540
541 @Deprecated
542 @Parameter(property = "rat.ExcludeFile")
543 public void setExcludeFile(String excludeFile) {
544 setArg("exclude-file", excludeFile);
545 }
546
547
548
549
550 @Parameter(property = "rat.InputExcludeFile")
551 public void setInputExcludeFile(String inputExcludeFile) {
552 setArg("input-exclude-file", inputExcludeFile);
553 }
554
555
556
557
558 @Parameter
559 public void setInputExcludeStds(String[] inputExcludeStd) {
560 addArg("input-exclude-std", inputExcludeStd);
561 }
562
563
564
565
566 @Parameter
567 public void setInputExcludeStd(String inputExcludeStd) {
568 addArg("input-exclude-std", inputExcludeStd);
569 }
570
571
572
573
574 @Parameter(property = "rat.InputExcludeSize")
575 public void setInputExcludeSize(String inputExcludeSize) {
576 setArg("input-exclude-size", inputExcludeSize);
577 }
578
579
580
581
582 @Parameter
583 public void setInputIncludes(String[] inputInclude) {
584 addArg("input-include", inputInclude);
585 }
586
587
588
589
590 @Parameter
591 public void setInputInclude(String inputInclude) {
592 addArg("input-include", inputInclude);
593 }
594
595
596
597
598
599 @Deprecated
600 @Parameter
601 public void setIncludes(String[] include) {
602 addArg("include", include);
603 }
604
605
606
607
608
609 @Deprecated
610 @Parameter
611 public void setInclude(String include) {
612 addArg("include", include);
613 }
614
615
616
617
618 @Parameter(property = "rat.InputIncludeFile")
619 public void setInputIncludeFile(String inputIncludeFile) {
620 setArg("input-include-file", inputIncludeFile);
621 }
622
623
624
625
626
627 @Deprecated
628 @Parameter(property = "rat.IncludesFile")
629 public void setIncludesFile(String includesFile) {
630 setArg("includes-file", includesFile);
631 }
632
633
634
635
636 @Parameter
637 public void setInputIncludeStds(String[] inputIncludeStd) {
638 addArg("input-include-std", inputIncludeStd);
639 }
640
641
642
643
644 @Parameter
645 public void setInputIncludeStd(String inputIncludeStd) {
646 addArg("input-include-std", inputIncludeStd);
647 }
648
649
650
651
652
653 @Deprecated
654 @Parameter(property = "rat.ScanHiddenDirectories")
655 public void setScanHiddenDirectories(boolean scanHiddenDirectories) {
656 if (scanHiddenDirectories) {
657 setArg("scan-hidden-directories", null);
658 } else {
659 removeArg("scan-hidden-directories");
660 }
661 }
662
663
664
665
666 @Parameter
667 public void setInputExcludeParsedScms(String[] inputExcludeParsedScm) {
668 addArg("input-exclude-parsed-scm", inputExcludeParsedScm);
669 }
670
671
672
673
674 @Parameter
675 public void setInputExcludeParsedScm(String inputExcludeParsedScm) {
676 addArg("input-exclude-parsed-scm", inputExcludeParsedScm);
677 }
678
679
680
681
682 @Parameter(property = "rat.OutputStyle")
683 public void setOutputStyle(String outputStyle) {
684 setArg("output-style", outputStyle);
685 }
686
687
688
689
690
691 @Deprecated
692 @Parameter(property = "rat.Stylesheet")
693 public void setStylesheet(String stylesheet) {
694 setArg("stylesheet", stylesheet);
695 }
696
697
698
699
700
701 @Deprecated
702 @Parameter(property = "rat.Xml")
703 public void setXml(boolean xml) {
704 if (xml) {
705 setArg("xml", null);
706 } else {
707 removeArg("xml");
708 }
709 }
710
711
712
713
714 @Parameter(property = "rat.OutputLicenses", defaultValue = "NONE")
715 public void setOutputLicenses(String outputLicenses) {
716 setArg("output-licenses", outputLicenses);
717 }
718
719
720
721
722
723 @Deprecated
724 @Parameter(property = "rat.ListLicenses")
725 public void setListLicenses(String listLicenses) {
726 setArg("list-licenses", listLicenses);
727 }
728
729
730
731
732 @Parameter(property = "rat.OutputFamilies", defaultValue = "NONE")
733 public void setOutputFamilies(String outputFamilies) {
734 setArg("output-families", outputFamilies);
735 }
736
737
738
739
740
741 @Deprecated
742 @Parameter(property = "rat.ListFamilies")
743 public void setListFamilies(String listFamilies) {
744 setArg("list-families", listFamilies);
745 }
746
747
748
749
750 @Parameter(property = "rat.DryRun")
751 public void setDryRun(boolean dryRun) {
752 if (dryRun) {
753 setArg("dry-run", null);
754 } else {
755 removeArg("dry-run");
756 }
757 }
758
759
760
761
762
763 @Deprecated
764 @Parameter(property = "rat.Out")
765 public void setOut(String out) {
766 setArg("out", out);
767 }
768
769
770
771
772 @Parameter(property = "rat.OutputFile", defaultValue = "${project.build.directory}/rat.txt")
773 public void setOutputFile(String outputFile) {
774 setArg("output-file", outputFile);
775 }
776
777
778
779
780 @Parameter(property = "rat.OutputArchive", defaultValue = "NOTIFICATION")
781 public void setOutputArchive(String outputArchive) {
782 setArg("output-archive", outputArchive);
783 }
784
785
786
787
788 @Parameter(property = "rat.OutputStandard", defaultValue = "ABSENCE")
789 public void setOutputStandard(String outputStandard) {
790 setArg("output-standard", outputStandard);
791 }
792
793
794
795
796 @Parameter(property = "rat.HelpLicenses")
797 public void setHelpLicenses(boolean helpLicenses) {
798 if (helpLicenses) {
799 setArg("help-licenses", null);
800 } else {
801 removeArg("help-licenses");
802 }
803 }
804 }