Apache Maven PMD Plugin

1. Overview

The PMD Plugin allows you to automatically run the PMD code analysis tool on your project's source code and generate a site report with its results. It also supports the separate Copy/Paste Detector tool (or CPD) distributed with PMD.

2. Plugin Goals

This 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.

3. Configuration

The PMD and CPD reports share the same configuration. For example, the following tells Maven to run the PMD and CPD report as part of the site report generation.
The reports will link directly to the cross-referenced source if you enable this with the linkXRef parameter. See the JXR plugin for more details. The following is a possible configuration:
<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.10.0</version>
        <configuration>
          <linkXref>true</linkXref>
          <sourceEncoding>utf-8</sourceEncoding>
          <minimumTokens>100</minimumTokens>
          <targetJdk>1.5</targetJdk>
          <excludes>
            <exclude>**/*Bean.java</exclude>
            <exclude>**/generated/*.java</exclude>
          </excludes>
          <excludeRoots>
            <excludeRoot>target/generated-sources/stubs</excludeRoot>
          </excludeRoots>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

4. Usage

The PMD plugin generates PMD and CPD reports using the PMD code analysis tool.
To include a report with default rule sets and configuration in your project site, set the following in the section of your POM:
<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.10.0</version>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>
You can also explicitly execute the PMD plugin and generate the same report by setting the plugin in the section of your POM as shown below:
<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.10.0</version>
      </plugin>
    </plugins>
  </build>
  ...
</project>

5. Examples

Using Rule Sets The PMD plugin ships a default rule set, that has various rules enabled. The ruleset is available as /rulesets/java/maven-pmd-plugin-default.xml. See below for the rules, that are enabled with the default rule set.
<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.10.0</version>
        <configuration>
          <rulesets>
            <!-- A rule set, that comes bundled with PMD -->
            <ruleset>/category/java/bestpractices.xml</ruleset>
            <!-- Custom local file system rule set -->
            <ruleset>d:\rulesets\strings.xml</ruleset>
            <!-- Custom remote rule set accessed via a URL -->
            <ruleset>http://localhost/design.xml</ruleset>
          </rulesets>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

6. Conclusion

In this quick guide, we went over the PMD plugin and gave instructions on using and customizing it. Also we have seen the different plugin goals and their usage.
Reference : https://maven.apache.org/plugins/maven-pmd-plugin/

Comments