From 2a72cc86d5f25625059a2e7428038cb1841ac612 Mon Sep 17 00:00:00 2001 From: Michael Osipov Date: Mon, 10 Jun 2024 17:09:40 +0200 Subject: [PATCH] [MPMD-399] Incorrect warning: The project X does not seem to be compiled. PMD results might be inaccurate. Update also FAQ about aggregate and type resolution- Co-authored-by: Andreas Dangel This closes #154 --- .../maven/plugins/pmd/AbstractPmdReport.java | 5 +++-- .../apache/maven/plugins/pmd/PmdReport.java | 22 +++++-------------- src/site/fml/faq.fml | 13 +++++------ 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java index 87c40d85..f5f0799d 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java @@ -140,7 +140,7 @@ public abstract class AbstractPmdReport extends AbstractMavenReport { private File[] excludeRoots; /** - * Run PMD on the tests. + * Run PMD on the tests as well. * * @since 2.2 */ @@ -152,7 +152,8 @@ public abstract class AbstractPmdReport extends AbstractMavenReport { * * @since 2.2 * @deprecated since 3.15.0 Use the goals pmd:aggregate-pmd and pmd:aggregate-cpd - * instead. + * instead. See FAQ: + * Why do I get sometimes false positive and/or false negative violations? for an explanation. */ @Parameter(property = "aggregate", defaultValue = "false") @Deprecated diff --git a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java index 5b3a14c8..b15cce11 100644 --- a/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java +++ b/src/main/java/org/apache/maven/plugins/pmd/PmdReport.java @@ -513,23 +513,11 @@ private String determineAuxClasspath() throws MavenReportException { resolvedArtifact.getArtifact().getFile().toString()); } - List projectClasspath = includeTests - ? localProject.getTestClasspathElements() - : localProject.getCompileClasspathElements(); - - // Add the project's target folder first - classpath.addAll(projectClasspath); - if (!localProject.isExecutionRoot()) { - for (String path : projectClasspath) { - File pathFile = new File(path); - String[] children = pathFile.list(); - - if (!pathFile.exists() || (children != null && children.length == 0)) { - getLog().warn("The project " + localProject.getArtifactId() - + " does not seem to be compiled. PMD results might be inaccurate."); - } - } - } + // Add the project's classes first + classpath.addAll( + includeTests + ? localProject.getTestClasspathElements() + : localProject.getCompileClasspathElements()); } // Add the dependencies as last entries diff --git a/src/site/fml/faq.fml b/src/site/fml/faq.fml index bcef70af..c7b9f618 100644 --- a/src/site/fml/faq.fml +++ b/src/site/fml/faq.fml @@ -101,25 +101,24 @@ under the License. - What does the warning "The project xyz does not seem to be compiled. PMD results might be inaccurate." mean? + Why do I get sometimes false positive and/or false negative violations?

In order to improve PMD's results, type resolution should be used. It is enabled by default (property typeResolution) and helps to avoid false positive - findings by matching the exact types of method parameters or variables. + or false negative findings by matching the exact types of method parameters or variables.

- However, this requires that the project is built first, so that not only the project's dependencies + However, this requires that the project is built first so that not only the project's dependencies can be used for type resolution, but also the project's classes as well.

When using the property aggregate, this is problematic: With aggregate=true, PMD is executed at the root of a multi-module project before the individual modules are built. Then the types of the individual projects are not available, which might lead to - false positive findings e.g. for the rule "UnusedPrivateMethod". - If this might be the case, then the warning "The project xyz does not seem - to be compiled. PMD results might be inaccurate" is issued. + false positive findings e.g. for the rule "UnusedPrivateMethod". That's why this property has + been deprecated.

In order to use type resolution and aggregate together, maven needs to be execute in two passes: @@ -135,4 +134,4 @@ under the License. - \ No newline at end of file +