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