View Javadoc
1   package org.apache.maven.plugins.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.util.List;
24  
25  import org.apache.maven.model.Resource;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.ResolutionScope;
28  import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
29  import org.apache.maven.project.MavenProject;
30  import org.apache.maven.reporting.MavenReportException;
31  
32  /**
33   * A reporting task that performs Checkstyle analysis and generates an HTML
34   * report on any violations that Checkstyle finds.
35   *
36   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
37   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
38   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
39   *
40   */
41  @Mojo( name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true )
42  public class CheckstyleReport
43      extends AbstractCheckstyleReport
44  {
45  
46      /** {@inheritDoc} */
47      protected MavenProject getProject()
48      {
49          return project;
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      protected CheckstyleExecutorRequest createRequest()
56              throws MavenReportException
57      {
58          CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
59          request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
60              .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
61              .setResourceIncludes( resourceIncludes )
62              .setResourceExcludes( resourceExcludes )
63              .setIncludeResources( includeResources )
64              .setIncludeTestResources( includeTestResources )
65              .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
66              .setProject( project ).setSourceDirectories( getSourceDirectories() )
67              .setResources( resources )
68              .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
69              .setTestSourceDirectories( getTestSourceDirectories() ).setConfigLocation( configLocation )
70              .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
71              .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
72              .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
73          return request;
74      }
75  
76      /** {@inheritDoc} */
77      public String getOutputName()
78      {
79          return "checkstyle";
80      }
81  
82      /** {@inheritDoc} */
83      public boolean canGenerateReport()
84      {
85          if ( skip )
86          {
87              return false;
88          }
89          
90          // TODO: would be good to scan the files here
91          for ( File sourceDirectory : getSourceDirectories() )
92          {
93              if ( sourceDirectory.exists() )
94              {
95                  return true;
96              }
97          }
98          
99          if ( includeTestSourceDirectory )
100         {
101             for ( File testSourceDirectory : getTestSourceDirectories() )
102             {
103                 if ( testSourceDirectory.exists() )
104                 {
105                     return true;
106                 }
107             }
108         }
109         
110         return ( ( includeResources && hasResources( resources ) )
111             || ( includeTestResources && hasResources( testResources ) )
112         );
113     }
114 
115     /**
116      * Check if any of the resources exist.
117      * @param resources The resources to check
118      * @return <code>true</code> if the resource directory exist
119      */
120     private boolean hasResources( List<Resource> resources )
121     {
122         for ( Resource resource : resources )
123         {
124             if ( new File( resource.getDirectory() ).exists() )
125             {
126                 return true;
127             }
128         }
129       return false;
130     }
131 
132 }