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