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.io.IOException;
24 import java.nio.charset.StandardCharsets;
25 import java.util.List;
26
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.MojoFailureException;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.apache.rat.ReportConfiguration;
33 import org.apache.rat.Reporter;
34 import org.apache.rat.commandline.Arg;
35 import org.apache.rat.commandline.StyleSheets;
36 import org.apache.rat.license.LicenseSetFactory.LicenseFilter;
37 import org.apache.rat.report.claim.ClaimStatistic;
38 import org.apache.rat.utils.DefaultLog;
39
40 import static java.lang.String.format;
41
42
43
44
45
46
47
48 @Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
49 public class RatCheckMojo extends AbstractRatMojo {
50
51
52
53
54 @Deprecated
55 @Parameter(defaultValue = "${project.build.directory}/rat.txt")
56 private File defaultReportFile;
57
58
59
60
61
62 @Deprecated
63 @Parameter
64 public void setReportFile(final File reportFile) {
65 if (!reportFile.getParentFile().exists() && !reportFile.getParentFile().mkdirs()) {
66 getLog().error("Unable to create directory " + reportFile.getParentFile());
67 }
68 setOutputFile(reportFile.getAbsolutePath());
69 }
70
71
72
73
74
75
76
77
78 @Deprecated
79 @Parameter(property = "rat.outputStyle")
80 public void setReportStyle(final String value) {
81 if (value.equalsIgnoreCase("xml")) {
82 setXml(true);
83 } else if (value.equalsIgnoreCase("plain")) {
84 setStylesheet("plain-rat");
85 } else {
86 setStylesheet(value);
87 }
88 }
89
90
91
92
93
94 @Deprecated
95 @Parameter(property = "rat.numUnapprovedLicenses", defaultValue = "0")
96 private int numUnapprovedLicenses;
97
98
99
100
101
102
103 @Deprecated
104 @Parameter(property = "rat.addLicenseHeaders")
105 public void setAddLicenseHeaders(final String addLicenseHeaders) {
106 switch (addLicenseHeaders.trim().toUpperCase()) {
107 case "FALSE":
108
109 break;
110 case "TRUE":
111 setAddLicense(true);
112 break;
113 case "FORCED":
114 setAddLicense(true);
115 setForce(true);
116 break;
117 default:
118 throw new IllegalArgumentException("Unknown addlicense header: " + addLicenseHeaders);
119 }
120 }
121
122
123
124
125
126 @Deprecated
127 @Parameter(property = "rat.copyrightMessage")
128 public void setCopyrightMessage(final String copyrightMessage) {
129 setCopyright(copyrightMessage);
130 }
131
132
133
134
135
136
137
138 @Parameter(property = "rat.ignoreErrors", defaultValue = "false")
139 private boolean ignoreErrors;
140
141
142
143
144
145
146
147
148 @Parameter(property = "rat.consoleOutput", defaultValue = "true")
149 private boolean consoleOutput;
150
151
152 private Reporter reporter;
153
154 @Override
155 protected ReportConfiguration getConfiguration() throws MojoExecutionException {
156 ReportConfiguration result = super.getConfiguration();
157 if (numUnapprovedLicenses > 0) {
158 result.getClaimValidator().setMax(ClaimStatistic.Counter.UNAPPROVED, numUnapprovedLicenses);
159 }
160 return result;
161 }
162
163
164
165
166
167
168
169
170
171 @Override
172 public void execute() throws MojoExecutionException, MojoFailureException {
173 if (skip) {
174 getLog().info("RAT will not execute since it is configured to be skipped via system property 'rat.skip'.");
175 return;
176 }
177
178 if (getValues(Arg.OUTPUT_FILE).isEmpty()) {
179 setArg(Arg.OUTPUT_FILE.option().getLongOpt(), defaultReportFile.getAbsolutePath());
180 }
181
182 ReportConfiguration config = getConfiguration();
183
184 logLicenses(config.getLicenses(LicenseFilter.ALL));
185 try {
186 this.reporter = new Reporter(config);
187 reporter.output();
188 check(config);
189 } catch (MojoFailureException e) {
190 throw e;
191 } catch (Exception e) {
192 throw new MojoExecutionException(e.getMessage(), e);
193 }
194 }
195
196 protected void check(final ReportConfiguration config) throws MojoFailureException {
197 ClaimStatistic statistics = reporter.getClaimsStatistic();
198 try {
199 reporter.writeSummary(DefaultLog.getInstance().asWriter());
200 if (config.getClaimValidator().hasErrors()) {
201 config.getClaimValidator().logIssues(statistics);
202 if (consoleOutput &&
203 !config.getClaimValidator().isValid(ClaimStatistic.Counter.UNAPPROVED, statistics.getCounter(ClaimStatistic.Counter.UNAPPROVED))) {
204 try {
205 ByteArrayOutputStream baos = new ByteArrayOutputStream();
206 reporter.output(StyleSheets.UNAPPROVED_LICENSES.getStyleSheet(), () -> baos);
207 getLog().warn(baos.toString(StandardCharsets.UTF_8.name()));
208 } catch (Exception e) {
209 getLog().warn("Unable to print the files with unapproved licenses to the console.");
210 }
211 }
212
213 String msg = format("Counter(s) %s exceeded minimum or maximum values. See RAT report in: '%s'.",
214 String.join(", ", config.getClaimValidator().listIssues(statistics)),
215 getRatTxtFile());
216
217 if (!ignoreErrors) {
218 throw new RatCheckException(msg);
219 } else {
220 getLog().info(msg);
221 }
222 }
223 } catch (IOException e) {
224 throw new MojoFailureException(e);
225 }
226 }
227
228
229
230
231
232
233
234 public File getRatTxtFile() throws MojoFailureException {
235 List<String> args = getValues(Arg.OUTPUT_FILE);
236 if (args != null) {
237 return new File(args.get(0));
238 }
239 throw new MojoFailureException("No output file specified");
240 }
241 }