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