自定义 PMD 规则集不起作用

Custom PMD ruleset not working

我已经创建了一个自定义的 PMD 规则集 xml 文件来排除一些规则检查。

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         name="full-pmd-ruleset"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
   <description>Full 5.1.1 PMD rule set</description>
   <rule ref="rulesets/java/coupling.xml">
     <exclude name="LawOfDemeter"/>
   </rule>
   <rule ref="rulesets/java/design.xml">
     <exclude name="GodClass"/>
   </rule>
   <rule ref="rulesets/java/unnecessary.xml">
     <exclude name="UnnecessaryFinalModifier"/>
     <exclude name="UnnecessaryReturn"/>
   </rule>

   <rule ref="rulesets/java/android.xml"/>
   <rule ref="rulesets/java/basic.xml"/>
   <rule ref="rulesets/java/braces.xml"/>
   <rule ref="rulesets/java/clone.xml"/>
   <rule ref="rulesets/java/codesize.xml"/>
   <rule ref="rulesets/java/comments.xml"/>
   <rule ref="rulesets/java/controversial.xml"/>
   <rule ref="rulesets/java/coupling.xml"/>
   <rule ref="rulesets/java/design.xml"/>
   <rule ref="rulesets/java/empty.xml"/>
   <rule ref="rulesets/java/finalizers.xml"/>
   <rule ref="rulesets/java/imports.xml"/>
   <rule ref="rulesets/java/j2ee.xml"/>
   <rule ref="rulesets/java/junit.xml"/>
   <rule ref="rulesets/java/logging-jakarta-commons.xml"/>
   <rule ref="rulesets/java/logging-java.xml"/>
   <rule ref="rulesets/java/javabeans.xml"/>
   <rule ref="rulesets/java/migrating.xml"/>
   <rule ref="rulesets/java/naming.xml"/>
   <rule ref="rulesets/java/optimizations.xml"/>
   <rule ref="rulesets/java/sunsecure.xml"/>
   <rule ref="rulesets/java/strictexception.xml"/>
   <rule ref="rulesets/java/strings.xml"/>
   <rule ref="rulesets/java/unnecessary.xml"/>
   <rule ref="rulesets/java/basic.xml"/>
   <rule ref="rulesets/java/unusedcode.xml"/>

   <rule ref="rulesets/plsql/codesize.xml"/>
   <rule ref="rulesets/plsql/TomKytesDespair.xml"/>
   <rule ref="rulesets/plsql/dates.xml"/>

</ruleset>

我使用以下命令 运行 PMD 与此规则集:

pmd -d C:\Users\leo\Documents\workspace\SampleProject\src\Sample.java -f html -R CustomRuleset.xml>Report.html

它运行良好,但排除 xml 中提到的规则不起作用。它也采用那些被排除的规则。

我做错了什么?

一切看起来都很好,只是您要导入一些规则集两次:)

例如rulesets/java/coupling.xml导入一次:

<rule ref="rulesets/java/coupling.xml">
    <exclude name="LawOfDemeter"/>
</rule>

稍后再次排除:

<rule ref="rulesets/java/coupling.xml"/>

如果删除重复的规则集 rulesets/java/coupling.xmlrulesets/java/design.xmlrulesets/java/unnecessary.xml,问题就会解决。

PMD 只是从上到下读取您的自定义规则集并愚蠢地执行文件中的内容:从 coupling.xml 导入所有规则,LawOfDemeter 除外,然后再次导入 coupling.xml 的所有规则, 这次包括 LawOfDemeter.

有趣的是,如果规则导入两次,PMD 没有问题。这种情况被默默地忽略了。但也许 PMD 应该引发错误? WDYT?