| 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.util.ArrayList; |
| 25 | |
import java.util.Collections; |
| 26 | |
import java.util.Iterator; |
| 27 | |
import java.util.List; |
| 28 | |
import java.util.Map; |
| 29 | |
import java.util.TreeMap; |
| 30 | |
|
| 31 | |
import org.apache.maven.model.ReportPlugin; |
| 32 | |
import org.apache.maven.project.MavenProject; |
| 33 | |
import org.apache.maven.reporting.AbstractMavenReport; |
| 34 | |
import org.codehaus.doxia.site.renderer.SiteRenderer; |
| 35 | |
import org.codehaus.plexus.util.FileUtils; |
| 36 | |
import org.codehaus.plexus.util.PathTool; |
| 37 | |
import org.codehaus.plexus.util.StringUtils; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | 12 | public abstract class AbstractPmdReport |
| 45 | |
extends AbstractMavenReport |
| 46 | |
{ |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
protected File targetDirectory; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
protected String outputDirectory; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
private SiteRenderer siteRenderer; |
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
protected MavenProject project; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | 12 | protected String format = "xml"; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
private boolean linkXRef; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
private File xrefLocation; |
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
private File xrefTestLocation; |
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
private String[] excludes; |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
private String[] includes; |
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
private List compileSourceRoots; |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
private List testSourceRoots; |
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
private List excludeRoots; |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
protected boolean includeTests; |
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
protected boolean aggregate; |
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
protected List reactorProjects; |
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
protected MavenProject getProject() |
| 186 | |
{ |
| 187 | 44 | return project; |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
protected SiteRenderer getSiteRenderer() |
| 194 | |
{ |
| 195 | 19 | return siteRenderer; |
| 196 | |
} |
| 197 | |
|
| 198 | |
protected String constructXRefLocation( boolean test ) |
| 199 | |
{ |
| 200 | 11 | String location = null; |
| 201 | 11 | if ( linkXRef ) |
| 202 | |
{ |
| 203 | 5 | File xrefLoc = test ? xrefTestLocation : xrefLocation; |
| 204 | |
|
| 205 | 5 | String relativePath = PathTool.getRelativePath( outputDirectory, xrefLoc.getAbsolutePath() ); |
| 206 | 5 | if ( StringUtils.isEmpty( relativePath ) ) |
| 207 | |
{ |
| 208 | 5 | relativePath = "."; |
| 209 | |
} |
| 210 | 5 | relativePath = relativePath + "/" + xrefLoc.getName(); |
| 211 | 5 | if ( xrefLoc.exists() ) |
| 212 | |
{ |
| 213 | |
|
| 214 | 2 | location = relativePath; |
| 215 | |
} |
| 216 | |
else |
| 217 | |
{ |
| 218 | |
|
| 219 | 3 | for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); ) |
| 220 | |
{ |
| 221 | 3 | ReportPlugin plugin = (ReportPlugin) reports.next(); |
| 222 | |
|
| 223 | 3 | String artifactId = plugin.getArtifactId(); |
| 224 | 3 | if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) ) |
| 225 | |
{ |
| 226 | 3 | location = relativePath; |
| 227 | |
} |
| 228 | |
} |
| 229 | |
} |
| 230 | |
|
| 231 | 5 | if ( location == null ) |
| 232 | |
{ |
| 233 | 0 | getLog().warn( "Unable to locate Source XRef to link to - DISABLED" ); |
| 234 | |
} |
| 235 | |
} |
| 236 | 11 | return location; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
protected Map getFilesToProcess( ) |
| 246 | |
throws IOException |
| 247 | |
{ |
| 248 | 11 | String sourceXref = constructXRefLocation( false ); |
| 249 | 11 | String testXref = includeTests ? constructXRefLocation( true ) : ""; |
| 250 | |
|
| 251 | 11 | if ( aggregate && !project.isExecutionRoot() ) |
| 252 | |
{ |
| 253 | 0 | return Collections.EMPTY_MAP; |
| 254 | |
} |
| 255 | |
|
| 256 | 11 | if ( excludeRoots == null ) |
| 257 | |
{ |
| 258 | 9 | excludeRoots = Collections.EMPTY_LIST; |
| 259 | |
} |
| 260 | 11 | List excludeRootFiles = new ArrayList( excludeRoots.size() ); |
| 261 | |
|
| 262 | 11 | for ( Iterator it = excludeRoots.iterator(); it.hasNext(); ) |
| 263 | |
{ |
| 264 | 0 | String root = (String) it.next(); |
| 265 | 0 | File file = new File( root ); |
| 266 | 0 | if ( file.exists() |
| 267 | |
&& file.isDirectory() ) |
| 268 | |
{ |
| 269 | 0 | excludeRootFiles.add( file ); |
| 270 | |
} |
| 271 | |
} |
| 272 | |
|
| 273 | 11 | List directories = new ArrayList(); |
| 274 | |
|
| 275 | 11 | for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); ) |
| 276 | |
{ |
| 277 | 9 | String root = (String) i.next(); |
| 278 | 9 | File sroot = new File( root ); |
| 279 | 9 | directories.add( new PmdFileInfo( project, sroot, sourceXref ) ); |
| 280 | |
} |
| 281 | |
|
| 282 | 9 | if ( includeTests ) |
| 283 | |
{ |
| 284 | 0 | for ( Iterator i = testSourceRoots.iterator(); i.hasNext(); ) |
| 285 | |
{ |
| 286 | 0 | String root = (String) i.next(); |
| 287 | 0 | File sroot = new File( root ); |
| 288 | 0 | directories.add( new PmdFileInfo( project, sroot, testXref ) ); |
| 289 | |
} |
| 290 | |
} |
| 291 | 9 | if ( aggregate ) |
| 292 | |
{ |
| 293 | 0 | for ( Iterator i = reactorProjects.iterator(); i.hasNext(); ) |
| 294 | |
{ |
| 295 | 0 | MavenProject localProject = (MavenProject) i.next(); |
| 296 | 0 | for ( Iterator i2 = localProject.getCompileSourceRoots().iterator(); i2.hasNext(); ) |
| 297 | |
{ |
| 298 | 0 | String root = (String) i2.next(); |
| 299 | 0 | File sroot = new File( root ); |
| 300 | 0 | directories.add( new PmdFileInfo( localProject, sroot, sourceXref ) ); |
| 301 | |
} |
| 302 | 0 | if ( includeTests ) |
| 303 | |
{ |
| 304 | 0 | for ( Iterator i2 = localProject.getTestCompileSourceRoots().iterator(); i2.hasNext(); ) |
| 305 | |
{ |
| 306 | 0 | String root = (String) i2.next(); |
| 307 | 0 | File sroot = new File( root ); |
| 308 | 0 | directories.add( new PmdFileInfo( localProject, sroot, testXref ) ); |
| 309 | |
} |
| 310 | |
} |
| 311 | |
} |
| 312 | |
|
| 313 | |
} |
| 314 | |
|
| 315 | 9 | String excluding = getIncludeExcludeString( excludes ); |
| 316 | 9 | String including = getIncludeExcludeString( includes ); |
| 317 | 9 | Map files = new TreeMap(); |
| 318 | |
|
| 319 | 9 | if ( "".equals( including ) ) |
| 320 | |
{ |
| 321 | 9 | including = "**/*.java"; |
| 322 | |
} |
| 323 | |
|
| 324 | 9 | StringBuffer excludesStr = new StringBuffer(); |
| 325 | 9 | if ( StringUtils.isNotEmpty( excluding ) ) |
| 326 | |
{ |
| 327 | 4 | excludesStr.append( excluding ); |
| 328 | |
} |
| 329 | 9 | String[] defaultExcludes = FileUtils.getDefaultExcludes(); |
| 330 | 180 | for ( int i = 0; i < defaultExcludes.length; i++ ) |
| 331 | |
{ |
| 332 | 171 | if ( excludesStr.length() > 0 ) |
| 333 | |
{ |
| 334 | 166 | excludesStr.append( "," ); |
| 335 | |
} |
| 336 | 171 | excludesStr.append( defaultExcludes[i] ); |
| 337 | |
} |
| 338 | 9 | getLog().debug( "Excluded files: '" + excludesStr + "'" ); |
| 339 | |
|
| 340 | 9 | for ( Iterator it = directories.iterator(); it.hasNext(); ) |
| 341 | |
{ |
| 342 | 9 | PmdFileInfo finfo = (PmdFileInfo) it.next(); |
| 343 | 9 | File sourceDirectory = finfo.getSourceDirectory(); |
| 344 | 9 | if ( sourceDirectory.exists() |
| 345 | |
&& sourceDirectory.isDirectory() |
| 346 | |
&& !excludeRootFiles.contains( sourceDirectory ) ) |
| 347 | |
{ |
| 348 | 9 | List newfiles = FileUtils.getFiles( sourceDirectory, including, excludesStr.toString() ); |
| 349 | 9 | for ( Iterator it2 = newfiles.iterator(); it2.hasNext(); ) |
| 350 | |
{ |
| 351 | 25 | files.put( it2.next(), finfo ); |
| 352 | |
} |
| 353 | |
} |
| 354 | |
} |
| 355 | |
|
| 356 | 9 | return files; |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | |
|
| 361 | |
|
| 362 | |
|
| 363 | |
|
| 364 | |
|
| 365 | |
private String getIncludeExcludeString( String[] arr ) |
| 366 | |
{ |
| 367 | 18 | StringBuffer str = new StringBuffer(); |
| 368 | |
|
| 369 | 18 | if ( arr != null ) |
| 370 | |
{ |
| 371 | 10 | for ( int index = 0; index < arr.length; index++ ) |
| 372 | |
{ |
| 373 | 6 | if ( str.length() > 0 ) |
| 374 | |
{ |
| 375 | 2 | str.append( ',' ); |
| 376 | |
} |
| 377 | 6 | str.append( arr[index] ); |
| 378 | |
} |
| 379 | |
} |
| 380 | |
|
| 381 | 18 | return str.toString(); |
| 382 | |
} |
| 383 | |
|
| 384 | |
|
| 385 | |
protected boolean isHtml() |
| 386 | |
{ |
| 387 | 7 | return "html".equals( format ); |
| 388 | |
} |
| 389 | |
|
| 390 | |
|
| 391 | |
|
| 392 | |
|
| 393 | |
public boolean canGenerateReport() |
| 394 | |
{ |
| 395 | 10 | if ( aggregate && !project.isExecutionRoot() ) |
| 396 | |
{ |
| 397 | 0 | return false; |
| 398 | |
} |
| 399 | |
|
| 400 | |
|
| 401 | |
|
| 402 | 10 | if ( "xml".equals( format ) ) |
| 403 | |
{ |
| 404 | 6 | return true; |
| 405 | |
} |
| 406 | |
try |
| 407 | |
{ |
| 408 | 4 | Map filesToProcess = getFilesToProcess( ); |
| 409 | 2 | if ( filesToProcess.isEmpty() ) |
| 410 | |
{ |
| 411 | 0 | return false; |
| 412 | |
} |
| 413 | |
} |
| 414 | 0 | catch ( IOException e ) |
| 415 | |
{ |
| 416 | 0 | getLog().error( e ); |
| 417 | 2 | } |
| 418 | 2 | return true; |
| 419 | |
} |
| 420 | |
|
| 421 | |
|
| 422 | |
|
| 423 | |
|
| 424 | |
protected String getOutputDirectory() |
| 425 | |
{ |
| 426 | 11 | return outputDirectory; |
| 427 | |
} |
| 428 | |
} |