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

Viewed 9705

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?

5 Answers

If you are using the maven-pmd-plugin tool to run PMD, then you can include a properties file listing the classes and rules to ignore.

exclude-pmd.properties

org.apache.maven.ClassA=UnusedPrivateField,EmptyCatchBlock
org.apache.maven.ClassB=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod

pom.xml

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <version>3.8</version>
        <executions>
          <execution>
            <goals>
              <goal>check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
          <execution>
            <goals>
              <goal>cpd-check</goal>
            </goals>
            <configuration>
              <excludeFromFailureFile>exclude-cpd.properties</excludeFromFailureFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

More Details: https://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

try this:

<rule ref="category/java/bestpractices.xml/UnusedPrivateField">
    <properties>
      <!--Ignore UnusedPrivateField on classes where the class name ends with DTO-->
      <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration['.*DTO']"/>
    </properties>
  </rule>

You can also exclude folders. for example, you want to exclude folders having "sample" name, in this case value will be like:

value="//ClassOrInterfaceDeclaration['.*/sample/.*']"

for more details, check here https://github.com/pmd/pmd/issues/1142

Yes you can.
In you code/rule use the following syntax:

<rule ref=rulesets/java/logging-java.xml>
   <property name="violationSuppressXPath" 
      value="./ancestor::ClassOrInterfaceDeclaration[matches(@SimpleName, 'Ignore')]" />
</rule>

Explanation:
From official documentation:

This property defines an XPath query to be executed using the violation node as the context node. If the XPath query matches anything, then the violation will be suppressed

Notes:

  1. Official documentation gives examples to use contains and matches to exclude files by patterns.
  2. Official documentation warns about using //.

Note the use of . to refer to the context node. Using // at the start of the expression should be avoided, as it would test all nodes in the file, and suppress more violations than expected.

  1. For more info regarding XPath rules that could be used, please check this page .
Related