4.2. PMD

PMD is another static analysis tool that checks for problematic code (possible bugs, dead code, suboptimal code and so on). As for Checkstyle, all the sister projects share a common PMD definition file, this one adapted from Apache group's own PMD definitions.

PMD integrates with both Maven and with Eclipse.

4.2.1. Maven Plugin

The corporate POM (see Part IV, “Building and Deploying the Corporate Artifacts”) automatically includes the Maven PMD plugin:

<!-- quality checks: pmd -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-pmd-plugin</artifactId>
    <configuration>
        <targetJdk>${compileTarget}</targetJdk>
        <rulesets>
            <ruleset>
              http://starobjects.sourceforge.net/m2-site/main/standards/resources/pmd.xml
            </ruleset>
        </rulesets>
        ...
    </configuration>
</plugin>

As for Checkstyle, this plugin is not bound to any Maven lifecycle phase, and is not intended to be run other than as a report within mvn site. In particular, note that the configuration is only defined in the <reporting> section, so it isn't possible to run using mvn pmd:pmd. For more immediate feedback, use the Eclipse plugin, below.

4.2.2. Eclipse Plugin

The PMD for eclipse plugin allows PMD violations to be flagged as warnings or errors within the Problems view of the Eclipse IDE. The plugin also provides a custom "PMD" perspective which also lists all violations. To associate PMD for Eclipse with the PMD config file, use Windows > Preferences:

First, use "Clear All" to remove the default rule set. Then, use "Import rule set" and specify the pmd.xml file. Unlike the Checkstyle plugin, this must be a local file so downloaded from the Star Objects website; the file to use is http://starobjects.sourceforge.net/m2-site/main/standards/resources/pmd.xml.

Also unlike the Checkstyle plugin, PMD violations are not continually updated against the code (it is not implemented as an Eclipse builder). To perform a check, you must use the context menu in Package Explorer and then use PMD > Check Code with PMD. This will switch into the PMD perspective; the Violations view will indicate any code that needs attention:

Alternatively, you can switch back to the Java view; any PMD violations show up in the Problems view:

Once violations are fixed, the Check must be performed again to refresh both of these views.

Note, to prevent PMD from switching to its own perspective, use Windows > Preferences > PMD and then disable "Show PMD Perspective when checking code".