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?
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
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.
org.apache.maven.ClassA=UnusedPrivateField,EmptyCatchBlock
org.apache.maven.ClassB=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod
<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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With