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