This is the second in a series of posts which have the ant targets and javascript to generate and report on source code derived quality metrics.

CPD

CPD is a subproject of the PMD and whole purpose is to crawl your source code looking for places where copy-and-paste coding has taken place. In fact, CPD stands for Copy and Paste Detector. Copy-and-paste coding is of course a maintenence nightmare and generally frowned upon in the developer community. I remember this being drilled into my head as soon as we were talk functions in C in college.

Here is the ant task.

<!-- cpd -->
<target name="cpd">
 <taskdef name="cpd" classname="net.sourceforge.pmd.cpd.CPDTask"
                     classpath="${metrics.dir.lib}\pmd\lib\pmd-3.9.jar"/>

 <cpd minimumTokenCount="100"
      outputFile="${metrics.dir.report}\xml\${report.name}"
      format="xml">
  <fileset dir="${dir.source}" includes="**\*.java"/>
 </cpd>
</target>

Variables:

  • metrics.dir.lib – Tell ant how to find the the cpd task in a decoupled manner. I try not to put these in the main ant lib dir.
  • metrics.dir.report – The place where you want the report put. In this case, my reports dir has ‘xml’, ‘xsl’ and ‘html’ dirs for organization of the various parts of the report generation process.
  • report.name – I call this target for each module I am collecting metrics for, so I pass in the name of the report as a parameter to it. Feel free to hardcode if that suits you better.
  • dir.source – Since CPD works on the actual sourcecode, you need to specify where the top-level dir that contains it. Note that if you have a package called org.agoucher.metrics, it would be the dir that contains the ‘org’ dir.