'UnsupportedClassVersionError from maven-pmd-plugin for java 11 project

We are migrating our java 8 projects to java 11. JAVA_HOME still points to java 8 but the first project to be migrated compiles in java 11 and the unit tests run just fine.

The maven-compiler-plugin and the toolchain are defined as in this question. Maven version is 3.5.0.

When i run

mvn verify

I get the following error:

Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.11.0:
pmd (pmd) on project <yourproject>: 
  Execution pmd of goal org.apache.maven.plugins:maven-pmd-plugin:3.11.0:
  pmd failed: An API incompatibility was encountered while executing 
  org.apache.maven.plugins:maven-pmd-plugin:3.11.0:pmd: 
    java.lang.UnsupportedClassVersionError: 
       <Yourclass> has been compiled by a more recent version of the Java Runtime 
       (class file version 55.0), this version of the Java Runtime only recognizes 
       class file versions up to 52.0    //(indentation added for readability)

I understand that the classes were compiled using java 11 (as intended) and that maven runs on java 8 because that is what JAVA_HOME points to. But why does that trip up the pmd-plugin? It is supposed to check the source code, never mind the compiled classes.

Is there a way around this, short of setting JAVA_HOME to 11?



Solution 1:[1]

Actually, PMD does care about compiled classes for type resolution.

PMD is able to check variable types, method return types, and so on for usage in it's analysis. To do so, PMD uses the compiled classes and complete dependencies classpath.

You can disable this through the plugin's configuration by setting the typeResolution property to false, but be warned, several rules rely on this information, and when not available work on a best effort basis, which means you will get more false positives / negatives in the analysis.

PMD's analysis quality is significantly hampered by not using type resolution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Johnco