1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.mp;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.File;
23 import java.nio.charset.StandardCharsets;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugin.MojoFailureException;
27 import org.apache.maven.plugins.annotations.LifecyclePhase;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.plugins.annotations.Parameter;
30 import org.apache.rat.Defaults;
31 import org.apache.rat.OptionCollection;
32 import org.apache.rat.ReportConfiguration;
33 import org.apache.rat.Reporter;
34 import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
35 import org.apache.rat.report.claim.ClaimStatistic;
36
37
38
39
40 @Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
41 public class RatCheckMojo extends AbstractRatMojo {
42
43
44 @Parameter(property = "rat.outputFile", defaultValue = "${project.build.directory}/rat.txt")
45 private File defaultReportFile;
46
47
48
49
50
51 @Deprecated
52 @Parameter(property = "rat.outputFile")
53 public void setReportFile(final File reportFile) {
54 if (!reportFile.getParentFile().exists()) {
55 if (!reportFile.getParentFile().mkdirs()) {
56 getLog().error("Unable to create directory " + reportFile.getParentFile());
57 }
58 }
59 setOut(reportFile.getAbsolutePath());
60 }
61
62
63
64
65
66
67
68
69 @Deprecated
70 @Parameter(property = "rat.outputStyle")
71 public void setReportStyle(final String value) {
72 if (value.equalsIgnoreCase("xml")) {
73 setXml(true);
74 } else if (value.equalsIgnoreCase("plain")) {
75 setStylesheet("plain-rat");
76 } else {
77 setStylesheet(value);
78 }
79 }
80
81
82
83
84 @Parameter(property = "rat.numUnapprovedLicenses", defaultValue = "0")
85 private int numUnapprovedLicenses;
86
87
88
89
90
91
92 @Deprecated
93 @Parameter(property = "rat.addLicenseHeaders")
94 public void setAddLicenseHeaders(final String addLicenseHeaders) {
95 switch (addLicenseHeaders.trim().toUpperCase()) {
96 case "FALSE":
97
98 break;
99 case "TRUE":
100 setAddLicense(true);
101 break;
102 case "FORCED":
103 setAddLicense(true);
104 setForce(true);
105 break;
106 default:
107 throw new IllegalArgumentException("Unknown addlicense header: " + addLicenseHeaders);
108 }
109 }
110
111
112
113
114
115
116 @Deprecated
117 @Parameter(property = "rat.copyrightMessage")
118 public void setCopyrightMessage(final String copyrightMessage) {
119 setCopyright(copyrightMessage);
120 }
121
122
123
124
125
126
127
128 @Parameter(property = "rat.ignoreErrors", defaultValue = "false")
129 private boolean ignoreErrors;
130
131
132
133
134
135
136
137
138 @Parameter(property = "rat.consoleOutput", defaultValue = "true")
139 private boolean consoleOutput;
140
141
142 private Reporter reporter;
143
144
145
146
147
148
149
150
151
152 @Override
153 public void execute() throws MojoExecutionException, MojoFailureException {
154 if (skip) {
155 getLog().info("RAT will not execute since it is configured to be skipped via system property 'rat.skip'.");
156 return;
157 }
158
159 String outKey = "--" + OptionCollection.OUT.getLongOpt();
160 if (args.get(outKey) == null) {
161 setArg(outKey, defaultReportFile.getPath());
162 }
163 ReportConfiguration config = getConfiguration();
164
165 logLicenses(config.getLicenses(LicenseFilter.ALL));
166 try {
167 this.reporter = new Reporter(config);
168 reporter.output();
169 check();
170 } catch (MojoFailureException e) {
171 throw e;
172 } catch (Exception e) {
173 throw new MojoExecutionException(e.getMessage(), e);
174 }
175 }
176
177 protected void check() throws MojoFailureException {
178 if (numUnapprovedLicenses > 0) {
179 getLog().info("You requested to accept " + numUnapprovedLicenses + " files with unapproved licenses.");
180 }
181 ClaimStatistic stats = reporter.getClaimsStatistic();
182
183 int numApproved = stats.getCounter(ClaimStatistic.Counter.APPROVED);
184 StringBuilder statSummary = new StringBuilder("Rat check: Summary over all files. Unapproved: ")
185 .append(stats.getCounter(ClaimStatistic.Counter.UNAPPROVED)).append(", unknown: ")
186 .append(stats.getCounter(ClaimStatistic.Counter.UNKNOWN)).append(", generated: ")
187 .append(stats.getCounter(ClaimStatistic.Counter.GENERATED)).append(", approved: ").append(numApproved)
188 .append(numApproved > 0 ? " licenses." : " license.");
189
190 getLog().info(statSummary.toString());
191 if (numUnapprovedLicenses < stats.getCounter(ClaimStatistic.Counter.UNAPPROVED)) {
192 if (consoleOutput) {
193 try {
194 ByteArrayOutputStream baos = new ByteArrayOutputStream();
195 reporter.output(Defaults.getUnapprovedLicensesStyleSheet(), () -> baos);
196 getLog().warn(baos.toString(StandardCharsets.UTF_8.name()));
197 } catch (Exception e) {
198 getLog().warn("Unable to print the files with unapproved licenses to the console.");
199 }
200 }
201
202 final String seeReport = " See RAT report in: " + args.get("--" + OptionCollection.OUT.getLongOpt());
203 if (!ignoreErrors) {
204 throw new RatCheckException("Too many files with unapproved license: "
205 + stats.getCounter(ClaimStatistic.Counter.UNAPPROVED) + seeReport);
206 }
207 getLog().warn("Rat check: " + stats.getCounter(ClaimStatistic.Counter.UNAPPROVED)
208 + " files with unapproved licenses." + seeReport);
209 }
210 }
211 }