问题
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 http://pmd.sourceforge.net/pmd-4.3/suppressing.html can use //NOPMD but I just want to ignore specific violation.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/32272138/suppressing-violations-in-pmd