You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "adangel (via GitHub)" <gi...@apache.org> on 2026/08/20 15:54:51 UTC

[PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

adangel opened a new pull request, #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728

   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.
   
   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:
   
   - [x] Your pull request should address just one issue, without pulling in other changes.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] 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.
   - [x] 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.
   - [x] Run `mvn verify` to make sure basic checks pass.
     A more thorough check will be performed on your pull request automatically.
   - [x] 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](https://www.apache.org/licenses/icla.pdf) 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](http://www.apache.org/licenses/LICENSE-2.0)
   you have to acknowledge this by using the following check-box.
   
   - [x] I hereby declare this contribution to be licenced under the [Apache License Version 2.0, January 2004](http://www.apache.org/licenses/LICENSE-2.0)
   - [ ] In any other case, please file an [Apache Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel commented on code in PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#discussion_r3977204565


##########
src/main/java/org/apache/maven/plugins/pmd/exec/CpdReportConsumer.java:
##########
@@ -83,7 +83,12 @@ private void writeXmlReport(CPDReport cpd) throws IOException {
     }
 
     private void writeFormattedReport(CPDReport cpd) throws IOException, MavenReportException {
-        CPDReportRenderer renderer = CpdExecutor.createRenderer(request.getFormat(), request.getOutputEncoding());
+        CPDReportRenderer renderer = null;
+        try {
+            renderer = CpdExecutor.createRenderer(request.getFormat(), request.getOutputEncoding());
+        } catch (PmdException e) {
+            throw new MavenReportException(e.getMessage(), e);
+        }
         writeReport(cpd, renderer, request.getFormat());

Review Comment:
   Wrap the whole block inside a try catch for readability



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "Bukama (via GitHub)" <gi...@apache.org>.
Bukama commented on PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#issuecomment-5629644317

   > 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.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel commented on PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#issuecomment-5684675642

   > 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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel commented on PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#issuecomment-5616609620

   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...
   
   @Bukama @slachiewicz What do you think?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel commented on code in PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#discussion_r3977862766


##########
src/main/java/org/apache/maven/plugins/pmd/exec/Executor.java:
##########
@@ -29,37 +29,83 @@
 import java.net.URLClassLoader;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.codehaus.plexus.util.IOUtil;
 
 abstract class Executor {
 
-    protected static String buildClasspath() {
-        StringBuilder classpath = new StringBuilder();
+    protected static String buildClasspath(String javaExecutable) {
+        List<String> classpath = new ArrayList<>();
 
         // plugin classpath needs to come first
         ClassLoader pluginClassloader = Executor.class.getClassLoader();
-        buildClasspath(classpath, pluginClassloader);
+        classpath.addAll(buildClasspath(pluginClassloader));
 
         ClassLoader coreClassloader = ConsoleLogger.class.getClassLoader();
-        buildClasspath(classpath, coreClassloader);
+        classpath.addAll(buildClasspath(coreClassloader));
 
-        return classpath.toString();
+        // Determine Java version: When running under maven4 and a toolchain
+        // selects a jdk < 17, then we need to exclude "maven-logging-x.jar" from the
+        // classpath. Otherwise slf4j's Logging Factory will try to initialize Maven4's
+        // service provider (org.apache.maven.slf4j.MavenServiceProvider) which will
+        // lead to a UnsupportedClassVersionError.
+        int majorJavaVersion = determineJavaVersion(javaExecutable);
+        if (majorJavaVersion < 17) {
+            classpath.removeIf(s -> s.contains("maven-logging"));

Review Comment:
   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.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel closed pull request #728: Fix toolchain support for maven4 + java < 17
URL: https://github.com/apache/maven-pmd-plugin/pull/728


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix toolchain support for maven4 + java < 17 [maven-pmd-plugin]

Posted by "adangel (via GitHub)" <gi...@apache.org>.
adangel commented on code in PR #728:
URL: https://github.com/apache/maven-pmd-plugin/pull/728#discussion_r3977215548


##########
src/main/java/org/apache/maven/plugins/pmd/exec/Executor.java:
##########
@@ -29,37 +29,83 @@
 import java.net.URLClassLoader;
 import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.codehaus.plexus.logging.console.ConsoleLogger;
+import org.codehaus.plexus.util.IOUtil;
 
 abstract class Executor {
 
-    protected static String buildClasspath() {
-        StringBuilder classpath = new StringBuilder();
+    protected static String buildClasspath(String javaExecutable) {
+        List<String> classpath = new ArrayList<>();
 
         // plugin classpath needs to come first
         ClassLoader pluginClassloader = Executor.class.getClassLoader();
-        buildClasspath(classpath, pluginClassloader);
+        classpath.addAll(buildClasspath(pluginClassloader));
 
         ClassLoader coreClassloader = ConsoleLogger.class.getClassLoader();
-        buildClasspath(classpath, coreClassloader);
+        classpath.addAll(buildClasspath(coreClassloader));
 
-        return classpath.toString();
+        // Determine Java version: When running under maven4 and a toolchain
+        // selects a jdk < 17, then we need to exclude "maven-logging-x.jar" from the
+        // classpath. Otherwise slf4j's Logging Factory will try to initiale Maven4's

Review Comment:
   ```suggestion
           // classpath. Otherwise slf4j's Logging Factory will try to initialize Maven4's
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org