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 java.io.File;
23  import java.net.URL;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  
30  import org.apache.maven.model.Resource;
31  import org.apache.maven.plugin.checkstyle.exec.CheckstyleExecutorRequest;
32  import org.apache.maven.plugins.annotations.Mojo;
33  import org.apache.maven.plugins.annotations.Parameter;
34  import org.apache.maven.plugins.annotations.ResolutionScope;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.reporting.MavenReportException;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * A reporting task that performs Checkstyle analysis and generates an HTML
41   * report on any violations that Checkstyle finds.
42   *
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
45   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
46   * @version $Id: CheckstyleReport.java 1621538 2014-08-30 21:12:11Z hboutemy $
47   */
48  @Mojo( name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
49  public class CheckstyleReport
50      extends AbstractCheckstyleReport
51  {
52      /**
53       * @deprecated Remove with format parameter.
54       */
55      private static final Map<String, String> FORMAT_TO_CONFIG_LOCATION;
56  
57      static
58      {
59          Map<String, String> fmt2Cfg = new HashMap<String, String>();
60  
61          fmt2Cfg.put( "sun", "config/sun_checks.xml" );
62          fmt2Cfg.put( "turbine", "config/turbine_checks.xml" );
63          fmt2Cfg.put( "avalon", "config/avalon_checks.xml" );
64          fmt2Cfg.put( "maven", "config/maven_checks.xml" );
65  
66          FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg );
67      }
68  
69      /**
70       * Specifies what predefined check set to use. Available sets are "sun" (for
71       * the Sun coding conventions), "turbine", and "avalon".
72       *
73       * @deprecated Use configLocation instead.
74       */
75      @Parameter( defaultValue = "sun" )
76      private String format;
77  
78      /**
79       * Specifies the location of the Checkstyle properties file that will be used to
80       * check the source.
81       *
82       * @deprecated Use propertiesLocation instead.
83       */
84      @Parameter
85      private File propertiesFile;
86  
87      /**
88       * Specifies the URL of the Checkstyle properties that will be used to check
89       * the source.
90       *
91       * @deprecated Use propertiesLocation instead.
92       */
93      @Parameter
94      private URL propertiesURL;
95  
96      /**
97       * Specifies the location of the License file (a.k.a. the header file) that
98       * is used by Checkstyle to verify that source code has the correct
99       * license header.
100      *
101      * @deprecated Use headerLocation instead.
102      */
103     @Parameter( defaultValue = "${basedir}/LICENSE.txt" )
104     private File headerFile;
105 
106     /**
107      * Specifies the location of the suppressions XML file to use. The plugin
108      * defines a Checkstyle property named
109      * <code>checkstyle.suppressions.file</code> with the value of this
110      * property. This allows using the Checkstyle property in your own custom
111      * Checkstyle configuration file when specifying a suppressions file.
112      *
113      * @deprecated Use suppressionsLocation instead.
114      */
115     @Parameter
116     private String suppressionsFile;
117 
118     /**
119      * <p>
120      * Specifies the location of the package names XML to be used to configure
121      * the Checkstyle <a
122      * href="http://checkstyle.sourceforge.net/config.html#Packages">Packages</a>.
123      * </p>
124      * <p/>
125      * <p>
126      * This parameter is resolved as resource, URL, then file. If resolved to a
127      * resource, or a URL, the contents of the package names XML is copied into
128      * the <code>${project.build.directory}/checkstyle-packagenames.xml</code>
129      * file before being passed to Checkstyle for loading.
130      * </p>
131      *
132      * @since 2.0-beta-2
133      */
134     @Parameter
135     private String packageNamesLocation;
136 
137     /**
138      * Specifies the location of the package names XML to be used to configure
139      * Checkstyle.
140      *
141      * @deprecated Use packageNamesLocation instead.
142      */
143     @Parameter
144     private String packageNamesFile;
145 
146     /** {@inheritDoc} */
147     protected MavenProject getProject()
148     {
149         return project;
150     }
151 
152     /** {@inheritDoc} */
153     public void executeReport( Locale locale )
154         throws MavenReportException
155     {
156         mergeDeprecatedInfo();
157         super.executeReport( locale );
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     protected CheckstyleExecutorRequest createRequest()
164             throws MavenReportException
165     {
166         CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
167         request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
168             .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
169             .setResourceIncludes( resourceIncludes )
170             .setResourceExcludes( resourceExcludes )
171             .setIncludeResources( includeResources )
172             .setIncludeTestResources( includeTestResources )
173             .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
174             .setLog( getLog() ).setProject( project ).setSourceDirectories( getSourceDirectories() ).setResources( resources )
175             .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
176             .setTestSourceDirectories( getTestSourceDirectories() ).setConfigLocation( configLocation )
177             .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
178             .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
179             .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
180         return request;
181     }
182 
183     /** {@inheritDoc} */
184     public String getOutputName()
185     {
186         return "checkstyle";
187     }
188 
189     /** {@inheritDoc} */
190     public boolean canGenerateReport()
191     {
192         if ( skip )
193         {
194             return false;
195         }
196         
197         // TODO: would be good to scan the files here
198         for ( File sourceDirectory : getSourceDirectories() )
199         {
200             if ( sourceDirectory.exists() )
201             {
202                 return true;
203             }
204         }
205         
206         if ( includeTestSourceDirectory )
207         {
208             for ( File testSourceDirectory : getTestSourceDirectories() )
209             {
210                 if ( testSourceDirectory.exists() )
211                 {
212                     return true;
213                 }
214             }
215         }
216         
217         return ( ( includeResources && hasResources( resources ) )
218             || ( includeTestResources && hasResources( testResources ) )
219         );
220     }
221 
222     /**
223      * Check if any of the resources exist.
224      * @param resources The resources to check
225      * @return <code>true</code> if the resource directory exist
226      */
227     private boolean hasResources( List<Resource> resources )
228     {
229         for ( Resource resource : resources )
230         {
231             if ( new File( resource.getDirectory() ).exists() )
232             {
233                 return true;
234             }
235         }
236       return false;
237     }
238 
239     /**
240      * Merge in the deprecated parameters to the new ones, unless the new
241      * parameters have values.
242      *
243      * @deprecated Remove when deprecated params are removed.
244      */
245     private void mergeDeprecatedInfo()
246     {
247         if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
248         {
249             configLocation = FORMAT_TO_CONFIG_LOCATION.get( format );
250         }
251 
252         if ( StringUtils.isEmpty( propertiesLocation ) )
253         {
254             if ( propertiesFile != null )
255             {
256                 propertiesLocation = propertiesFile.getPath();
257             }
258             else if ( propertiesURL != null )
259             {
260                 propertiesLocation = propertiesURL.toExternalForm();
261             }
262         }
263 
264         if ( "LICENSE.txt".equals( headerLocation ) )
265         {
266             File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
267             if ( !defaultHeaderFile.equals( headerFile ) )
268             {
269                 headerLocation = headerFile.getPath();
270             }
271         }
272 
273         if ( StringUtils.isEmpty( suppressionsLocation ) )
274         {
275             suppressionsLocation = suppressionsFile;
276         }
277 
278         if ( StringUtils.isEmpty( packageNamesLocation ) )
279         {
280             packageNamesLocation = packageNamesFile;
281         }
282     }
283 
284 }