自定义PMD规则集无法正常工作

时间:2016-10-28 06:13:57

标签: java xml command-line pmd

我创建了一个自定义的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中提到的规则不起作用。它也是采取那些被排除的规则。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

除了您要导入一些规则集两次外,所有看起来都很好:)

E.g。导入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只是从上到下读取你的自定义规则集并且愚蠢地执行文件中的内容:从couplingO中导入除了LawOfDemeter之外的所有规则,稍后再次导入coupling.xml中的所有规则,这次包括LawOfDemeter。

有趣的是,如果规则被导入两次,PMD没有问题。这个案例被默默地忽略了。但也许PMD会引发错误呢? WDYT?