java 抑制 pmd 中的违规行为

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32272138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:55:52  来源:igfitidea点击:

Suppressing violations in pmd

javaspringpmd

提问by blue-sky

When I run a pmd analysis I recive violation :

当我运行 pmd 分析时,我收到了违规:

Each class should declare at least one constructor

This violation is on a Spring controller. This controller is instantiated by Spring so should'nt need to invoke this class.

此违规发生在 Spring 控制器上。此控制器由 Spring 实例化,因此不需要调用此类。

What is recommended way of ignoring this violation ?

忽略这种违规行为的推荐方法是什么?

According to http://pmd.sourceforge.net/pmd-4.3/suppressing.htmlcan use //NOPMD but I just want to ignore specific violation.

根据http://pmd.sourceforge.net/pmd-4.3/suppressing.html可以使用 //NOPMD 但我只想忽略特定的违规行为。

回答by HairyFotr

PMD also supports the @SuppressWarnings annotations:

PMD 还支持 @SuppressWarnings 注释:

// 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.

您可能还想研究的是创建规则集和排除项。也许您想禁用某个规则,或者从 PMD 检查中排除某些文件。

回答by akshayp

@blue-sky 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.

@blue-sky 在我的组织中,我使用 POM 文件中的 PMD 忽略选项来抑制为我们自动生成的客户端存根(并保存在单独的模块中)生成的所有警告,因为它们是我们不使用的第三方客户端存根”认为有任何警告或违规行为时往往会触动他们,我也为您假设了同样的事情。

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

下面是 POM 文件的一个片段,它有客户端存根模块

<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.

这样我们就可以完全跳过 PMD 插件引发的警告。

Hope this helps you and I am correct with your question understanding.

希望这对您有所帮助,我对您的问题理解是正确的。

Regards, Akshay

问候, 阿克谢