Skip to content

[MPMD-399] Incorrect warning: The project X does not seem to be compiled. PMD results might be inaccurate. #366

Closed

Description

@jira-importer

AB-xdev opened MPMD-399 and commented

PMD plugin is reporting the following warnings on in a multi-maven project:
"The project X does not seem to be compiled. PMD results might be inaccurate."

This is caused by the fact that not all modules have a

  • src/main/java or
  • src/test/java directory

and therefore no target/classes or target/test-classes directories are generated.

The affected code that causes the problem can be found here:

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.");
}
}
}

Reproduction example

  • Create a maven multi-module project
  • Leave src/main/java of one module empty
  • Execute pmd:aggregate-pmd or test pmd:aggregate-pmd-no-fork -DskipTests

→ (x) Warning is reported

Possible fixes

  • Make it possible to ignore this warning
  • Do not warn when at least target/classes or target/test-classes is present

 


Affects: 3.22.0

Remote Links:

Activity

  1. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    What packaging type is the project in question?

  2. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    AB-xdev commented

    The packaging type is jar

  3. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    Does it work for your with -Dpmd.typeResolution=false?

  4. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    AB-xdev commented

    Well it then fails with some false positives (as the types are not resolved), but yes the warning is then gone.

    I still prefer some incorrect warnings instead of FP, so I don't think this is a long term solution ;)

  5. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    I am inclined to delete this entire block

    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.");
    }
    }
    }
    because in a single module there is not such message, but would make sense to be consistent:
    } else {
    classpath.addAll(
    includeTests ? project.getTestClasspathElements() : project.getCompileClasspathElements());

    Either both or none, WDYT?

  6. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    AB-xdev commented

    Well I'm not that familiar with the code, but anyway my opinion:

    Block 1: Either delete it or add some kind of flag that let's the end-user control this check
    Block 2: According to https://github.com/apache/maven-pmd-plugin/blob/master/src/main/java/org/apache/maven/plugins/pmd/AbstractPmdReport.java#L548 this seems to be deprecated anyway? Maybe a good time to remove it :)

  7. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    The method is deprecated, but the information is still required and needs to be obtained elsewhere. That is different discussion. I would take care of the message.

    Why do you have a JAR project w/o source code in the first place?

  8. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    AB-xdev commented

    I have source code, but some modules only have it in

    • src/main and nothing inside src/test (Example: a small API module that only contains a few classes which don't have any tests)
    • or just in src/test and nothing inside src/main (dedicated projects for integration tests for example)

    If either of this is the case the warning occurs

  9. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    Hold on, that confuses me. In the default case if src/main is there there should be not warning, only if src/tests are there and includeTests is false and the other way around: true and only src/main. The first case I do not understand, but the latter makes sense. You should see the warning. Can you confirm?

  10. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    AB-xdev commented

    includeTests is always true (set it on the parent) and produces the behavior described in https://issues.apache.org/jira/browse/MPMD-399?focusedCommentId=17853672&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-17853672

    if I remove includeTests (or set it to false) it I still get warnings for the "src/test and nothing inside src/main"-case.

  11. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    I have a slight problem with includeTests: It documentation says "Run PMD on the tests.". Looking at

    if (null == testSourceRoots) {
    testSourceRoots = project.getTestCompileSourceRoots();
    }
    if (includeTests && testSourceRoots != null) {
    for (String root : testSourceRoots) {
    File sroot = new File(root);
    if (sroot.exists()) {
    String testXref = constructXRefLocation(true);
    directories.add(new PmdFileInfo(project, sroot, testXref));
    }
    }
    }
    if (isAggregator()) {
    for (MavenProject localProject : getAggregatedProjects()) {
    List<String> localCompileSourceRoots = localProject.getCompileSourceRoots();
    for (String root : localCompileSourceRoots) {
    File sroot = new File(root);
    if (sroot.exists()) {
    String sourceXref = constructXRefLocation(false);
    directories.add(new PmdFileInfo(localProject, sroot, sourceXref));
    }
    }
    if (includeTests) {
    List<String> localTestCompileSourceRoots = localProject.getTestCompileSourceRoots();
    for (String root : localTestCompileSourceRoots) {
    File sroot = new File(root);
    if (sroot.exists()) {
    String testXref = constructXRefLocation(true);
    directories.add(new PmdFileInfo(localProject, sroot, testXref));
    }
    }
    }
    }
    }
    . The operation is additive/inclusive. The description is wrong. But here, it is exclusive:
    List<String> projectClasspath = includeTests
    ? localProject.getTestClasspathElements()
    : localProject.getCompileClasspathElements();

    Andreas Dangel, shouldn't auxClasspath include main classpath as well when tests should be included? Since dependencies are inclusive as well:

    TransformableFilter filter = new AndFilter(Arrays.asList(
    new ExclusionsFilter(exclusionPatterns),
    includeTests
    ? ScopeFilter.including("compile", "provided", "test")
    : ScopeFilter.including("compile", "provided")));

    It does not compute fo me.

    AB-xdev, sorry to deviate, but that needs to clarified first.

  12. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    I am also confused why the single project case does not include the deps...

  13. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    Forget my previous questions because localProject.getTestClasspathElements() is cumulative. Ony the documentation needs to be updated. AB-xdev, we can get back to the actual problem.

  14. jira-importer commented on Jun 10, 2024

    @jira-importer
    Author

    Michael Osipov commented

    Have a look at the PR.

  15. jira-importer commented on Jun 11, 2024

    @jira-importer
    Author

    Andreas Dangel commented

    Andreas Dangel, shouldn't auxClasspath include main classpath as well when tests should be included?

    Yes, when analyzing test code, the auxClasspath should contain both main+test dependencies.

  16. jira-importer commented on Jun 11, 2024

    @jira-importer
    Author

    Michael Osipov commented

    ...and it does. I verified by debugging it. The comment for the param is just confusing.

  17. jira-importer commented on Jul 7, 2024

    @jira-importer
    Author
  18. added
    bug
    priority:major
    on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpriority:major

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions