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.computeIfAbsent(key, k -> new ArrayList<>());
208 values.addAll(newValues);
209 }
210 }
211 }
212
213
214
215
216
217
218
219 protected void addArg(String key, String value) {
220 if (StringUtils.isNotBlank(value)) {
221 if (validateSet(key)) {
222 List<String> values = args.get(key);
223 if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
224 DefaultLog.getInstance().debug(String.format("Adding [%s] to %s", String.join(", ", Arrays.asList(value)), key));
225 }
226 if (values == null) {
227 values = new ArrayList<>();
228 args.put(key, values);
229 }
230 values.add(value);
231 }
232 }
233 }
234
235
236
237
238
239 protected void removeArg(String key) {
240 args.remove(key);
241 }
242
243
244
245 protected BaseRatMojo() {
246 setDeprecationReporter();
247 }
248
249
250
251
252
253
254
255
256
257 @Deprecated
258 @Parameter(property = "rat.Copyright")
259 public void setCopyright(String copyright) {
260 setArg("copyright", copyright);
261 }
262
263
264
265
266 @Parameter(property = "rat.EditCopyright")
267 public void setEditCopyright(String editCopyright) {
268 setArg("edit-copyright", editCopyright);
269 }
270
271
272
273
274
275 @Deprecated
276 @Parameter(property = "rat.Force")
277 public void setForce(boolean force) {
278 if (force) {
279 setArg("force", null);
280 } else {
281 removeArg("force");
282 }
283 }
284
285
286
287
288 @Parameter(property = "rat.EditOverwrite")
289 public void setEditOverwrite(boolean editOverwrite) {
290 if (editOverwrite) {
291 setArg("edit-overwrite", null);
292 } else {
293 removeArg("edit-overwrite");
294 }
295 }
296
297
298
299
300
301 @Deprecated
302 @Parameter(property = "rat.AddLicense")
303 public void setAddLicense(boolean addLicense) {
304 if (addLicense) {
305 setArg("addLicense", null);
306 } else {
307 removeArg("addLicense");
308 }
309 }
310
311
312
313
314 @Parameter(property = "rat.EditLicense")
315 public void setEditLicense(boolean editLicense) {
316 if (editLicense) {
317 setArg("edit-license", null);
318 } else {
319 removeArg("edit-license");
320 }
321 }
322
323
324
325
326 @Parameter
327 public void setConfigs(String[] config) {
328 addArg("config", config);
329 }
330
331
332
333
334 @Parameter
335 public void setConfig(String config) {
336 addArg("config", config);
337 }
338
339
340
341
342
343 @Deprecated
344 @Parameter
345 public void setLicenses(String[] licenses) {
346 addArg("licenses", licenses);
347 }
348
349
350
351
352
353 @Deprecated
354 @Parameter
355 public void setLicenses(String licenses) {
356 addArg("licenses", licenses);
357 }
358
359
360
361
362 @Parameter(property = "rat.ConfigurationNoDefaults")
363 public void setConfigurationNoDefaults(boolean configurationNoDefaults) {
364 if (configurationNoDefaults) {
365 setArg("configuration-no-defaults", null);
366 } else {
367 removeArg("configuration-no-defaults");
368 }
369 }
370
371
372
373
374
375 @Deprecated
376 @Parameter(property = "rat.NoDefaultLicenses")
377 public void setNoDefaultLicenses(boolean noDefaultLicenses) {
378 if (noDefaultLicenses) {
379 setArg("no-default-licenses", null);
380 } else {
381 removeArg("no-default-licenses");
382 }
383 }
384
385
386
387
388 @Parameter(property = "rat.LicensesApproved")
389 public void setLicensesApproved(String licensesApproved) {
390 setArg("licenses-approved", licensesApproved);
391 }
392
393
394
395
396 @Parameter(property = "rat.LicensesApprovedFile")
397 public void setLicensesApprovedFile(String licensesApprovedFile) {
398 setArg("licenses-approved-file", licensesApprovedFile);
399 }
400
401
402
403
404 @Parameter(property = "rat.LicenseFamiliesApproved")
405 public void setLicenseFamiliesApproved(String licenseFamiliesApproved) {
406 setArg("license-families-approved", licenseFamiliesApproved);
407 }
408
409
410
411
412 @Parameter(property = "rat.LicenseFamiliesApprovedFile")
413 public void setLicenseFamiliesApprovedFile(String licenseFamiliesApprovedFile) {
414 setArg("license-families-approved-file", licenseFamiliesApprovedFile);
415 }
416
417
418
419
420 @Parameter(property = "rat.LicensesDenied")
421 public void setLicensesDenied(String licensesDenied) {
422 setArg("licenses-denied", licensesDenied);
423 }
424
425
426
427
428 @Parameter(property = "rat.LicensesDeniedFile")
429 public void setLicensesDeniedFile(String licensesDeniedFile) {
430 setArg("licenses-denied-file", licensesDeniedFile);
431 }
432
433
434
435
436 @Parameter(property = "rat.LicenseFamiliesDenied")
437 public void setLicenseFamiliesDenied(String licenseFamiliesDenied) {
438 setArg("license-families-denied", licenseFamiliesDenied);
439 }
440
441
442
443
444 @Parameter(property = "rat.LicenseFamiliesDeniedFile")
445 public void setLicenseFamiliesDeniedFile(String licenseFamiliesDeniedFile) {
446 setArg("license-families-denied-file", licenseFamiliesDeniedFile);
447 }
448
449
450
451
452 @Parameter
453 public void setCounterMaxs(String[] counterMax) {
454 addArg("counter-max", counterMax);
455 }
456
457
458
459
460 @Parameter
461 public void setCounterMax(String counterMax) {
462 addArg("counter-max", counterMax);
463 }
464
465
466
467
468 @Parameter
469 public void setCounterMins(String[] counterMin) {
470 addArg("counter-min", counterMin);
471 }
472
473
474
475
476 @Parameter
477 public void setCounterMin(String counterMin) {
478 addArg("counter-min", counterMin);
479 }
480
481
482
483
484 @Parameter
485 public void setInputSources(String[] inputSource) {
486 addArg("input-source", inputSource);
487 }
488
489
490
491
492 @Parameter
493 public void setInputSource(String inputSource) {
494 addArg("input-source", inputSource);
495 }
496
497
498
499
500
501 @Deprecated
502 @Parameter
503 public void setExcludes(String[] exclude) {
504 addArg("exclude", exclude);
505 }
506
507
508
509
510
511 @Deprecated
512 @Parameter
513 public void setExclude(String exclude) {
514 addArg("exclude", exclude);
515 }
516
517
518
519
520 @Parameter
521 public void setInputExcludes(String[] inputExclude) {
522 addArg("input-exclude", inputExclude);
523 }
524
525
526
527
528 @Parameter
529 public void setInputExclude(String inputExclude) {
530 addArg("input-exclude", inputExclude);
531 }
532
533
534
535
536
537 @Deprecated
538 @Parameter(property = "rat.ExcludeFile")
539 public void setExcludeFile(String excludeFile) {
540 setArg("exclude-file", excludeFile);
541 }
542
543
544
545
546 @Parameter(property = "rat.InputExcludeFile")
547 public void setInputExcludeFile(String inputExcludeFile) {
548 setArg("input-exclude-file", inputExcludeFile);
549 }
550
551
552
553
554 @Parameter
555 public void setInputExcludeStds(String[] inputExcludeStd) {
556 addArg("input-exclude-std", inputExcludeStd);
557 }
558
559
560
561
562 @Parameter
563 public void setInputExcludeStd(String inputExcludeStd) {
564 addArg("input-exclude-std", inputExcludeStd);
565 }
566
567
568
569
570 @Parameter(property = "rat.InputExcludeSize")
571 public void setInputExcludeSize(String inputExcludeSize) {
572 setArg("input-exclude-size", inputExcludeSize);
573 }
574
575
576
577
578 @Parameter
579 public void setInputIncludes(String[] inputInclude) {
580 addArg("input-include", inputInclude);
581 }
582
583
584
585
586 @Parameter
587 public void setInputInclude(String inputInclude) {
588 addArg("input-include", inputInclude);
589 }
590
591
592
593
594
595 @Deprecated
596 @Parameter
597 public void setIncludes(String[] include) {
598 addArg("include", include);
599 }
600
601
602
603
604
605 @Deprecated
606 @Parameter
607 public void setInclude(String include) {
608 addArg("include", include);
609 }
610
611
612
613
614 @Parameter(property = "rat.InputIncludeFile")
615 public void setInputIncludeFile(String inputIncludeFile) {
616 setArg("input-include-file", inputIncludeFile);
617 }
618
619
620
621
622
623 @Deprecated
624 @Parameter(property = "rat.IncludesFile")
625 public void setIncludesFile(String includesFile) {
626 setArg("includes-file", includesFile);
627 }
628
629
630
631
632 @Parameter
633 public void setInputIncludeStds(String[] inputIncludeStd) {
634 addArg("input-include-std", inputIncludeStd);
635 }
636
637
638
639
640 @Parameter
641 public void setInputIncludeStd(String inputIncludeStd) {
642 addArg("input-include-std", inputIncludeStd);
643 }
644
645
646
647
648
649 @Deprecated
650 @Parameter(property = "rat.ScanHiddenDirectories")
651 public void setScanHiddenDirectories(boolean scanHiddenDirectories) {
652 if (scanHiddenDirectories) {
653 setArg("scan-hidden-directories", null);
654 } else {
655 removeArg("scan-hidden-directories");
656 }
657 }
658
659
660
661
662 @Parameter
663 public void setInputExcludeParsedScms(String[] inputExcludeParsedScm) {
664 addArg("input-exclude-parsed-scm", inputExcludeParsedScm);
665 }
666
667
668
669
670 @Parameter
671 public void setInputExcludeParsedScm(String inputExcludeParsedScm) {
672 addArg("input-exclude-parsed-scm", inputExcludeParsedScm);
673 }
674
675
676
677
678 @Parameter(property = "rat.OutputStyle")
679 public void setOutputStyle(String outputStyle) {
680 setArg("output-style", outputStyle);
681 }
682
683
684
685
686
687 @Deprecated
688 @Parameter(property = "rat.Stylesheet")
689 public void setStylesheet(String stylesheet) {
690 setArg("stylesheet", stylesheet);
691 }
692
693
694
695
696
697 @Deprecated
698 @Parameter(property = "rat.Xml")
699 public void setXml(boolean xml) {
700 if (xml) {
701 setArg("xml", null);
702 } else {
703 removeArg("xml");
704 }
705 }
706
707
708
709
710 @Parameter(property = "rat.OutputLicenses", defaultValue = "NONE")
711 public void setOutputLicenses(String outputLicenses) {
712 setArg("output-licenses", outputLicenses);
713 }
714
715
716
717
718
719 @Deprecated
720 @Parameter(property = "rat.ListLicenses")
721 public void setListLicenses(String listLicenses) {
722 setArg("list-licenses", listLicenses);
723 }
724
725
726
727
728 @Parameter(property = "rat.OutputFamilies", defaultValue = "NONE")
729 public void setOutputFamilies(String outputFamilies) {
730 setArg("output-families", outputFamilies);
731 }
732
733
734
735
736
737 @Deprecated
738 @Parameter(property = "rat.ListFamilies")
739 public void setListFamilies(String listFamilies) {
740 setArg("list-families", listFamilies);
741 }
742
743
744
745
746 @Parameter(property = "rat.DryRun")
747 public void setDryRun(boolean dryRun) {
748 if (dryRun) {
749 setArg("dry-run", null);
750 } else {
751 removeArg("dry-run");
752 }
753 }
754
755
756
757
758
759 @Deprecated
760 @Parameter(property = "rat.Out")
761 public void setOut(String out) {
762 setArg("out", out);
763 }
764
765
766
767
768 @Parameter(property = "rat.OutputFile", defaultValue = "${project.build.directory}/rat.txt")
769 public void setOutputFile(String outputFile) {
770 setArg("output-file", outputFile);
771 }
772
773
774
775
776 @Parameter(property = "rat.OutputArchive", defaultValue = "NOTIFICATION")
777 public void setOutputArchive(String outputArchive) {
778 setArg("output-archive", outputArchive);
779 }
780
781
782
783
784 @Parameter(property = "rat.OutputStandard", defaultValue = "ABSENCE")
785 public void setOutputStandard(String outputStandard) {
786 setArg("output-standard", outputStandard);
787 }
788
789
790
791
792 @Parameter(property = "rat.HelpLicenses")
793 public void setHelpLicenses(boolean helpLicenses) {
794 if (helpLicenses) {
795 setArg("help-licenses", null);
796 } else {
797 removeArg("help-licenses");
798 }
799 }
800 }