My new website on Artificial Intelligence and Machine Learning www.aimlfront.com

Maven Setup for PMD and CPD

PMD and CPD both are good at checking coding standards and are easy to extend. But PMD has additional rules to check for cyclomatic complexity, Npath complexity, etc which allows you write healthy code.

Another advantage of using PMD is CPD (Copy/Paste Detector).It finds out code duplication across projects and is not constrained to JAVA. It works for JSP too.

PMD to find problematic code areas and next refactoring targets.

PMD & CPD setup for the Maven

Add plugin information (inside plugins tag) to pom.xml for the project wise. Otherwise if we have common parent for all the projects, better we can add, we can utilize for all the projects.

<plugin>
   <artifactId>maven-pmd-plugin</artifactId>
   <configuration>
      <includeTests>true</includeTests>
      <minimumTokens>100</minimumTokens>
      <linkXref>true</linkXref>
      <targetJdk>${targetJdk}</targetJdk>
      <rulesets>
         <ruleset>${pmd-config-url}</ruleset>
      </rulesets>
      <failOnViolation>true</failOnViolation>
   </configuration>
   <executions>
      <execution>
         <phase>test</phase>
         <goals>
            <goal>pmd</goal>
            <goal>cpd</goal>
            <goal>cpd-check</goal>
            <goal>check</goal>
         </goals>
      </execution>
   </executions>
</plugin>

Add the below property to pom.xml related to above plug-in.

<properties>
   <pmd-config-url>
      ${location}/rulesets/pmd/2.0/pmd-2.0.xml
   </pmd-config-url>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


Do maven clean install. If you have any PMD or CPD vioaltions, build will fail.

PMD will check for

  • Unused imports
  • Unnecessary/Unused variables
  • Code duplication/CPD
  • Catching an exception without doing anything
  • Having dead code
  • Too many complex methods
  • Direct use of implementations instead of interfaces
  • Implementing the hashcode() method without the not equals(Object object) method
  • Especially Cyclomatic Complexity of code.(about Cyclomatic Complexity see last topic)

Note: Go to this location will get the details about PMD errors for the project.

	${workspace location}\${Project location}\target\site\pmd.html
	${workspace location}\${Project location}\target\site\cpd.html

		(or)

	${workspace location}\${Project location}\target\pmd.xml
	${workspace location}\${Project location}\target\cpd.xml
	


PMD CPD plugin has 4 goals

pmd:pmd: creates a PMD site report based on the rulesets and configuration set in the plugin. It can also generate a pmd output file aside from the site report in any of the following formats: xml, csv or txt.
pmd:cpd: generates a report for PMD's Copy/Paste Detector (CPD) tool. It can also generate a cpd results file in any of these formats: xml, csv or txt.
pmd:check: verifies that the PMD report is empty and fails the build if it is not. This goal is executed by default when pmd:pmd is executed.
pmd:cpd-check: verifies that the CPD report is empty and fails the build if it is not. This goal is executed by default when pmd:cpd is executed.