adangel commented on code in PR #726:
URL: https://github.com/apache/maven-pmd-plugin/pull/726#discussion_r3821452415


##########
src/main/java/org/apache/maven/plugins/pmd/PmdReport.java:
##########
@@ -487,20 +505,114 @@ private String determineAuxClasspath() throws 
MavenReportException {
 
                 // Add the dependencies as last entries
                 classpath.addAll(dependencies);
+                classpath.removeIf(this::emptyOrNotExisting);
 
                 getLog().debug("Using aggregated aux classpath: " + classpath);
             } else {
                 classpath.addAll(
                         includeTests ? project.getTestClasspathElements() : 
project.getCompileClasspathElements());
+                classpath.removeIf(this::emptyOrNotExisting);
 
                 getLog().debug("Using aux classpath: " + classpath);
             }
+
+            classpath.addAll(determineJavaRuntimeClasses());
+
             return String.join(File.pathSeparator, classpath);
         } catch (Exception e) {
             throw new MavenReportException(e.getMessage(), e);
         }
     }
 
+    private boolean emptyOrNotExisting(String entry) {
+        Path path = Paths.get(entry);
+        if (Files.isDirectory(path)) {
+            boolean emptyDirectory = false;
+            try (Stream<Path> stream = Files.list(path)) {
+                emptyDirectory = !stream.findAny().isPresent();
+            } catch (IOException ex) {
+                throw new UncheckedIOException(ex);
+            }
+            if (emptyDirectory) {
+                getLog().debug("Ignoring empty directory for aux classpath: " 
+ path);
+                return true;
+            }
+        } else if (!Files.exists(path)) {
+            getLog().debug("Ignoring non-existing entry for aux classpath: " + 
path);
+            return true;
+        }
+        return false;
+    }
+
+    private List<String> determineJavaRuntimeClasses() {
+        if (!"java".equals(language) && null != language) {
+            return Collections.emptyList();
+        }
+        if (targetJdk == null) {
+            return Collections.emptyList();
+        }
+
+        List<Toolchain> toolchains =
+                toolchainManager.getToolchains(session, "jdk", 
Collections.singletonMap("version", targetJdk));
+        if (toolchains != null && !toolchains.isEmpty()) {
+            Toolchain toolchain = toolchains.get(0);
+            String javaPath = toolchain.findTool("java");
+
+            if (javaPath != null) {
+                Path javaHome = 
Paths.get(javaPath).resolve("../..").normalize();
+                Path runtimeJar = findJavaRuntimeJar(javaHome);
+                if (runtimeJar != null) {
+                    getLog().debug("Adding " + runtimeJar + " to aux 
classpath");
+                    return Collections.singletonList(runtimeJar.toString());
+                }
+
+                getLog().warn("Couldn't find java runtime classes in " + 
javaHome
+                        + ". Please make sure the java installation exists.");
+            } else {
+                getLog().warn("Could't find java executable for " + toolchain
+                        + ". Please double check your toolchains.xml for 
outdated entries.");
+            }
+        }
+
+        // Always apply fallback (in case of no or invalid toolchain) to avoid 
an additional warning
+        // from PMD itself.
+        //
+        // The fallback is to use to current runtime - this might be the wrong 
java version for targetJdk,
+        // but preserves old (pre 3.29.0) behavior.

Review Comment:
   I'd vote for 3.29.0, since this is an enhancement.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to