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/run.sh 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:
<project>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                            <goal>cpd-check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Integrating with IDE using PMD plugin:
http://pmd.sourceforge.net/pmd-5.1.2/integrations.html

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