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 <reporting> section of your POM:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.26.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 <build> section of your POM as shown below:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.26.0</version>
      </plugin>
    </plugins>
  </build>
  ...
</project>

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.

If your source uses a non-default encoding, you can use the inputEncoding parameter to tell Maven which encoding to use when reading the java source. Note also the ability to exclude source which you want to ignore.

You can configure the minimum code size which trips the CPD. The default of 100 tokens corresponds to approximately 5-10 lines of code.

Since PMD parses the Java source, it needs to know which Java version to use. The following is a possible configuration:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.26.0</version>
        <configuration>
          <linkXref>true</linkXref>
          <inputEncoding>utf-8</inputEncoding>
          <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>
        <reportSets>
          <reportSet><!-- by default, id = "default" -->
            <reports><!-- select non-aggregate reports -->
              <report>pmd</report>
              <report>cpd</report>
            </reports>
          </reportSet>
          <reportSet><!-- aggregate reportSet, to define in poms having modules -->
            <id>aggregate</id>
            <inherited>false</inherited> <!-- don't run aggregate in child modules -->
            <reports>
              <report>aggregate-pmd</report>
              <report>aggregate-cpd</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>