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.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.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 sourceEncoding 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 default is 1.4. The following is a possible configuration:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.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>