| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PmdViolationCheckMojo |
|
| 2.6666666666666665;2,667 |
| 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 | * @goal check | |
| 35 | * @phase verify | |
| 36 | * @execute goal="pmd" | |
| 37 | */ | |
| 38 | 5 | public class PmdViolationCheckMojo |
| 39 | extends AbstractPmdViolationCheckMojo | |
| 40 | { | |
| 41 | /** | |
| 42 | * What priority level to fail the build on. Failures at or above this level | |
| 43 | * will stop the build. Anything below will be warnings and will be | |
| 44 | * displayed in the build output if verbose=true. Note: Minumum Priority = 5 | |
| 45 | * Maximum Priority = 0 | |
| 46 | * | |
| 47 | * @parameter expression="${pmd.failurePriority}" default-value="5" | |
| 48 | * @required | |
| 49 | */ | |
| 50 | private int failurePriority; | |
| 51 | ||
| 52 | /** | |
| 53 | * Skip the PMD checks. Most useful on the command line | |
| 54 | * via "-Dpmd.skip=true". | |
| 55 | * | |
| 56 | * @parameter expression="${pmd.skip}" default-value="false" | |
| 57 | */ | |
| 58 | private boolean skip; | |
| 59 | ||
| 60 | /** | |
| 61 | * @see org.apache.maven.plugin.AbstractMojo#execute() | |
| 62 | */ | |
| 63 | public void execute() | |
| 64 | throws MojoExecutionException, MojoFailureException | |
| 65 | { | |
| 66 | 3 | if ( !skip ) |
| 67 | { | |
| 68 | 3 | executeCheck( "pmd.xml", "violation", "PMD violation", failurePriority ); |
| 69 | } | |
| 70 | 2 | } |
| 71 | ||
| 72 | /** | |
| 73 | * Formats the failure details and prints them as an INFO message | |
| 74 | * | |
| 75 | * @param item | |
| 76 | * parsed details about error | |
| 77 | */ | |
| 78 | protected void printError( Map item, String severity ) | |
| 79 | { | |
| 80 | ||
| 81 | 6 | StringBuffer buff = new StringBuffer( 100 ); |
| 82 | 6 | buff.append( "PMD " + severity + ": " ); |
| 83 | 6 | if ( item.containsKey( "package" ) ) |
| 84 | { | |
| 85 | 6 | buff.append( item.get( "package" ) ); |
| 86 | 6 | buff.append( "." ); |
| 87 | } | |
| 88 | 6 | if ( item.containsKey( "class" ) ) |
| 89 | { | |
| 90 | 5 | buff.append( item.get( "class" ) ); |
| 91 | 5 | buff.append( ":" ); |
| 92 | } | |
| 93 | 6 | buff.append( item.get( "beginline" ) ); |
| 94 | 6 | buff.append( " Rule:" ).append( item.get( "rule" ) ); |
| 95 | 6 | buff.append( " Priority:" ).append( item.get( "priority" ) ); |
| 96 | 6 | buff.append( " " ).append( item.get( "text" ) ).append( "." ); |
| 97 | ||
| 98 | 6 | this.getLog().info( buff.toString() ); |
| 99 | 6 | } |
| 100 | ||
| 101 | /** | |
| 102 | * Gets the attributes and text for the violation tag and puts them in a | |
| 103 | * HashMap | |
| 104 | * | |
| 105 | * @param xpp | |
| 106 | * @throws XmlPullParserException | |
| 107 | * @throws IOException | |
| 108 | */ | |
| 109 | protected Map getErrorDetails( XmlPullParser xpp ) | |
| 110 | throws XmlPullParserException, IOException | |
| 111 | { | |
| 112 | 18 | int index = 0; |
| 113 | 18 | int attributeCount = 0; |
| 114 | 18 | HashMap msgs = new HashMap(); |
| 115 | ||
| 116 | 18 | attributeCount = xpp.getAttributeCount(); |
| 117 | 207 | while ( index < attributeCount ) |
| 118 | { | |
| 119 | ||
| 120 | 189 | msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) ); |
| 121 | ||
| 122 | 189 | index++; |
| 123 | } | |
| 124 | ||
| 125 | // get the tag's text | |
| 126 | 18 | if ( xpp.next() == XmlPullParser.TEXT ) |
| 127 | { | |
| 128 | 18 | msgs.put( "text", xpp.getText().trim() ); |
| 129 | } | |
| 130 | 18 | return msgs; |
| 131 | } | |
| 132 | ||
| 133 | } |