PMD scans Java source code and looks for potential problems like:

  • Possible bugs – empty try/catch/finally/switch statements
  • Dead code – unused local variables, parameters and private methods
  • Suboptimal code – wasteful String/StringBuffer usage
  • Overcomplicated expressions – unnecessary if statements, for loops that could be while loops
  • Duplicate code – copied/pasted code means copied/pasted bugs

The default rulesets: basic.xml, unusedcode.xml and imports.xml

  • best practice would be to create a custom ruleset file kept under code versioning control and used with the IDE integration plugin; this way standards changes are easy to propagate around the team
  • use standard PMD rules plus new custom rules

It can be run as

  • stand alone:

./bin/ pmd -d /opt/data/paypipe/connectors/src/main/java/ -f summaryhtml -R rulesets/java/basic.xml -version 1.6 -language java > ../pmdSummary.html

  • mvn plugin, outputing by default to target/pmd.xml and target/site/pmd.html
    mvn pmd:pmd

Including PMD in the Maven verify lifecycle phase:



Integrating with IDE using PMD plugin:

  • a custom ruleset file can be declared to the PMD Plugin so changes are shared