Search code examples
javapmd

Can you exclude a source file for a specific PMD rule?


When defining a PMD ruleset is it possible to exclude a source file from a specific rule?

I want to do something like the following:

<rule ref=rulesets/java/logging-java.xml>
  <exclude name="Ignore.java" />
</rule>

Exclude only seems to be supported for rule names. Is there anything similar for source files?


Solution

  • Not without writing a custom rule that adds logic for excluding by file name.

    I think in your scenario the best approach is to run PMD in two passes - one with the larger set of rules against all the code. And one with the smaller set of rules against the code you want extra checks on.

    This does have the downside of generating two reports. But that is an easier problem to solve than the one you posed (or than creating a custom rule.) PMD can generate XML output. You can merge those programmatically and then call PMD's code to generate an HTML report at the end. Or you can just have two reports and be done right away.