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