When I run a PMD analysis I receive violation:
Each class should declare at least one constructor
This violation is on a Spring controller. This controller is instantiated by Spring, so I shouldn't need to invoke this class.
What is recommended way of ignoring this violation?
According to this doc can use //NOPMD
but I just want to ignore specific violation.
NOPMD comment ✏️️ Note that PMD expects the //NOPMD marker to be on the same line as the violation. So, for example, if you want to suppress an “empty if statement” warning, you'll need to place it on the line containing the if keyword, e.g.: $ cat ~/tmp/Foo.
PMD violations are assigned a priority from 1 (most severe) to 5 (least severe) according the the rule's priority. Violations at or less than this priority level are considered failures and will fail the build if failOnViolation=true and the count exceeds maxAllowedViolations .
Conceptually, PMD rules work by matching a “pattern” against the AST of a file. Rules explore the AST and find nodes that satisfy some conditions that are characteristic of the specific thing the rule is trying to flag. Rules then report a violation on these nodes.
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.
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