View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.checkstyle;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.model.Resource;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.plugins.annotations.ResolutionScope;
27  import org.apache.maven.plugins.checkstyle.exec.CheckstyleExecutorRequest;
28  import org.apache.maven.reporting.MavenReportException;
29  
30  /**
31   * A reporting task that performs Checkstyle analysis and generates an HTML
32   * report on any violations that Checkstyle finds.
33   *
34   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
35   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
36   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
37   *
38   */
39  @Mojo(name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
40  public class CheckstyleReport extends AbstractCheckstyleReport {
41      /**
42       * {@inheritDoc}
43       */
44      protected CheckstyleExecutorRequest createRequest() throws MavenReportException {
45          CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
46          request.setConsoleListener(getConsoleListener())
47                  .setConsoleOutput(consoleOutput)
48                  .setExcludes(excludes)
49                  .setFailsOnError(failsOnError)
50                  .setIncludes(includes)
51                  .setResourceIncludes(resourceIncludes)
52                  .setResourceExcludes(resourceExcludes)
53                  .setIncludeResources(includeResources)
54                  .setIncludeTestResources(includeTestResources)
55                  .setIncludeTestSourceDirectory(includeTestSourceDirectory)
56                  .setListener(getListener())
57                  .setProject(project)
58                  .setSourceDirectories(getSourceDirectories())
59                  .setResources(resources)
60                  .setTestResources(testResources)
61                  .setStringOutputStream(stringOutputStream)
62                  .setSuppressionsLocation(suppressionsLocation)
63                  .setTestSourceDirectories(getTestSourceDirectories())
64                  .setPropertyExpansion(propertyExpansion)
65                  .setHeaderLocation(headerLocation)
66                  .setCacheFile(cacheFile)
67                  .setSuppressionsFileExpression(suppressionsFileExpression)
68                  .setEncoding(getInputEncoding())
69                  .setPropertiesLocation(propertiesLocation);
70          return request;
71      }
72  
73      /** {@inheritDoc} */
74      public String getOutputName() {
75          return "checkstyle";
76      }
77  
78      /** {@inheritDoc} */
79      public boolean canGenerateReport() {
80          if (skip) {
81              return false;
82          }
83  
84          // TODO: would be good to scan the files here
85          for (File sourceDirectory : getSourceDirectories()) {
86              if (sourceDirectory.exists()) {
87                  return true;
88              }
89          }
90  
91          if (includeTestSourceDirectory) {
92              for (File testSourceDirectory : getTestSourceDirectories()) {
93                  if (testSourceDirectory.exists()) {
94                      return true;
95                  }
96              }
97          }
98  
99          return ((includeResources && hasResources(resources)) || (includeTestResources && hasResources(testResources)));
100     }
101 
102     /**
103      * Check if any of the resources exist.
104      * @param resources The resources to check
105      * @return <code>true</code> if the resource directory exist
106      */
107     private boolean hasResources(List<Resource> resources) {
108         for (Resource resource : resources) {
109             if (new File(resource.getDirectory()).exists()) {
110                 return true;
111             }
112         }
113         return false;
114     }
115 }