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                  .setTestResources(testResources)
68                  .setStringOutputStream(stringOutputStream)
69                  .setSuppressionsLocation(suppressionsLocation)
70                  .setTestSourceDirectories(getTestSourceDirectories())
71                  .setPropertyExpansion(propertyExpansion)
72                  .setHeaderLocation(headerLocation)
73                  .setCacheFile(cacheFile)
74                  .setSuppressionsFileExpression(suppressionsFileExpression)
75                  .setEncoding(getInputEncoding())
76                  .setPropertiesLocation(propertiesLocation);
77          return request;
78      }
79  
80      /** {@inheritDoc} */
81      public String getOutputName() {
82          return "checkstyle";
83      }
84  
85      /** {@inheritDoc} */
86      public boolean canGenerateReport() {
87          if (skip) {
88              return false;
89          }
90  
91          // TODO: would be good to scan the files here
92          for (File sourceDirectory : getSourceDirectories()) {
93              if (sourceDirectory.exists()) {
94                  return true;
95              }
96          }
97  
98          if (includeTestSourceDirectory) {
99              for (File testSourceDirectory : getTestSourceDirectories()) {
100                 if (testSourceDirectory.exists()) {
101                     return true;
102                 }
103             }
104         }
105 
106         return ((includeResources && hasResources(resources)) || (includeTestResources && hasResources(testResources)));
107     }
108 
109     /**
110      * Check if any of the resources exist.
111      * @param resources The resources to check
112      * @return <code>true</code> if the resource directory exist
113      */
114     private boolean hasResources(List<Resource> resources) {
115         for (Resource resource : resources) {
116             if (new File(resource.getDirectory()).exists()) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 }