How to suppress violations in PMD?

PMD also supports the @SuppressWarnings annotations:

// This will suppress all the PMD warnings in this class
@SuppressWarnings("PMD")
public class Bar {
 void bar() {
  int foo;
 }
}

Or just one type of warning:

// This will suppress UnusedLocalVariable warnings in this class
@SuppressWarnings("PMD.UnusedLocalVariable")
public class Bar {
 void bar() {
  int foo;
 }
}

And what you might also want to look into are creating a ruleset and exclusions. Maybe you want to disable a certain rule, or exclude certain files from PMD checking.


In my organization I use the PMD ignore option in POM file to suppress all warnings that are generated for client stubs (and kept in separate module) that we auto-generate, as those are third party client-stubs we don't tend to touch them thought there are any warnings or violations and I am assuming the same thing for you as we well.

Below is a snippet from POM file, which has client stub module

<build>
    <plugins>
        <plugin>
            <!-- <groupId>org.apache.maven.plugins</groupId> -->
            <artifactId>maven-pmd-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>

    <pluginManagement>
        <plugins>       
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.8</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

This way we can entirely skip the warnings that are raised by PMD plugin.

Tags:

Java

Spring

Pmd