View Javadoc
1   package org.apache.rat.mp;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Run Rat to perform a violation check.
43   */
44  @Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE)
45  public class RatCheckMojo extends AbstractRatMojo {
46      /**
47       * Where to store the report.
48       */
49      @Parameter(property = "rat.outputFile", defaultValue = "${project.build.directory}/rat.txt")
50      private File reportFile;
51  
52      /**
53       * Output style of the report. Use "plain" (the default) for a plain text
54       * report or "xml" for the raw XML report. Alternatively you can give the
55       * path of an XSL transformation that will be applied on the raw XML to
56       * produce the report written to the output file.
57       */
58      @Parameter(property = "rat.outputStyle", defaultValue = "plain")
59      private String reportStyle;
60  
61      /**
62       * Maximum number of files with unapproved licenses.
63       */
64      @Parameter(property = "rat.numUnapprovedLicenses", defaultValue = "0")
65      private int numUnapprovedLicenses;
66  
67      /**
68       * Whether to add license headers; possible values are
69       * {@code forced}, {@code true}, and {@code false} (default).
70       */
71      @Parameter(property = "rat.addLicenseHeaders", defaultValue = "false")
72      private String addLicenseHeaders;
73  
74      /**
75       * Copyright message to add to license headers. This option is
76       * ignored, unless {@code addLicenseHeaders} is set to {@code true},
77       * or {@code forced}.
78       */
79      @Parameter(property = "rat.copyrightMessage")
80      private String copyrightMessage;
81  
82      /**
83       * Will ignore rat errors and display a log message if any.
84       * Its use is NOT RECOMMENDED, but quite convenient on occasion.
85       *
86       * @since 0.9
87       */
88      @Parameter(property = "rat.ignoreErrors", defaultValue = "false")
89      private boolean ignoreErrors;
90  
91      /**
92       * Whether to output the names of files that have unapproved licenses to the
93       * console.
94       *
95       * @since 0.12
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      * Returns the XSL stylesheet to be used for formatting the report.
118      *
119      * @return report stylesheet, or <code>null</code> for raw XML
120      * @throws MojoExecutionException if the stylesheet can not be found
121      * @see #reportStyle
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      * Invoked by Maven to execute the Mojo.
140      *
141      * @throws MojoFailureException   An error in the plugin configuration was detected.
142      * @throws MojoExecutionException Another error occurred while executing the plugin.
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             // Nothing to do
204         } else {
205             throw new MojoFailureException("Invalid value for addLicenseHeaders: Expected " + AddLicenseHeaders.getValuesForHelp() + ", got "
206                     + addLicenseHeaders);
207         }
208         return configuration;
209     }
210 }