Coverage Report - org.apache.maven.plugin.pmd.AbstractPmdReport
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractPmdReport
66 %
72/108
50 %
38/76
5,083
 
 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.File;
 23  
 import java.io.IOException;
 24  
 import java.util.ArrayList;
 25  
 import java.util.Arrays;
 26  
 import java.util.Collection;
 27  
 import java.util.Collections;
 28  
 import java.util.HashSet;
 29  
 import java.util.Iterator;
 30  
 import java.util.LinkedHashSet;
 31  
 import java.util.List;
 32  
 import java.util.Map;
 33  
 import java.util.TreeMap;
 34  
 
 35  
 import net.sourceforge.pmd.PMD;
 36  
 
 37  
 import org.apache.maven.doxia.siterenderer.Renderer;
 38  
 import org.apache.maven.model.ReportPlugin;
 39  
 import org.apache.maven.project.MavenProject;
 40  
 import org.apache.maven.reporting.AbstractMavenReport;
 41  
 import org.codehaus.plexus.util.FileUtils;
 42  
 import org.codehaus.plexus.util.PathTool;
 43  
 import org.codehaus.plexus.util.ReaderFactory;
 44  
 import org.codehaus.plexus.util.StringUtils;
 45  
 
 46  
 /**
 47  
  * Base class for the PMD reports.
 48  
  *
 49  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 50  
  * @version $Id: org.apache.maven.plugin.pmd.AbstractPmdReport.html 816691 2012-05-08 15:16:42Z hboutemy $
 51  
  */
 52  13
 public abstract class AbstractPmdReport
 53  
     extends AbstractMavenReport
 54  
 {
 55  
     /**
 56  
      * The output directory for the intermediate XML report.
 57  
      *
 58  
      * @parameter expression="${project.build.directory}"
 59  
      * @required
 60  
      */
 61  
     protected File targetDirectory;
 62  
 
 63  
     /**
 64  
      * The output directory for the final HTML report. Note that this parameter is only evaluated if the goal is run
 65  
      * directly from the command line or during the default lifecycle. If the goal is run indirectly as part of a site
 66  
      * generation, the output directory configured in the Maven Site Plugin is used instead.
 67  
      *
 68  
      * @parameter expression="${project.reporting.outputDirectory}"
 69  
      * @required
 70  
      */
 71  
     protected File outputDirectory;
 72  
 
 73  
     /**
 74  
      * Site rendering component for generating the HTML report.
 75  
      *
 76  
      * @component
 77  
      */
 78  
     private Renderer siteRenderer;
 79  
 
 80  
     /**
 81  
      * The project to analyse.
 82  
      *
 83  
      * @parameter expression="${project}"
 84  
      * @required
 85  
      * @readonly
 86  
      */
 87  
     protected MavenProject project;
 88  
 
 89  
     /**
 90  
      * Set the output format type, in addition to the HTML report.  Must be one of: "none",
 91  
      * "csv", "xml", "txt" or the full class name of the PMD renderer to use.
 92  
      * See the net.sourceforge.pmd.renderers package javadoc for available renderers.
 93  
      * XML is required if the pmd:check goal is being used.
 94  
      *
 95  
      * @parameter expression="${format}" default-value="xml"
 96  
      */
 97  13
     protected String format = "xml";
 98  
 
 99  
     /**
 100  
      * Link the violation line numbers to the source xref. Links will be created
 101  
      * automatically if the jxr plugin is being used.
 102  
      *
 103  
      * @parameter expression="${linkXRef}" default-value="true"
 104  
      */
 105  
     private boolean linkXRef;
 106  
 
 107  
     /**
 108  
      * Location of the Xrefs to link to.
 109  
      *
 110  
      * @parameter default-value="${project.reporting.outputDirectory}/xref"
 111  
      */
 112  
     private File xrefLocation;
 113  
 
 114  
     /**
 115  
      * Location of the Test Xrefs to link to.
 116  
      *
 117  
      * @parameter default-value="${project.reporting.outputDirectory}/xref-test"
 118  
      */
 119  
     private File xrefTestLocation;
 120  
 
 121  
     /**
 122  
      * A list of files to exclude from checking. Can contain Ant-style wildcards and double wildcards. Note that these
 123  
      * exclusion patterns only operate on the path of a source file relative to its source root directory. In other
 124  
      * words, files are excluded based on their package and/or class name. If you want to exclude entire source root
 125  
      * directories, use the parameter <code>excludeRoots</code> instead.
 126  
      *
 127  
      * @parameter
 128  
      * @since 2.2
 129  
      */
 130  
     private String[] excludes;
 131  
 
 132  
     /**
 133  
      * A list of files to include from checking. Can contain Ant-style wildcards and double wildcards.
 134  
      * Defaults to **\/*.java.
 135  
      *
 136  
      * @parameter
 137  
      * @since 2.2
 138  
      */
 139  
     private String[] includes;
 140  
 
 141  
     /**
 142  
      * The directories containing the sources to be compiled.
 143  
      *
 144  
      * @parameter expression="${project.compileSourceRoots}"
 145  
      * @required
 146  
      * @readonly
 147  
      */
 148  
     private List compileSourceRoots;
 149  
 
 150  
     /**
 151  
      * The directories containing the test-sources to be compiled.
 152  
      *
 153  
      * @parameter expression="${project.testCompileSourceRoots}"
 154  
      * @required
 155  
      * @readonly
 156  
      */
 157  
     private List testSourceRoots;
 158  
 
 159  
     /**
 160  
      * The project source directories that should be excluded.
 161  
      *
 162  
      * @parameter
 163  
      * @since 2.2
 164  
      */
 165  
     private File[] excludeRoots;
 166  
 
 167  
     /**
 168  
      * Run PMD on the tests.
 169  
      *
 170  
      * @parameter default-value="false"
 171  
      * @since 2.2
 172  
      */
 173  
     protected boolean includeTests;
 174  
 
 175  
     /**
 176  
      * Whether to build an aggregated report at the root, or build individual reports.
 177  
      *
 178  
      * @parameter expression="${aggregate}" default-value="false"
 179  
      * @since 2.2
 180  
      */
 181  
     protected boolean aggregate;
 182  
 
 183  
     /**
 184  
      * The file encoding to use when reading the Java sources.
 185  
      *
 186  
      * @parameter expression="${encoding}" default-value="${project.build.sourceEncoding}"
 187  
      * @since 2.3
 188  
      */
 189  
     private String sourceEncoding;
 190  
 
 191  
     /**
 192  
      * The file encoding when writing non-HTML reports.
 193  
      *
 194  
      * @parameter expression="${outputEncoding}" default-value="${project.reporting.outputEncoding}"
 195  
      * @since 2.5
 196  
      */
 197  
     private String outputEncoding;
 198  
 
 199  
     /**
 200  
      * The projects in the reactor for aggregation report.
 201  
      *
 202  
      * @parameter expression="${reactorProjects}"
 203  
      * @readonly
 204  
      */
 205  
     protected List reactorProjects;
 206  
 
 207  
     /** {@inheritDoc} */
 208  
     protected MavenProject getProject()
 209  
     {
 210  0
         return project;
 211  
     }
 212  
 
 213  
     /** {@inheritDoc} */
 214  
     protected Renderer getSiteRenderer()
 215  
     {
 216  13
         return siteRenderer;
 217  
     }
 218  
 
 219  
     protected String constructXRefLocation( boolean test )
 220  
     {
 221  19
         String location = null;
 222  19
         if ( linkXRef )
 223  
         {
 224  8
             File xrefLoc = test ? xrefTestLocation : xrefLocation;
 225  
 
 226  8
             String relativePath = PathTool.getRelativePath( outputDirectory.getAbsolutePath(), xrefLoc.getAbsolutePath() );
 227  8
             if ( StringUtils.isEmpty( relativePath ) )
 228  
             {
 229  0
                 relativePath = ".";
 230  
             }
 231  8
             relativePath = relativePath + "/" + xrefLoc.getName();
 232  8
             if ( xrefLoc.exists() )
 233  
             {
 234  
                 // XRef was already generated by manual execution of a lifecycle binding
 235  2
                 location = relativePath;
 236  
             }
 237  
             else
 238  
             {
 239  
                 // Not yet generated - check if the report is on its way
 240  6
                 for ( Iterator reports = project.getReportPlugins().iterator(); reports.hasNext(); )
 241  
                 {
 242  6
                     ReportPlugin plugin = (ReportPlugin) reports.next();
 243  
 
 244  6
                     String artifactId = plugin.getArtifactId();
 245  6
                     if ( "maven-jxr-plugin".equals( artifactId ) || "jxr-maven-plugin".equals( artifactId ) )
 246  
                     {
 247  6
                         location = relativePath;
 248  
                     }
 249  6
                 }
 250  
             }
 251  
 
 252  8
             if ( location == null )
 253  
             {
 254  0
                 getLog().warn( "Unable to locate Source XRef to link to - DISABLED" );
 255  
             }
 256  
         }
 257  19
         return location;
 258  
     }
 259  
 
 260  
     /**
 261  
      * Convenience method to get the list of files where the PMD tool will be executed
 262  
      *
 263  
      * @return a List of the files where the PMD tool will be executed
 264  
      * @throws java.io.IOException
 265  
      */
 266  
     protected Map getFilesToProcess()
 267  
         throws IOException
 268  
     {
 269  19
         String sourceXref = constructXRefLocation( false );
 270  19
         String testXref = includeTests ? constructXRefLocation( true ) : "";
 271  
 
 272  19
         if ( aggregate && !project.isExecutionRoot() )
 273  
         {
 274  0
             return Collections.EMPTY_MAP;
 275  
         }
 276  
 
 277  19
         if ( excludeRoots == null )
 278  
         {
 279  10
             excludeRoots = new File[0];
 280  
         }
 281  19
         Collection excludeRootFiles = new HashSet( excludeRoots.length );
 282  
 
 283  19
         for ( int i = 0; i < excludeRoots.length; i++ )
 284  
         {
 285  0
             File file = excludeRoots[i];
 286  0
             if ( file.isDirectory() )
 287  
             {
 288  0
                 excludeRootFiles.add( file );
 289  
             }
 290  
         }
 291  
 
 292  19
         List directories = new ArrayList();
 293  
 
 294  19
         if ( compileSourceRoots != null )
 295  
         {
 296  
 
 297  18
             for ( Iterator i = compileSourceRoots.iterator(); i.hasNext(); )
 298  
             {
 299  18
                 String root = (String) i.next();
 300  18
                 File sroot = new File( root );
 301  18
                 directories.add( new PmdFileInfo( project, sroot, sourceXref ) );
 302  18
             }
 303  
 
 304  
         }
 305  19
         if ( includeTests )
 306  
         {
 307  0
             if ( testSourceRoots != null )
 308  
             {
 309  0
                 for ( Iterator i = testSourceRoots.iterator(); i.hasNext(); )
 310  
                 {
 311  0
                     String root = (String) i.next();
 312  0
                     File sroot = new File( root );
 313  0
                     directories.add( new PmdFileInfo( project, sroot, testXref ) );
 314  0
                 }
 315  
             }
 316  
         }
 317  19
         if ( aggregate )
 318  
         {
 319  0
             for ( Iterator i = reactorProjects.iterator(); i.hasNext(); )
 320  
             {
 321  0
                 MavenProject localProject = (MavenProject) i.next();
 322  0
                 for ( Iterator i2 = localProject.getCompileSourceRoots().iterator(); i2.hasNext(); )
 323  
                 {
 324  0
                     String root = (String) i2.next();
 325  0
                     File sroot = new File( root );
 326  0
                     directories.add( new PmdFileInfo( localProject, sroot, sourceXref ) );
 327  0
                 }
 328  0
                 if ( includeTests )
 329  
                 {
 330  0
                     for ( Iterator i2 = localProject.getTestCompileSourceRoots().iterator(); i2.hasNext(); )
 331  
                     {
 332  0
                         String root = (String) i2.next();
 333  0
                         File sroot = new File( root );
 334  0
                         directories.add( new PmdFileInfo( localProject, sroot, testXref ) );
 335  0
                     }
 336  
                 }
 337  0
             }
 338  
 
 339  
         }
 340  
 
 341  19
         String excluding = getExcludes();
 342  19
         getLog().debug( "Exclusions: " + excluding );
 343  19
         String including = getIncludes();
 344  19
         getLog().debug( "Inclusions: " + including );
 345  
 
 346  19
         Map files = new TreeMap();
 347  
 
 348  19
         for ( Iterator it = directories.iterator(); it.hasNext(); )
 349  
         {
 350  18
             PmdFileInfo finfo = (PmdFileInfo) it.next();
 351  18
             File sourceDirectory = finfo.getSourceDirectory();
 352  18
             if ( sourceDirectory.isDirectory() && !excludeRootFiles.contains( sourceDirectory ) )
 353  
             {
 354  18
                 List newfiles = FileUtils.getFiles( sourceDirectory, including, excluding );
 355  18
                 for ( Iterator it2 = newfiles.iterator(); it2.hasNext(); )
 356  
                 {
 357  45
                     files.put( it2.next(), finfo );
 358  
                 }
 359  
             }
 360  18
         }
 361  
 
 362  19
         return files;
 363  
     }
 364  
 
 365  
     /**
 366  
      * Gets the comma separated list of effective include patterns.
 367  
      *
 368  
      * @return The comma separated list of effective include patterns, never <code>null</code>.
 369  
      */
 370  
     private String getIncludes()
 371  
     {
 372  19
         Collection patterns = new LinkedHashSet();
 373  19
         if ( includes != null )
 374  
         {
 375  0
             patterns.addAll( Arrays.asList( includes ) );
 376  
         }
 377  19
         if ( patterns.isEmpty() )
 378  
         {
 379  19
             patterns.add( "**/*.java" );
 380  
         }
 381  19
         return StringUtils.join( patterns.iterator(), "," );
 382  
     }
 383  
 
 384  
     /**
 385  
      * Gets the comma separated list of effective exclude patterns.
 386  
      *
 387  
      * @return The comma separated list of effective exclude patterns, never <code>null</code>.
 388  
      */
 389  
     private String getExcludes()
 390  
     {
 391  19
         Collection patterns = new LinkedHashSet( FileUtils.getDefaultExcludesAsList() );
 392  19
         if ( excludes != null )
 393  
         {
 394  8
             patterns.addAll( Arrays.asList( excludes ) );
 395  
         }
 396  19
         return StringUtils.join( patterns.iterator(), "," );
 397  
     }
 398  
 
 399  
     protected boolean isHtml()
 400  
     {
 401  8
         return "html".equals( format );
 402  
     }
 403  
 
 404  
     /** {@inheritDoc} */
 405  
     public boolean canGenerateReport()
 406  
     {
 407  29
         if ( aggregate && !project.isExecutionRoot() )
 408  
         {
 409  0
             return false;
 410  
         }
 411  
 
 412  29
         if ( "pom".equals( project.getPackaging() ) && !aggregate )
 413  
         {
 414  0
             return false;
 415  
         }
 416  
 
 417  
         // if format is XML, we need to output it even if the file list is empty
 418  
         // so the "check" goals can check for failures
 419  29
         if ( "xml".equals( format ) )
 420  
         {
 421  18
             return true;
 422  
         }
 423  
         try
 424  
         {
 425  11
             Map filesToProcess = getFilesToProcess();
 426  11
             if ( filesToProcess.isEmpty() )
 427  
             {
 428  1
                 return false;
 429  
             }
 430  
         }
 431  0
         catch ( IOException e )
 432  
         {
 433  0
             getLog().error( e );
 434  10
         }
 435  10
         return true;
 436  
     }
 437  
 
 438  
     /** {@inheritDoc} */
 439  
     protected String getOutputDirectory()
 440  
     {
 441  17
         return outputDirectory.getAbsolutePath();
 442  
     }
 443  
 
 444  
     protected String getSourceEncoding()
 445  
     {
 446  84
         return sourceEncoding;
 447  
     }
 448  
 
 449  
     /**
 450  
      * Gets the effective reporting output files encoding.
 451  
      *
 452  
      * @return The effective reporting output file encoding, never <code>null</code>.
 453  
      * @since 2.5
 454  
      */
 455  
     protected String getOutputEncoding()
 456  
     {
 457  14
         return ( outputEncoding != null ) ? outputEncoding : ReaderFactory.UTF_8;
 458  
     }
 459  
 
 460  
     static String getPmdVersion()
 461  
     {
 462  
         try
 463  
         {
 464  8
             return (String) PMD.class.getField( "VERSION" ).get( null );
 465  
         }
 466  0
         catch ( IllegalAccessException e )
 467  
         {
 468  0
             throw new RuntimeException( "PMD VERSION field not accessible", e );
 469  
         }
 470  0
         catch ( NoSuchFieldException e )
 471  
         {
 472  0
             throw new RuntimeException( "PMD VERSION field not found", e );
 473  
         }
 474  
     }
 475  
 }