9

I am trying to have a pom.xml file with PMD plugin integrated into build stage.

PMD version 3.13.0

Oracle JDK 14

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <pmd.plugin.version>3.13.0</pmd.plugin.version>
    </properties>

    <dependencies>

    </dependencies>


    <reporting>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
            </plugin>

        </plugins>
    </reporting>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${pmd.plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <configuration>
                    <!-- failOnViolation is actually true by default, but can be disabled -->
                    <failOnViolation>true</failOnViolation>
                    <!-- printFailingErrors is pretty useful -->
                    <printFailingErrors>true</printFailingErrors>
                    <targetJdk>14</targetJdk>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

mvn clean install fails with

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd (pmd) on project notes-service: Execution pmd of goal org.apache.maven.plugins:maven-pmd-plugin:3.13.0:pmd failed: org.apache.maven.reporting.MavenR
eportException: Unsupported targetJdk value '14'. -> [Help 1]

What I tried

https://stackoverflow.com/a/2033299/1839360 Added a dependency for pmd-java - it did not work. I tried both versions https://mvnrepository.com/artifact/net.sourceforge.pmd/pmd-java/6.21.0 https://mvnrepository.com/artifact/net.sourceforge.pmd/pmd-java/6.23.0

With the latest version, there is an error

Exception in thread "main" java.util.ServiceConfigurationError: net.sourceforge.pmd.lang.Language: Provider net.sourceforge.pmd.lang.java.JavaLanguageModule could not be instantiated
        at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:584)
        at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:806)
        at java.base/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:724)
        at java.base/java.util.ServiceLoader$3.next(ServiceLoader.java:1396)
        at net.sourceforge.pmd.lang.LanguageRegistry.<init>(LanguageRegistry.java:35)
        at net.sourceforge.pmd.lang.LanguageRegistry.<clinit>(LanguageRegistry.java:24)
        at org.apache.maven.plugins.pmd.PmdReport.getPMDConfiguration(PmdReport.java:723)
        at org.apache.maven.plugins.pmd.PmdReport.executePmd(PmdReport.java:411)
        at org.apache.maven.plugins.pmd.PmdReport.executePmdWithClassloader(PmdReport.java:376)
        at org.apache.maven.plugins.pmd.PmdReport.canGenerateReport(PmdReport.java:350)
        at org.apache.maven.reporting.AbstractMavenReport.execute(AbstractMavenReport.java:120)
        at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
        at org.apache.maven.lifecycle.internal.MojoExecutor.executeForkedExecutions(MojoExecutor.java:355)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:200)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
        at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
        at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
        at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
Caused by: java.lang.NoSuchMethodError: 'void net.sourceforge.pmd.lang.metrics.internal.AbstractLanguageMetricsProvider.<init>(java.lang.Class, java.lang.Class)'
        at net.sourceforge.pmd.lang.java.JavaLanguageHandler$JavaMetricsProvider.<init>(JavaLanguageHandler.java:48)
        at net.sourceforge.pmd.lang.java.AbstractJavaHandler.<init>(AbstractJavaHandler.java:53)
        at net.sourceforge.pmd.lang.java.JavaLanguageHandler.<init>(JavaLanguageHandler.java:35)
        at net.sourceforge.pmd.lang.java.JavaLanguageHandler.<init>(JavaLanguageHandler.java:32)
        at net.sourceforge.pmd.lang.java.JavaLanguageModule.<init>(JavaLanguageModule.java:20)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
        at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:782)
        ... 35 more

Naman
  • 27,789
  • 26
  • 218
  • 353
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • Note, my question is not a duplicate for https://stackoverflow.com/questions/61237831/maven-pmd-plugin-3-5-fails-with-jdk13 It does not have an answer. – Yan Khonski Apr 24 '20 at 21:49
  • Can be realted, but I am not sure. It is for older version Java https://stackoverflow.com/questions/2033217/how-to-make-maven-pmd-plugin-support-the-latest-pmd-release – Yan Khonski Apr 24 '20 at 21:52
  • 1
    Support for JDK 14 was formally announced with [PMD 6.22.0 released on March 12](https://github.com/pmd/pmd/releases/tag/pmd_releases%2F6.22.0#java-14-support): _"This release of PMD brings support for Java 14. PMD can parse Switch Expressions, which have been promoted to be a standard language feature of Java. PMD also parses Text Blocks as String literals, which is still a preview language feature in Java 14. The new Pattern Matching for instanceof can be used as well as Records."_ – skomisa Apr 25 '20 at 01:42

1 Answers1

4

I had to add these dependencies

<dependencies>
    <dependency>
        <groupId>net.sourceforge.pmd</groupId>
        <artifactId>pmd-java</artifactId>
        <version>6.23.0</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.pmd</groupId>
        <artifactId>pmd-core</artifactId>
        <version>6.23.0</version>
        </dependency>
</dependencies>

The whole pom.xml is

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>group</groupId>
    <artifactId>artifact</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>


    <properties>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <pmd.plugin.version>3.13.0</pmd.plugin.version>
    </properties>

    <dependencies>

    </dependencies>


    <reporting>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
            </plugin>

        </plugins>
    </reporting>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-pmd-plugin</artifactId>
                    <version>${pmd.plugin.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <configuration>
                    <!-- failOnViolation is actually true by default, but can be disabled -->
                    <failOnViolation>true</failOnViolation>
                    <!-- printFailingErrors is pretty useful -->
                    <printFailingErrors>true</printFailingErrors>
                    <targetJdk>14</targetJdk>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>check</goal>
                        </goals>
                    </execution>
                </executions>

                <dependencies>
                    <!-- https://mvnrepository.com/artifact/net.sourceforge.pmd/pmd-java -->
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-java</artifactId>
                        <version>6.23.0</version>
                    </dependency>

                    <!-- https://mvnrepository.com/artifact/net.sourceforge.pmd/pmd-core -->
                    <dependency>
                        <groupId>net.sourceforge.pmd</groupId>
                        <artifactId>pmd-core</artifactId>
                        <version>6.23.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114
  • 3
    Java 14 is only supported with PMD 6.22.0+. What you've configured now, is described here: https://maven.apache.org/plugins/maven-pmd-plugin/examples/upgrading-PMD-at-runtime.html The next maven-pmd-plugin will use a newer PMD version (see https://issues.apache.org/jira/browse/MPMD-302) – adangel Apr 25 '20 at 09:00
  • 3
    Agree, I saw this page through related searches. However, it does not say anything about Java 14. After putting my answer here, other people can google and find it here. Feel free to post your answer with more details. – Yan Khonski Apr 25 '20 at 09:07
  • Can confirm, I Googled this issue and found this. It worked, thanks! I didn't specify `6.23.0`, but I specified a range of `[6.22.0,)`, but basically the same thing you did. – Austin Brown Nov 04 '20 at 00:07