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