简体   繁体   English

如何抑制 PMD 中的违规行为?

[英]How to suppress violations in PMD?

When I run a PMD analysis I receive violation:当我运行 PMD 分析时,我收到违规信息:

Each class should declare at least one constructor每个类应该至少声明一个构造函数

This violation is on a Spring controller.此违规发生在 Spring 控制器上。 This controller is instantiated by Spring, so I shouldn't need to invoke this class.这个控制器是由 Spring 实例化的,所以我不需要调用这个类。

What is recommended way of ignoring this violation?忽略此违规行为的推荐方法是什么?

According to this doc can use //NOPMD but I just want to ignore specific violation.根据此文档可以使用//NOPMD但我只想忽略特定的违规行为。

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 检查中排除某些文件。

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.在我的组织中,我使用 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 插件引发的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM