1 package org.apache.maven.plugin.checkstyle;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedReader;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStream;
29 import java.io.Reader;
30
31 import org.apache.maven.plugin.AbstractMojo;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.apache.maven.project.MavenProject;
35 import org.codehaus.plexus.util.ReaderFactory;
36 import org.codehaus.plexus.util.StringUtils;
37 import org.codehaus.plexus.util.xml.pull.MXParser;
38 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
39 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
40
41 import com.puppycrawl.tools.checkstyle.DefaultLogger;
42 import com.puppycrawl.tools.checkstyle.XMLLogger;
43 import com.puppycrawl.tools.checkstyle.api.AuditListener;
44 import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class CheckstyleViolationCheckMojo
59 extends AbstractMojo
60 {
61
62
63
64
65
66
67
68
69 private File outputFile;
70
71
72
73
74
75
76
77 private String outputFileFormat;
78
79
80
81
82
83
84
85 private boolean failOnViolation;
86
87
88
89
90
91
92
93
94 private int maxAllowedViolations = 0;
95
96
97
98
99
100
101
102
103 private String violationSeverity = "error";
104
105
106
107
108
109
110
111 private boolean skip;
112
113
114
115
116
117
118
119 private boolean skipExec;
120
121
122
123
124
125
126
127 private boolean logViolationsToConsole;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 private String configLocation;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 private String propertiesLocation;
188
189
190
191
192
193
194 private String propertyExpansion;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221 private String headerLocation;
222
223
224
225
226
227
228 private String cacheFile;
229
230
231
232
233
234
235
236
237 private String suppressionsFileExpression;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 private String suppressionsLocation;
260
261
262
263
264
265
266
267
268
269 private String encoding;
270
271
272
273
274
275
276
277 protected CheckstyleExecutor checkstyleExecutor;
278
279
280
281
282
283
284 private boolean consoleOutput;
285
286
287
288
289
290
291
292
293 protected MavenProject project;
294
295
296
297
298
299
300
301 private File useFile;
302
303
304
305
306
307
308
309 private String excludes;
310
311
312
313
314
315
316
317 private String includes;
318
319
320
321
322
323
324 private boolean failsOnError;
325
326
327
328
329
330
331
332
333 private File testSourceDirectory;
334
335
336
337
338
339
340
341 private boolean includeTestSourceDirectory;
342
343
344
345
346
347
348
349 private File sourceDirectory;
350
351 private ByteArrayOutputStream stringOutputStream;
352
353
354
355 public void execute()
356 throws MojoExecutionException, MojoFailureException
357 {
358
359 if ( !skip )
360 {
361
362 if ( !skipExec )
363 {
364
365 ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
366
367 try
368 {
369 CheckstyleExecutorRequest request = new CheckstyleExecutorRequest();
370 request.setConsoleListener( getConsoleListener() ).setConsoleOutput( consoleOutput )
371 .setExcludes( excludes ).setFailsOnError( failsOnError ).setIncludes( includes )
372 .setIncludeTestSourceDirectory( includeTestSourceDirectory ).setListener( getListener() )
373 .setLog( getLog() ).setProject( project ).setSourceDirectory( sourceDirectory )
374 .setStringOutputStream( stringOutputStream ).setSuppressionsLocation( suppressionsLocation )
375 .setTestSourceDirectory( testSourceDirectory ).setConfigLocation( configLocation )
376 .setPropertyExpansion( propertyExpansion ).setHeaderLocation( headerLocation )
377 .setCacheFile( cacheFile ).setSuppressionsFileExpression( suppressionsFileExpression )
378 .setEncoding( encoding ).setPropertiesLocation( propertiesLocation );
379
380 checkstyleExecutor.executeCheckstyle( request );
381
382 }
383 catch ( CheckstyleException e )
384 {
385 throw new MojoExecutionException( "Failed during checkstyle configuration", e );
386 }
387 catch ( CheckstyleExecutorException e )
388 {
389 throw new MojoExecutionException( "Failed during checkstyle execution", e );
390 }
391 finally
392 {
393
394 Thread.currentThread().setContextClassLoader( currentClassLoader );
395 }
396
397 }
398 if ( !"xml".equals( outputFileFormat ) )
399 {
400 throw new MojoExecutionException( "Output format is '" + outputFileFormat
401 + "', checkstyle:check requires format to be 'xml'." );
402 }
403
404 if ( !outputFile.exists() )
405 {
406 getLog().info(
407 "Unable to perform checkstyle:check, "
408 + "unable to find checkstyle:checkstyle outputFile." );
409 return;
410 }
411
412 try
413 {
414 XmlPullParser xpp = new MXParser();
415 Reader freader = ReaderFactory.newXmlReader( outputFile );
416 BufferedReader breader = new BufferedReader( freader );
417 xpp.setInput( breader );
418
419 int violations = countViolations( xpp );
420 if ( violations > maxAllowedViolations )
421 {
422 if ( failOnViolation )
423 {
424 String msg = "You have " + violations + " Checkstyle violation"
425 + ( ( violations > 1 ) ? "s" : "" ) + ".";
426 if ( maxAllowedViolations > 0 )
427 {
428 msg += " The maximum number of allowed violations is " + maxAllowedViolations + ".";
429 }
430 throw new MojoFailureException( msg );
431 }
432
433 getLog().warn( "checkstyle:check violations detected but failOnViolation set to false" );
434 }
435 }
436 catch ( IOException e )
437 {
438 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
439 + outputFile.getAbsolutePath(), e );
440 }
441 catch ( XmlPullParserException e )
442 {
443 throw new MojoExecutionException( "Unable to read Checkstyle results xml: "
444 + outputFile.getAbsolutePath(), e );
445 }
446 }
447 }
448
449 private int countViolations( XmlPullParser xpp )
450 throws XmlPullParserException, IOException
451 {
452 int count = 0;
453
454 int eventType = xpp.getEventType();
455 String file = "";
456 while ( eventType != XmlPullParser.END_DOCUMENT )
457 {
458 if ( eventType == XmlPullParser.START_TAG && "file".equals( xpp.getName() ) )
459 {
460 file = xpp.getAttributeValue( "", "name" );
461 file = file.substring( file.lastIndexOf( File.separatorChar ) + 1 );
462 }
463
464 if ( eventType == XmlPullParser.START_TAG && "error".equals( xpp.getName() )
465 && isViolation( xpp.getAttributeValue( "", "severity" ) ) )
466 {
467 if ( logViolationsToConsole )
468 {
469 StringBuffer stb = new StringBuffer();
470 stb.append( file );
471 stb.append( '[' );
472 stb.append( xpp.getAttributeValue( "", "line" ) );
473 stb.append( ':' );
474 stb.append( xpp.getAttributeValue( "", "column" ) );
475 stb.append( "] " );
476 stb.append( xpp.getAttributeValue( "", "message" ) );
477 getLog().error( stb.toString() );
478 }
479 count++;
480 }
481 eventType = xpp.next();
482 }
483
484 return count;
485 }
486
487
488
489
490
491
492
493 private boolean isViolation( String severity )
494 {
495 if ( "error".equals( severity ) )
496 {
497 return "error".equals( violationSeverity ) || "warning".equals( violationSeverity )
498 || "info".equals( violationSeverity );
499 }
500 else if ( "warning".equals( severity ) )
501 {
502 return "warning".equals( violationSeverity ) || "info".equals( violationSeverity );
503 }
504 else if ( "info".equals( severity ) )
505 {
506 return "info".equals( violationSeverity );
507 }
508 else
509 {
510 return false;
511 }
512 }
513 private DefaultLogger getConsoleListener()
514 throws MojoExecutionException
515 {
516 DefaultLogger consoleListener;
517
518 if ( useFile == null )
519 {
520 stringOutputStream = new ByteArrayOutputStream();
521 consoleListener = new DefaultLogger( stringOutputStream, false );
522 }
523 else
524 {
525 OutputStream out = getOutputStream( useFile );
526
527 consoleListener = new DefaultLogger( out, true );
528 }
529
530 return consoleListener;
531 }
532
533 private OutputStream getOutputStream( File file )
534 throws MojoExecutionException
535 {
536 File parentFile = file.getAbsoluteFile().getParentFile();
537
538 if ( !parentFile.exists() )
539 {
540 parentFile.mkdirs();
541 }
542
543 FileOutputStream fileOutputStream;
544 try
545 {
546 fileOutputStream = new FileOutputStream( file );
547 }
548 catch ( FileNotFoundException e )
549 {
550 throw new MojoExecutionException( "Unable to create output stream: " + file, e );
551 }
552 return fileOutputStream;
553 }
554
555 private AuditListener getListener()
556 throws MojoFailureException, MojoExecutionException
557 {
558 AuditListener listener = null;
559
560 if ( StringUtils.isNotEmpty( outputFileFormat ) )
561 {
562 File resultFile = outputFile;
563
564 OutputStream out = getOutputStream( resultFile );
565
566 if ( "xml".equals( outputFileFormat ) )
567 {
568 listener = new XMLLogger( out, true );
569 }
570 else if ( "plain".equals( outputFileFormat ) )
571 {
572 listener = new DefaultLogger( out, true );
573 }
574 else
575 {
576 throw new MojoFailureException( "Invalid output file format: (" + outputFileFormat
577 + "). Must be 'plain' or 'xml'." );
578 }
579 }
580
581 return listener;
582 }
583
584 }