View Javadoc

1   package org.apache.maven.plugin.checkstyle;
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.maven.plugins.annotations.Mojo;
23  import org.apache.maven.plugins.annotations.Parameter;
24  import org.apache.maven.plugins.annotations.ResolutionScope;
25  import org.apache.maven.project.MavenProject;
26  import org.apache.maven.reporting.MavenReportException;
27  
28  import java.io.File;
29  import java.util.List;
30  
31  /**
32   * A reporting task that performs Checkstyle analysis and generates an aggregate HTML report on the violations that checkstyle finds in a multi-module reactor build.
33   *
34   * @version $Id: CheckstyleAggregateReport.java 1540563 2013-11-10 22:07:54Z bimargulies $
35   * @since 2.8
36   */
37  @Mojo( name = "checkstyle-aggregate", aggregator = true, requiresDependencyResolution = ResolutionScope.COMPILE,
38         threadSafe = true )
39  public class CheckstyleAggregateReport
40      extends AbstractCheckstyleReport
41  {
42  
43      /**
44       * Specifies the names filter of the source files to be used for Checkstyle.
45       *
46       * <strong>Note:</strong> default value is {@code **\/*.java}.
47       */
48      @Parameter( property = "checkstyle.includes", defaultValue = JAVA_FILES, required = true )
49      private String includes;
50  
51      /**
52       * Specifies the names filter of the source files to be excluded for
53       * Checkstyle.
54       */
55      @Parameter( property = "checkstyle.excludes" )
56      private String excludes;
57  
58      /**
59       * Specifies the names filter of the resource files to be used for Checkstyle.
60       *
61       * <strong>Note:</strong> default value is {@code **\/*.properties}.
62       * @since 2.11
63       */
64      @Parameter( property = "checkstyle.resourceIncludes", defaultValue = "**/*.properties", required = true )
65      private String resourceIncludes;
66  
67      /**
68       * Specifies the names filter of the resource files to be excluded for
69       * Checkstyle.
70       * @since 2.11
71       */
72      @Parameter( property = "checkstyle.resourceExcludes" )
73      private String resourceExcludes;
74  
75      /**
76       * Specifies whether to include the resource directories in the check.
77       * @since 2.11
78       */
79      @Parameter( property = "checkstyle.includeResources", defaultValue = "true", required = true )
80      private boolean includeResources;
81  
82      /**
83       * Specifies whether to include the test resource directories in the check.
84       * @since 2.11
85       */
86      @Parameter( property = "checkstyle.includeTestResources", defaultValue = "true", required = true )
87      private boolean includeTestResources;
88  
89  
90      /**
91       * <p>
92       * Specifies the location of the XML configuration to use.
93       * </p>
94       * <p/>
95       * <p>
96       * Potential values are a filesystem path, a URL, or a classpath resource.
97       * This parameter expects that the contents of the location conform to the
98       * xml format (Checkstyle <a
99       * href="http://checkstyle.sourceforge.net/config.html#Modules">Checker
100      * module</a>) configuration of rulesets.
101      * </p>
102      * <p/>
103      * <p>
104      * This parameter is resolved as resource, URL, then file. If successfully
105      * resolved, the contents of the configuration is copied into the
106      * <code>${project.build.directory}/checkstyle-configuration.xml</code>
107      * file before being passed to Checkstyle as a configuration.
108      * </p>
109      * <p/>
110      * <p>
111      * There are 4 predefined rulesets.
112      * </p>
113      * <p/>
114      * <ul>
115      * <li><code>config/sun_checks.xml</code>: Sun Checks.</li>
116      * <li><code>config/turbine_checks.xml</code>: Turbine Checks.</li>
117      * <li><code>config/avalon_checks.xml</code>: Avalon Checks.</li>
118      * <li><code>config/maven_checks.xml</code>: Maven Source Checks.</li>
119      * </ul>
120      */
121     @Parameter( property = "checkstyle.config.location", defaultValue = "config/sun_checks.xml" )
122     private String configLocation;
123 
124     /**
125      * <p>
126      * Specifies the location of the properties file.
127      * </p>
128      * <p/>
129      * <p>
130      * This parameter is resolved as URL, File then resource. If successfully
131      * resolved, the contents of the properties location is copied into the
132      * <code>${project.build.directory}/checkstyle-checker.properties</code>
133      * file before being passed to Checkstyle for loading.
134      * </p>
135      * <p/>
136      * <p>
137      * The contents of the <code>propertiesLocation</code> will be made
138      * available to Checkstyle for specifying values for parameters within the
139      * xml configuration (specified in the <code>configLocation</code>
140      * parameter).
141      * </p>
142      *
143      * @since 2.0-beta-2
144      */
145     @Parameter( property = "checkstyle.properties.location" )
146     private String propertiesLocation;
147 
148     /**
149      * Allows for specifying raw property expansion information.
150      */
151     @Parameter
152     private String propertyExpansion;
153 
154     /**
155      * <p>
156      * Specifies the location of the License file (a.k.a. the header file) that
157      * can be used by Checkstyle to verify that source code has the correct
158      * license header.
159      * </p>
160      * <p>
161      * You need to use ${checkstyle.header.file} in your Checkstyle xml
162      * configuration to reference the name of this header file.
163      * </p>
164      * <p>
165      * For instance:
166      * </p>
167      * <p>
168      * <code>
169      * &lt;module name="RegexpHeader">
170      * &lt;property name="headerFile" value="${checkstyle.header.file}"/>
171      * &lt;/module>
172      * </code>
173      * </p>
174      *
175      * @since 2.0-beta-2
176      */
177     @Parameter( property = "checkstyle.header.file", defaultValue = "LICENSE.txt" )
178     private String headerLocation;
179 
180     /**
181      * Specifies the cache file used to speed up Checkstyle on successive runs.
182      */
183     @Parameter( defaultValue = "${project.build.directory}/checkstyle-cachefile" )
184     private String cacheFile;
185 
186     /**
187      * <p>
188      * Specifies the location of the suppressions XML file to use.
189      * </p>
190      * <p/>
191      * <p>
192      * This parameter is resolved as resource, URL, then file. If successfully
193      * resolved, the contents of the suppressions XML is copied into the
194      * <code>${project.build.directory}/checkstyle-supressions.xml</code> file
195      * before being passed to Checkstyle for loading.
196      * </p>
197      * <p/>
198      * <p>
199      * See <code>suppressionsFileExpression</code> for the property that will
200      * be made available to your checkstyle configuration.
201      * </p>
202      *
203      * @since 2.0-beta-2
204      */
205     @Parameter( property = "checkstyle.suppressions.location" )
206     private String suppressionsLocation;
207 
208     /**
209      * The key to be used in the properties for the suppressions file.
210      *
211      * @since 2.1
212      */
213     @Parameter( property = "checkstyle.suppression.expression", defaultValue = "checkstyle.suppressions.file" )
214     private String suppressionsFileExpression;
215 
216     /**
217      * Specifies if the build should fail upon a violation.
218      */
219     @Parameter( defaultValue = "false" )
220     private boolean failsOnError;
221 
222     /**
223      * Specifies the location of the source directory to be used for Checkstyle.
224      */
225     @Parameter( defaultValue = "${project.build.sourceDirectory}", required = true )
226     private File sourceDirectory;
227 
228     /**
229      * Specifies the location of the test source directory to be used for
230      * Checkstyle.
231      *
232      * @since 2.2
233      */
234     @Parameter( defaultValue = "${project.build.testSourceDirectory}" )
235     private File testSourceDirectory;
236 
237     /**
238      * Include or not the test source directory to be used for Checkstyle.
239      *
240      * @since 2.2
241      */
242     @Parameter( defaultValue = "false" )
243     private boolean includeTestSourceDirectory;
244 
245     /**
246      * Output errors to console.
247      */
248     @Parameter( defaultValue = "false" )
249     private boolean consoleOutput;
250 
251     /**
252      * The file encoding to use when reading the source files. If the property <code>project.build.sourceEncoding</code>
253      * is not set, the platform default encoding is used. <strong>Note:</strong> This parameter always overrides the
254      * property <code>charset</code> from Checkstyle's <code>TreeWalker</code> module.
255      *
256      * @since 2.2
257      */
258     @Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
259     private String encoding;
260 
261     /**
262      * The projects in the reactor for aggregation report.
263      *
264      * @since 2.8
265      */
266     @Parameter( property = "reactorProjects", readonly = true )
267     private List<MavenProject> reactorProjects;
268 
269     /** {@inheritDoc} */
270     protected MavenProject getProject()
271     {
272         return project;
273     }
274 
275     /**
276      * {@inheritDoc}
277      */
278     protected CheckstyleExecutorRequest createRequest()
279             throws MavenReportException
280     {
281         CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
282         request.setAggregate(true)
283             .setReactorProjects(reactorProjects)
284             .setConsoleListener(getConsoleListener()).setConsoleOutput(consoleOutput)
285             .setExcludes(excludes).setFailsOnError(failsOnError).setIncludes(includes)
286             .setIncludeResources( includeResources )
287             .setIncludeTestResources( includeTestResources )
288             .setResourceIncludes( resourceIncludes )
289             .setResourceExcludes( resourceExcludes )
290             .setIncludeTestSourceDirectory(includeTestSourceDirectory).setListener( getListener() )
291             .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory ).setResources( resources )
292             .setTestResources( testResources )
293             .setStringOutputStream(stringOutputStream).setSuppressionsLocation( suppressionsLocation )
294             .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation )
295             .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
296             .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
297             .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
298         return request;
299     }
300 
301 
302     /** {@inheritDoc} */
303     public String getOutputName()
304     {
305         return "checkstyle-aggregate";
306     }
307 
308     /** {@inheritDoc} */
309     public boolean canGenerateReport()
310     {
311         // TODO: would be good to scan the files here
312         return !skip && project.isExecutionRoot() && reactorProjects.size() > 1;
313     }
314 }