1 package org.apache.maven.plugins.pmd;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.plugin.MojoFailureException;
30 import org.apache.maven.plugins.annotations.Parameter;
31 import org.apache.maven.project.MavenProject;
32 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
33
34
35
36
37
38
39
40
41
42 public abstract class AbstractPmdViolationCheckMojo<D>
43 extends AbstractMojo
44 {
45
46
47
48 @Parameter( property = "project.build.directory", required = true )
49 private File targetDirectory;
50
51
52
53
54 @Parameter( property = "pmd.failOnViolation", defaultValue = "true", required = true )
55 protected boolean failOnViolation;
56
57
58
59
60
61
62 @Parameter( property = "aggregate", defaultValue = "false" )
63 protected boolean aggregate;
64
65
66
67
68 @Parameter( property = "pmd.verbose", defaultValue = "false" )
69 private boolean verbose;
70
71
72
73
74
75
76 @Parameter( property = "pmd.printFailingErrors", defaultValue = "false" )
77 private boolean printFailingErrors;
78
79
80
81
82
83
84
85
86
87 @Parameter( property = "pmd.excludeFromFailureFile", defaultValue = "" )
88 private String excludeFromFailureFile;
89
90
91 private final ExcludeFromFile<D> excludeFromFile;
92
93
94
95
96
97 protected AbstractPmdViolationCheckMojo( ExcludeFromFile<D> excludeFromFile )
98 {
99 this.excludeFromFile = excludeFromFile;
100 }
101
102
103
104
105 @Parameter( defaultValue = "${project}", readonly = true, required = true )
106 protected MavenProject project;
107
108 protected void executeCheck( final String filename, final String tagName, final String key,
109 final int failurePriority )
110 throws MojoFailureException, MojoExecutionException
111 {
112 if ( aggregate && !project.isExecutionRoot() )
113 {
114 return;
115 }
116
117 if ( "pom".equals( project.getPackaging() ) && !aggregate )
118 {
119 return;
120 }
121
122 excludeFromFile.loadExcludeFromFailuresData( excludeFromFailureFile );
123 final File outputFile = new File( targetDirectory, filename );
124
125 if ( outputFile.exists() )
126 {
127 try
128 {
129 final ViolationDetails<D> violations = getViolations( outputFile, failurePriority );
130
131 final List<D> failures = violations.getFailureDetails();
132 final List<D> warnings = violations.getWarningDetails();
133
134 if ( verbose )
135 {
136 printErrors( failures, warnings );
137 }
138
139 final int failureCount = failures.size();
140 final int warningCount = warnings.size();
141
142 final String message = getMessage( failureCount, warningCount, key, outputFile );
143
144 getLog().debug( "PMD failureCount: " + failureCount + ", warningCount: " + warningCount );
145
146 if ( failureCount > 0 && isFailOnViolation() )
147 {
148 throw new MojoFailureException( message );
149 }
150
151 this.getLog().info( message );
152 }
153 catch ( final IOException e )
154 {
155 throw new MojoExecutionException(
156 "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
157 e );
158 }
159 catch ( final XmlPullParserException e )
160 {
161 throw new MojoExecutionException(
162 "Unable to read PMD results xml: " + outputFile.getAbsolutePath(),
163 e );
164 }
165 }
166 else
167 {
168 throw new MojoFailureException( "Unable to perform check, " + "unable to find " + outputFile );
169 }
170 }
171
172
173
174
175
176
177
178
179
180
181 private ViolationDetails<D> getViolations( final File analysisFile, final int failurePriority )
182 throws XmlPullParserException, IOException
183 {
184 final List<D> failures = new ArrayList<>();
185 final List<D> warnings = new ArrayList<>();
186
187 final List<D> violations = getErrorDetails( analysisFile );
188
189 for ( final D violation : violations )
190 {
191 final int priority = getPriority( violation );
192 if ( priority <= failurePriority && !excludeFromFile.isExcludedFromFailure( violation ) )
193 {
194 failures.add( violation );
195 if ( printFailingErrors )
196 {
197 printError( violation, "Failure" );
198 }
199 }
200 else
201 {
202 warnings.add( violation );
203 }
204 }
205
206 final ViolationDetails<D> details = newViolationDetailsInstance();
207 details.setFailureDetails( failures );
208 details.setWarningDetails( warnings );
209 return details;
210 }
211
212 protected abstract int getPriority( D errorDetail );
213
214 protected abstract ViolationDetails<D> newViolationDetailsInstance();
215
216
217
218
219
220
221
222 protected void printErrors( final List<D> failures, final List<D> warnings )
223 {
224 for ( final D warning : warnings )
225 {
226 printError( warning, "Warning" );
227 }
228
229 for ( final D failure : failures )
230 {
231 printError( failure, "Failure" );
232 }
233 }
234
235
236
237
238
239
240
241
242
243
244 private String getMessage( final int failureCount, final int warningCount, final String key, final File outputFile )
245 {
246 final StringBuilder message = new StringBuilder( 256 );
247 if ( failureCount > 0 || warningCount > 0 )
248 {
249 if ( failureCount > 0 )
250 {
251 message.append( "You have " ).append( failureCount ).append( " " ).append( key ).
252 append( failureCount > 1 ? "s" : "" );
253 }
254
255 if ( warningCount > 0 )
256 {
257 if ( failureCount > 0 )
258 {
259 message.append( " and " );
260 }
261 else
262 {
263 message.append( "You have " );
264 }
265 message.append( warningCount ).append( " warning" ).append( warningCount > 1 ? "s" : "" );
266 }
267
268 message.append( ". For more details see: " ).append( outputFile.getAbsolutePath() );
269 }
270 return message.toString();
271 }
272
273
274
275
276
277
278
279
280 protected abstract void printError( D item, String severity );
281
282
283
284
285
286
287
288
289
290
291 protected abstract List<D> getErrorDetails( File analysisFile )
292 throws XmlPullParserException, IOException;
293
294 public boolean isFailOnViolation()
295 {
296 return failOnViolation;
297 }
298 }