Skip to content

Commit

Permalink
[MPMD-399] Incorrect warning: The project X does not seem to be compi…
Browse files Browse the repository at this point in the history
…led. PMD results might be inaccurate.

Update also FAQ about aggregate and type resolution-

Co-authored-by: Andreas Dangel <adangel@apache.org>

This closes #154
  • Loading branch information
michael-o committed Jul 7, 2024
1 parent 71c807b commit 2a72cc8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -152,7 +152,8 @@ public abstract class AbstractPmdReport extends AbstractMavenReport {
*
* @since 2.2
* @deprecated since 3.15.0 Use the goals <code>pmd:aggregate-pmd</code> and <code>pmd:aggregate-cpd</code>
* instead.
* instead. See <a href="https://maven.apache.org/plugins/maven-pmd-plugin/faq.html#typeresolution_aggregate">FAQ:
* Why do I get sometimes false positive and/or false negative violations?</a> for an explanation.
*/
@Parameter(property = "aggregate", defaultValue = "false")
@Deprecated
Expand Down
22 changes: 5 additions & 17 deletions src/main/java/org/apache/maven/plugins/pmd/PmdReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,23 +513,11 @@ private String determineAuxClasspath() throws MavenReportException {
resolvedArtifact.getArtifact().getFile().toString());
}

List<String> 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
Expand Down
13 changes: 6 additions & 7 deletions src/site/fml/faq.fml
Original file line number Diff line number Diff line change
Expand Up @@ -101,25 +101,24 @@ under the License.
</faq>
<faq id="typeresolution aggregate">
<question>
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?
</question>
<answer>
<p>
In order to improve PMD's results, type resolution should be used. It is enabled by default
(property <a href="pmd-mojo.html#typeResolution">typeResolution</a>) 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.
</p>
<p>
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.
</p>
<p>
When using the property <a href="pmd-mojo.html#aggregate">aggregate</a>, this is problematic: With
aggregate=true, PMD is executed at the root of a multi-module project <em>before the individual
modules are built</em>. 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.
</p>
<p>
In order to use type resolution and aggregate together, maven needs to be execute in two passes:
Expand All @@ -135,4 +134,4 @@ under the License.
</answer>
</faq>
</part>
</faqs>
</faqs>

0 comments on commit 2a72cc8

Please sign in to comment.