Skip to content

Fix toolchain support for maven4 + java < 17 - #728

Closed
adangel wants to merge 3 commits into
apache:masterfrom
adangel:fix-toolchain-support-maven4
Closed

adangel wants to merge 3 commits into
apache:masterfrom
adangel:fix-toolchain-support-maven4

Conversation

@adangel

@adangel adangel commented Aug 20, 2026

Copy link
Copy Markdown
Member

This is addressing an edge case: The combination of using maven-pmd-plugin with a toolchain < java 17 and running maven4:

When PMD execution is forked with using the toolchain selected JVM < java 17, then we can't access maven classes from maven4 due to UnsupportedClassVersionError.

The classes PmdExecutor/CpdExecutor should only depend on classes of the maven-pmd-plugin or PMD, but not on maven. The exceptions have been replaced by the new PmdException, which is later on wrapped as MavenReportException or MojoExecutionException.

This enables the IT "MPMD-304-toolchain-support" (it was skipped before due to no Oracle JDK in toolchains).


Following this checklist to help us incorporate your
contribution quickly and easily:

  • Your pull request should address just one issue, without pulling in other changes.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body.
    Note that commits might be squashed by a maintainer on merge.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied.
    This may not always be possible but is a best-practice.
  • Run mvn verify to make sure basic checks pass.
    A more thorough check will be performed on your pull request automatically.
  • You have run the integration tests successfully (mvn -Prun-its verify).

If your pull request is about ~20 lines of code you don't need to sign an
Individual Contributor License Agreement if you are unsure
please ask on the developers list.

To make clear that you license your contribution under
the Apache License Version 2.0, January 2004
you have to acknowledge this by using the following check-box.

@adangel adangel added the bug Something isn't working label Aug 20, 2026
@adangel
adangel force-pushed the fix-toolchain-support-maven4 branch from ca7b8cf to ddbb60c Compare August 20, 2026 16:10
@adangel adangel added this to the 3.28.1 milestone Aug 20, 2026
When PMD execution is forked with using the toolchain selected JVM,
then we can't access maven classes from maven4 due to UnsupportedClassVersionError.

The classes PmdExecutor/CpdExecutor should only depend on
classes of the maven-pmd-plugin or PMD, but not on maven. The exceptions
have been replaced by the new PmdException, which is later on wrapped
as MavenReportException or MojoExecutionException.
@adangel
adangel force-pushed the fix-toolchain-support-maven4 branch from ddbb60c to 320a35a Compare September 10, 2026 08:38
Comment thread src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportConsumer.java Outdated
Comment thread src/main/java/org/apache/maven/plugins/pmd/exec/Executor.java Outdated
@adangel

adangel commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

So, this is really addressing a edge case here: Only when using maven4 (and you naturally run with java 17+ then) and want to analyze a java project, that uses e.g. java 8 or 11. In that case, you run into the problem with toolchains.

The solution here makes the whole plugin a bit more fragile - and the logging also doesn't work anymore.

Is it worth to support this edge case?

With #726, the project can remove the toolchain plugin/configuration and PMD will run in the same JVM as maven (without forking) but still use the correct toolchain for type resolution.
(See https://github.com/adangel/scratchpad/tree/auxclasspath-java-runtime). This requires an update of the project configuration, when they switch to maven4.

The current integration test (MPMD-304-toolchain-support) can be fixed easily by just using java 17 instead of 11... (see #736)

@Bukama @slachiewicz What do you think?

// lead to a UnsupportedClassVersionError.
int majorJavaVersion = determineJavaVersion(javaExecutable);
if (majorJavaVersion < 17) {
classpath.removeIf(s -> s.contains("maven-logging"));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this will break the logging. The output will look like this:

[INFO] --- pmd:3.28.1-SNAPSHOT:pmd (pmd) @ auxclasspath-java-runtime ---
[INFO] PMD version: 7.27.0
[INFO] Toolchain in maven-pmd-plugin: JDK[/home/andreas/programs/openjdk/eclipse-temurin/jdk-8]
[WARNING] [stderr] SLF4J(W): No SLF4J providers were found.
[WARNING] [stderr] SLF4J(W): Defaulting to no-operation (NOP) logger implementation
[WARNING] [stderr] SLF4J(W): See https://www.slf4j.org/codes.html#noProviders for further details.

And when running with -X, no logging from PMD itself is visible. So, not an optimal solution.

@adangel adangel removed this from the 3.29.0 milestone Sep 10, 2026
@Bukama

Bukama commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

So, this is really addressing a edge case here: Only when using maven4 (and you naturally run with java 17+ then) and want to analyze a java project, that uses e.g. java 8 or 11. In that case, you run into the problem with toolchains.

The solution here makes the whole plugin a bit more fragile - and the logging also doesn't work anymore.

Is it worth to support this edge case?

I think that there are still a lot of apps that target JDK 8, but I don't think that there are that much that will use such combination. My team builds on 17 but only compile against 8 - so no toolchain. However we get a lot of these auxClasspath warnings when building.

A broken logging raises a "I dont like it" gut feeling.

But as you can see by my words: Both are feelings, I don't have numbers. What about not supporting this edge case and wait until several users come up with such setup esp. as Maven 4 not out yet and we expect adoption will take some while? So I tend to not support this edge case.

@adangel

adangel commented Sep 15, 2026

Copy link
Copy Markdown
Member Author

My team builds on 17 but only compile against 8 - so no toolchain. However we get a lot of these auxClasspath warnings when building.

The auxClasspath warnings are addressed by #726 - but only if you have a matching toolchain in your toolchain.xml. You don't need to configure the toolchain-plugin, as otherwise, you'd run into exactly this issue (when using maven4).

A broken logging raises a "I dont like it" gut feeling.

But as you can see by my words: Both are feelings, I don't have numbers. What about not supporting this edge case and wait until several users come up with such setup esp. as Maven 4 not out yet and we expect adoption will take some while? So I tend to not support this edge case.

Yes, let's skip this edge case for now until there is a real need.
If this happens, one solution could be, to call PMD with its own CLI classes and infra/full dependencies (currently m-pmd-p just uses a custom wrapper around PMD classes). Then PMD would use its own logging and m-pmd-p would need to tunnel the process stdout/stderr back. PMD would still write the pmd.xml file, which can be read by m-pmd-p. Will be a bit tricky to get the dependencies correct, but that could work.

I'm closing this PR then.

@adangel adangel closed this Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants