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