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

Mark picture Mark · Jul 25, 2012 · Viewed 8.3k times · Source

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?

Answer

lightswitch05 picture lightswitch05 · Sep 26, 2017

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