1 package org.apache.rat.mp;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.IOUtils;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.maven.plugin.MojoFailureException;
25 import org.apache.maven.plugins.annotations.LifecyclePhase;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.rat.Defaults;
29 import org.apache.rat.ReportConfiguration;
30 import org.apache.rat.config.AddLicenseHeaders;
31 import org.apache.rat.config.ReportFormat;
32 import org.apache.rat.report.claim.ClaimStatistic;
33
34 import java.io.File;
35 import java.io.FileInputStream;
36 import java.io.FileNotFoundException;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.io.InputStream;
40
41
42
43
44 @Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE)
45 public class RatCheckMojo extends AbstractRatMojo {
46
47
48
49 @Parameter(property = "rat.outputFile", defaultValue = "${project.build.directory}/rat.txt")
50 private File reportFile;
51
52
53
54
55
56
57
58 @Parameter(property = "rat.outputStyle", defaultValue = "plain")
59 private String reportStyle;
60
61
62
63
64 @Parameter(property = "rat.numUnapprovedLicenses", defaultValue = "0")
65 private int numUnapprovedLicenses;
66
67
68
69
70
71 @Parameter(property = "rat.addLicenseHeaders", defaultValue = "false")
72 private String addLicenseHeaders;
73
74
75
76
77
78
79 @Parameter(property = "rat.copyrightMessage")
80 private String copyrightMessage;
81
82
83
84
85
86
87
88 @Parameter(property = "rat.ignoreErrors", defaultValue = "false")
89 private boolean ignoreErrors;
90
91
92
93
94
95
96
97 @Parameter(property = "rat.consoleOutput", defaultValue = "false")
98 private boolean consoleOutput;
99
100 private ClaimStatistic getRawReport()
101 throws MojoExecutionException, MojoFailureException {
102 FileWriter fw = null;
103 try {
104 fw = new FileWriter(reportFile);
105 final ClaimStatistic statistic = createReport(fw, getStyleSheet());
106 fw.close();
107 fw = null;
108 return statistic;
109 } catch (IOException e) {
110 throw new MojoExecutionException(e.getMessage(), e);
111 } finally {
112 IOUtils.closeQuietly(fw);
113 }
114 }
115
116
117
118
119
120
121
122
123 private InputStream getStyleSheet() throws MojoExecutionException {
124 if (reportStyle == null || ReportFormat.PLAIN.is(reportStyle)) {
125 return Defaults.getPlainStyleSheet();
126 } else if (ReportFormat.XML.is(reportStyle)) {
127 return null;
128 } else {
129 try {
130 return new FileInputStream(reportStyle);
131 } catch (FileNotFoundException e) {
132 throw new MojoExecutionException(
133 "Unable to find report stylesheet: " + reportStyle, e);
134 }
135 }
136 }
137
138
139
140
141
142
143
144 public void execute() throws MojoExecutionException, MojoFailureException {
145 if (skip) {
146 getLog().info("RAT will not execute since it is configured to be skipped via system property 'rat.skip'.");
147 return;
148 }
149
150 final File parent = reportFile.getParentFile();
151 if (!parent.mkdirs() && !parent.isDirectory()) {
152 throw new MojoExecutionException("Could not create report parent directory " + parent);
153 }
154
155 final ClaimStatistic report = getRawReport();
156 check(report);
157 }
158
159 protected void check(ClaimStatistic statistics)
160 throws MojoFailureException {
161 if (numUnapprovedLicenses > 0) {
162 getLog().info("You requested to accept " + numUnapprovedLicenses + " files with unapproved licenses.");
163 }
164
165 int numApproved = statistics.getNumApproved();
166 getLog().info("Rat check: Summary over all files. Unapproved: " + statistics.getNumUnApproved() +
167 ", unknown: " + statistics.getNumUnknown() +
168 ", generated: " + statistics.getNumGenerated() +
169 ", approved: " + numApproved +
170 (numApproved > 0 ? " licenses." : " license."));
171
172 if (numUnapprovedLicenses < statistics.getNumUnApproved()) {
173 if (consoleOutput) {
174 try {
175 getLog().warn(createReport(Defaults.getUnapprovedLicensesStyleSheet()).trim());
176 } catch (MojoExecutionException e) {
177 getLog().warn("Unable to print the files with unapproved licenses to the console.");
178 }
179 }
180
181 final String seeReport = " See RAT report in: " + reportFile;
182 if (!ignoreErrors) {
183 throw new RatCheckException("Too many files with unapproved license: " + statistics.getNumUnApproved() + seeReport);
184 } else {
185 getLog().warn("Rat check: " + statistics.getNumUnApproved() + " files with unapproved licenses." + seeReport);
186 }
187 }
188 }
189
190 @Override
191 protected ReportConfiguration getConfiguration()
192 throws MojoFailureException, MojoExecutionException {
193 final ReportConfiguration configuration = super.getConfiguration();
194
195 if (AddLicenseHeaders.FORCED.name().equalsIgnoreCase(addLicenseHeaders)) {
196 configuration.setAddingLicenses(true);
197 configuration.setAddingLicensesForced(true);
198 configuration.setCopyrightMessage(copyrightMessage);
199 } else if (AddLicenseHeaders.TRUE.name().equalsIgnoreCase(addLicenseHeaders)) {
200 configuration.setAddingLicenses(true);
201 configuration.setCopyrightMessage(copyrightMessage);
202 } else if (AddLicenseHeaders.FALSE.name().equalsIgnoreCase(addLicenseHeaders)) {
203
204 } else {
205 throw new MojoFailureException("Invalid value for addLicenseHeaders: Expected " + AddLicenseHeaders.getValuesForHelp() + ", got "
206 + addLicenseHeaders);
207 }
208 return configuration;
209 }
210 }