Maven PMD 插件中忽略的 skipAnnotations

skipAnnotations ignored in Maven PMD plugin

我正在为默认 PMD strings.xml 规则集指定 skipAnnotations,值为 true

<rule ref="rulesets/java/strings.xml">
    <properties>
        <property name="skipAnnotations" value="true"/>
    </properties>
</rule>

在像

这样的简单情况下会被忽略
public class NewMain {

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void method1(Object arg1) {
        System.out.println("method1");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void method2(Object arg1) {
        System.out.println("method2");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void method3(Object arg1) {
        System.out.println("method3");
    }

    @SuppressWarnings("PMD.UnusedFormalParameter")
    private void method4(Object arg1) {
        System.out.println("method4");
    }
}

mvn validate 由于 Failed to execute goal org.apache.maven.plugins:maven-pmd-plugin:3.8:check (pmd-check) on project pmd-skip-annotations-demo: You have 1 PMD violation. [...].

而失败

MCVE 位于 https://github.com/krichter722/pmd-skip-annotations-demo

我正在使用 maven-pmd-plugin 3.8.

属性 对应给定的规则,而不是整个规则集。因此,你的配置是无效的,你应该这样写:

<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
    <properties>
        <property name="skipAnnotations" value="true"/>
    </properties>
</rule>

要包含整个字符串规则集,但有这个 属性,你应该写

<rule ref="rulesets/java/strings.xml">
    <exclude name="AvoidDuplicateLiterals"/>
</rule>
<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
    <properties>
        <property name="skipAnnotations" value="true"/>
    </properties>
</rule>