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 1663881 2015-03-04 08:10:45Z dennisl $
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( "maven", "config/maven_checks.xml" );
63  
64          FORMAT_TO_CONFIG_LOCATION = Collections.unmodifiableMap( fmt2Cfg );
65      }
66  
67      /**
68       * Specifies what predefined check set to use. Available sets are "sun" (for
69       * the Sun coding conventions), and "maven".
70       *
71       * @deprecated Use configLocation instead.
72       */
73      @Parameter( defaultValue = "sun" )
74      private String format;
75  
76      /**
77       * Specifies the location of the Checkstyle properties file that will be used to
78       * check the source.
79       *
80       * @deprecated Use propertiesLocation instead.
81       */
82      @Parameter
83      private File propertiesFile;
84  
85      /**
86       * Specifies the URL of the Checkstyle properties that will be used to check
87       * the source.
88       *
89       * @deprecated Use propertiesLocation instead.
90       */
91      @Parameter
92      private URL propertiesURL;
93  
94      /**
95       * Specifies the location of the License file (a.k.a. the header file) that
96       * is used by Checkstyle to verify that source code has the correct
97       * license header.
98       *
99       * @deprecated Use headerLocation instead.
100      */
101     @Parameter( defaultValue = "${basedir}/LICENSE.txt" )
102     private File headerFile;
103 
104     /**
105      * Specifies the location of the suppressions XML file to use. The plugin
106      * defines a Checkstyle property named
107      * <code>checkstyle.suppressions.file</code> with the value of this
108      * property. This allows using the Checkstyle property in your own custom
109      * Checkstyle configuration file when specifying a suppressions file.
110      *
111      * @deprecated Use suppressionsLocation instead.
112      */
113     @Parameter
114     private String suppressionsFile;
115 
116     /**
117      * <p>
118      * Specifies the location of the package names XML to be used to configure
119      * the Checkstyle <a
120      * href="http://checkstyle.sourceforge.net/config.html#Packages">Packages</a>.
121      * </p>
122      * <p/>
123      * <p>
124      * This parameter is resolved as resource, URL, then file. If resolved to a
125      * resource, or a URL, the contents of the package names XML is copied into
126      * the <code>${project.build.directory}/checkstyle-packagenames.xml</code>
127      * file before being passed to Checkstyle for loading.
128      * </p>
129      *
130      * @since 2.0-beta-2
131      */
132     @Parameter
133     private String packageNamesLocation;
134 
135     /**
136      * Specifies the location of the package names XML to be used to configure
137      * Checkstyle.
138      *
139      * @deprecated Use packageNamesLocation instead.
140      */
141     @Parameter
142     private String packageNamesFile;
143 
144     /** {@inheritDoc} */
145     protected MavenProject getProject()
146     {
147         return project;
148     }
149 
150     /** {@inheritDoc} */
151     public void executeReport( Locale locale )
152         throws MavenReportException
153     {
154         mergeDeprecatedInfo();
155         super.executeReport( locale );
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     protected CheckstyleExecutorRequest createRequest()
162             throws MavenReportException
163     {
164         CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
165         request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
166             .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
167             .setResourceIncludes( resourceIncludes )
168             .setResourceExcludes( resourceExcludes )
169             .setIncludeResources( includeResources )
170             .setIncludeTestResources( includeTestResources )
171             .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
172             .setProject( project ).setSourceDirectories( getSourceDirectories() )
173             .setResources( resources )
174             .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
175             .setTestSourceDirectories( getTestSourceDirectories() ).setConfigLocation( configLocation )
176             .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
177             .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
178             .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
179         return request;
180     }
181 
182     /** {@inheritDoc} */
183     public String getOutputName()
184     {
185         return "checkstyle";
186     }
187 
188     /** {@inheritDoc} */
189     public boolean canGenerateReport()
190     {
191         if ( skip )
192         {
193             return false;
194         }
195         
196         // TODO: would be good to scan the files here
197         for ( File sourceDirectory : getSourceDirectories() )
198         {
199             if ( sourceDirectory.exists() )
200             {
201                 return true;
202             }
203         }
204         
205         if ( includeTestSourceDirectory )
206         {
207             for ( File testSourceDirectory : getTestSourceDirectories() )
208             {
209                 if ( testSourceDirectory.exists() )
210                 {
211                     return true;
212                 }
213             }
214         }
215         
216         return ( ( includeResources && hasResources( resources ) )
217             || ( includeTestResources && hasResources( testResources ) )
218         );
219     }
220 
221     /**
222      * Check if any of the resources exist.
223      * @param resources The resources to check
224      * @return <code>true</code> if the resource directory exist
225      */
226     private boolean hasResources( List<Resource> resources )
227     {
228         for ( Resource resource : resources )
229         {
230             if ( new File( resource.getDirectory() ).exists() )
231             {
232                 return true;
233             }
234         }
235       return false;
236     }
237 
238     /**
239      * Merge in the deprecated parameters to the new ones, unless the new
240      * parameters have values.
241      * @throws MavenReportException 
242      *
243      * @deprecated Remove when deprecated params are removed.
244      */
245     private void mergeDeprecatedInfo()
246         throws MavenReportException
247     {
248         if ( "config/sun_checks.xml".equals( configLocation ) && !"sun".equals( format ) )
249         {
250             configLocation = FORMAT_TO_CONFIG_LOCATION.get( format );
251 
252             throw new MavenReportException( "'format' parameter is deprecated: please replace with <configLocation>"
253                 + configLocation + "</configLocation>." );
254         }
255 
256         if ( StringUtils.isEmpty( propertiesLocation ) )
257         {
258             if ( propertiesFile != null )
259             {
260                 propertiesLocation = propertiesFile.getPath();
261 
262                 throw new MavenReportException( "'propertiesFile' parameter is deprecated: please replace with "
263                     + "<propertiesLocation>" + propertiesLocation + "</propertiesLocation>." );
264             }
265             else if ( propertiesURL != null )
266             {
267                 propertiesLocation = propertiesURL.toExternalForm();
268 
269                 throw new MavenReportException( "'propertiesURL' parameter is deprecated: please replace with "
270                                 + "<propertiesLocation>" + propertiesLocation + "</propertiesLocation>." );
271             }
272         }
273 
274         if ( "LICENSE.txt".equals( headerLocation ) )
275         {
276             File defaultHeaderFile = new File( project.getBasedir(), "LICENSE.txt" );
277             if ( !defaultHeaderFile.equals( headerFile ) )
278             {
279                 headerLocation = headerFile.getPath();
280             }
281         }
282 
283         if ( StringUtils.isEmpty( suppressionsLocation ) )
284         {
285             suppressionsLocation = suppressionsFile;
286 
287             if ( StringUtils.isNotEmpty( suppressionsFile ) )
288             {
289                 throw new MavenReportException( "'suppressionsFile' parameter is deprecated: please replace with "
290                     + "<suppressionsLocation>" + suppressionsLocation + "</suppressionsLocation>." );
291             }
292         }
293 
294         if ( StringUtils.isEmpty( packageNamesLocation ) )
295         {
296             packageNamesLocation = packageNamesFile;
297 
298             if ( StringUtils.isNotEmpty( packageNamesFile ) )
299             {
300                 throw new MavenReportException( "'packageNamesFile' parameter is deprecated: please replace with "
301                     + "<packageNamesFile>" + suppressionsLocation + "</packageNamesFile>." );
302             }
303         }
304     }
305 
306 }