1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36
37
38
39
40 @Mojo(name = "checkstyle", requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
41 public class CheckstyleReport extends AbstractCheckstyleReport {
42
43
44 protected MavenProject getProject() {
45 return project;
46 }
47
48
49
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
80 public String getOutputName() {
81 return "checkstyle";
82 }
83
84
85 public boolean canGenerateReport() {
86 if (skip) {
87 return false;
88 }
89
90
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
110
111
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 }