Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

pmd

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?

like image 770
Mark Avatar asked Jul 25 '12 08:07

Mark


2 Answers

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

like image 195
Ragini Yadav Avatar answered Oct 11 '22 04:10

Ragini Yadav


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

like image 31
lightswitch05 Avatar answered Oct 11 '22 02:10

lightswitch05