Target JDK

There is a configuration element in the PMD plugin that lets you set which target JDK your Java code will be compiled for. Often you want this to be in sync with the configuration for maven-compiler-plugin. The best practice for this is to set up the wellknown property maven.compiler.target and the rest will work automatically. Alternatively, you can set it manually:

<project>
  ...
  <reporting>
    <plugins>
      <plugin>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.21.2</version>
        <configuration>
          <targetJdk>1.6</targetJdk>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  ...
</project>

Using Maven Toolchains

Since version 3.14.0 of the PMD plugin, toolchains are supported. This helps if the build system is running a different JDK than being used for compiling. PMD reads the class files for type resolution and this fails with ClassFormatErrors if the JDK version is incorrect.

To set this up, refer to the Guide to Using Toolchains, which makes use of the Maven Toolchains Plugin.

With the maven-toolchains-plugin you configure 1 default JDK toolchain for all related maven-plugins. Since maven-pmd-plugin 3.14.0 when using with Maven 3.3.1+ it is also possible to give the plugin its own toolchain, which can be useful in case of different JDK calls per execution block (e.g. the test sources require a different JDK compared to the main sources).