1 package org.apache.maven.plugin.pmd;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.IOException;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30
31 /**
32 * Fail the build if there were any PMD violations in the source code.
33 *
34 * @since 2.0
35 * @version $Id: PmdViolationCheckMojo.html 816691 2012-05-08 15:16:42Z hboutemy $
36 * @goal check
37 * @phase verify
38 * @execute goal="pmd"
39 */
40 public class PmdViolationCheckMojo
41 extends AbstractPmdViolationCheckMojo
42 {
43 /**
44 * What priority level to fail the build on. Failures at or above this level
45 * will stop the build. Anything below will be warnings and will be
46 * displayed in the build output if verbose=true. Note: Minimum Priority = 5
47 * Maximum Priority = 0
48 *
49 * @parameter expression="${pmd.failurePriority}" default-value="5"
50 * @required
51 */
52 private int failurePriority;
53
54 /**
55 * Skip the PMD checks. Most useful on the command line
56 * via "-Dpmd.skip=true".
57 *
58 * @parameter expression="${pmd.skip}" default-value="false"
59 */
60 private boolean skip;
61
62 /** {@inheritDoc} */
63 public void execute()
64 throws MojoExecutionException, MojoFailureException
65 {
66 if ( !skip )
67 {
68 executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority );
69 }
70 }
71
72 /** {@inheritDoc} */
73 protected void printError( Map item, String severity )
74 {
75
76 StringBuffer buff = new StringBuffer( 100 );
77 buff.append( "PMD " + severity + ": " );
78 if ( item.containsKey( "class" ) )
79 {
80 if ( item.containsKey( "package" ) )
81 {
82 buff.append( item.get( "package" ) );
83 buff.append( "." );
84 }
85 buff.append( item.get( "class" ) );
86 }
87 else
88 {
89 buff.append( item.get( "filename" ) );
90 }
91 buff.append( ":" );
92 buff.append( item.get( "beginline" ) );
93 buff.append( " Rule:" ).append( item.get( "rule" ) );
94 buff.append( " Priority:" ).append( item.get( "priority" ) );
95 buff.append( " " ).append( item.get( "text" ) ).append( "." );
96
97 this.getLog().info( buff.toString() );
98 }
99
100 /** {@inheritDoc} */
101 protected Map getErrorDetails( XmlPullParser xpp )
102 throws XmlPullParserException, IOException
103 {
104 int index = 0;
105 int attributeCount = 0;
106 HashMap msgs = new HashMap();
107
108 attributeCount = xpp.getAttributeCount();
109 while ( index < attributeCount )
110 {
111
112 msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) );
113
114 index++;
115 }
116
117 // get the tag's text
118 if ( xpp.next() == XmlPullParser.TEXT )
119 {
120 msgs.put( "text", xpp.getText().trim() );
121 }
122 return msgs;
123 }
124 }