Suppressing violations in pmd

℡╲_俬逩灬. 提交于 2021-02-14 18:49:47

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!