View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Run Rat to perform a violation check.
39   */
40  @Mojo(name = "check", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true)
41  public class RatCheckMojo extends AbstractRatMojo {
42  
43      /** The default output file if no other is specified. */
44      @Parameter(property = "rat.outputFile", defaultValue = "${project.build.directory}/rat.txt")
45      private File defaultReportFile;
46  
47      /**
48       * Where to store the report.
49       * @deprecated use 'out' property.
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       * Output style of the report. Use "plain" (the default) for a plain text report
64       * or "xml" for the raw XML report. Alternatively you can give the path of an
65       * XSL transformation that will be applied on the raw XML to produce the report
66       * written to the output file.
67       * @deprecated use setStyleSheet or xml
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       * Maximum number of files with unapproved licenses.
83       */
84      @Parameter(property = "rat.numUnapprovedLicenses", defaultValue = "0")
85      private int numUnapprovedLicenses;
86  
87      /**
88       * Whether to add license headers; possible values are {@code forced},
89       * {@code true}, and {@code false} (default).
90       * @deprecated use addLicense and forced
91       */
92      @Deprecated
93      @Parameter(property = "rat.addLicenseHeaders")
94      public void setAddLicenseHeaders(final String addLicenseHeaders) {
95          switch (addLicenseHeaders.trim().toUpperCase()) {
96              case "FALSE":
97                  // do nothing;
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      * Copyright message to add to license headers. This option is ignored, unless
113      * {@code addLicenseHeaders} is set to {@code true}, or {@code forced}.
114      * @deprecated use copyright
115      */
116     @Deprecated
117     @Parameter(property = "rat.copyrightMessage")
118     public void setCopyrightMessage(final String copyrightMessage) {
119         setCopyright(copyrightMessage);
120     }
121 
122     /**
123      * Will ignore rat errors and display a log message if any. Its use is NOT
124      * RECOMMENDED, but quite convenient on occasion.
125      *
126      * @since 0.9
127      */
128     @Parameter(property = "rat.ignoreErrors", defaultValue = "false")
129     private boolean ignoreErrors;
130 
131     /**
132      * Whether to output the names of files that have unapproved licenses to the
133      * console. Defaults to {@code true} to ease builds in containers where you are
134      * unable to access rat.txt easily.
135      *
136      * @since 0.12
137      */
138     @Parameter(property = "rat.consoleOutput", defaultValue = "true")
139     private boolean consoleOutput;
140 
141     /** The reporter that this mojo uses */
142     private Reporter reporter;
143 
144     /**
145      * Invoked by Maven to execute the Mojo.
146      *
147      * @throws MojoFailureException An error in the plugin configuration was
148      * detected.
149      * @throws MojoExecutionException Another error occurred while executing the
150      * plugin.
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 }