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 java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileNotFoundException;
25  import java.io.FileWriter;
26  import java.io.IOException;
27  import java.io.InputStream;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.MojoFailureException;
31  import org.apache.rat.Defaults;
32  import org.apache.rat.ReportConfiguration;
33  import org.apache.rat.report.claim.ClaimStatistic;
34  
35  /**
36   * Run RAT to perform a violation check.
37   * 
38   * @goal check
39   * @phase verify
40   */
41  public class RatCheckMojo extends AbstractRatMojo
42  {
43      /**
44       * Where to store the report.
45       * 
46       * @parameter expression="${rat.outputFile}" default-value="${project.build.directory}/rat.txt"
47       */
48      private File reportFile;
49  
50      /**
51       * Output style of the report. Use "plain" (the default) for a plain text
52       * report or "xml" for the raw XML report. Alternatively you can give the
53       * path of an XSL transformation that will be applied on the raw XML to
54       * produce the report written to the output file. 
55       * 
56       * @parameter expression="${rat.outputStyle}" default-value="plain"
57       */
58      private String reportStyle;
59  
60      /**
61       * Maximum number of files with unapproved licenses.
62       * @parameter expression="${rat.numUnapprovedLicenses}" default-value="0"
63       */
64      private int numUnapprovedLicenses;
65  
66      /**
67       * Whether to add license headers; possible values are
68       * {@code forced}, {@code true}, and {@code false} (default).
69       *
70       * @parameter expression="${rat.addLicenseHeaders}" default-value="false"
71       */
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 expression="${rat.copyrightMessage}"
80       */
81      private String copyrightMessage;
82  
83      private ClaimStatistic getRawReport()
84          throws MojoExecutionException, MojoFailureException
85      {
86          FileWriter fw = null;
87          try
88          {
89              fw = new FileWriter( reportFile );
90              final ClaimStatistic statistic = createReport( fw, getStyleSheet() );
91              fw.close();
92              fw = null;
93              return statistic;
94          }
95          catch ( IOException e )
96          {
97              throw new MojoExecutionException( e.getMessage(), e );
98          }
99          finally
100         {
101             if ( fw != null )
102             {
103                 try
104                 {
105                     fw.close();
106                 }
107                 catch ( Throwable t )
108                 {
109                     /* Ignore me */
110                 }
111             }
112         }
113     }
114 
115     /**
116      * Returns the XSL stylesheet to be used for formatting the report.
117      *
118      * @see #reportStyle
119      * @return report stylesheet, or <code>null</code> for raw XML
120      * @throws MojoExecutionException if the stylesheet can not be found
121      */
122     private InputStream getStyleSheet() throws MojoExecutionException {
123         if ( reportStyle == null || reportStyle.equals( "plain" ) )
124         {
125             return Defaults.getPlainStyleSheet();
126         }
127         else if ( reportStyle.equals( "xml" ) )
128         {
129             return null;
130         }
131         else
132         {
133             try
134             {
135                 return new FileInputStream( reportStyle );
136             }
137             catch ( FileNotFoundException e )
138             {
139                 throw new MojoExecutionException(
140                         "Unable to find report stylesheet: " + reportStyle, e );
141             }
142         }
143     }
144 
145     /**
146      * Invoked by Maven to execute the Mojo.
147      * 
148      * @throws MojoFailureException
149      *             An error in the plugin configuration was detected.
150      * @throws MojoExecutionException
151      *             Another error occurred while executing the plugin.
152      */
153     public void execute() throws MojoExecutionException, MojoFailureException
154     {
155         File parent = reportFile.getParentFile();
156         parent.mkdirs();
157 
158         final ClaimStatistic report = getRawReport();
159         check( report );
160     }
161 
162     protected void check( ClaimStatistic statistics )
163         throws MojoFailureException
164     {
165         if ( numUnapprovedLicenses < statistics.getNumUnApproved() )
166         {
167             throw new RatCheckException( "Too many unapproved licenses: " + statistics.getNumUnApproved() );
168         }
169     }
170 
171     protected ReportConfiguration getConfiguration()
172             throws MojoFailureException, MojoExecutionException {
173         final ReportConfiguration configuration = super.getConfiguration();
174         if ("forced".equals(addLicenseHeaders)) {
175             configuration.setAddingLicenses(true);
176             configuration.setAddingLicensesForced(true);
177             configuration.setCopyrightMessage(copyrightMessage);
178         } else if ("true".equals(addLicenseHeaders)) {
179             configuration.setAddingLicenses(true);
180             configuration.setCopyrightMessage(copyrightMessage);
181         } else if ("false".equals(addLicenseHeaders)) {
182             // Nothing to do
183         } else {
184             throw new MojoFailureException("Invalid value for addLicenseHeaders: Expected forced|true|false, got "
185                     + addLicenseHeaders);
186         }
187         return configuration;
188     }
189 }