使用maven中的pmd规则集,如何选择单个规则?

时间:2014-12-29 04:16:46

标签: java maven pmd

我使用以下内容查看PMD in maven中的错误异常模式:

<configuration>
    <rulesets>
        <ruleset>/rulesets/java/strictexception.xml</ruleset>
    </rulesets>
</configuration>

这就是我想要做的,但它不起作用(用于诊断和专注于特定需求)

<configuration>
    <rulesets>
        <ruleset>/rulesets/java/strictexception.xml/AvoidCatchingThrowable</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/SignatureDeclareThrowsException</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/ExceptionAsFlowControl</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidCatchingNPE</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidThrowingRawExceptionTypes</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidThrowingNullPointerException</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidRethrowingException</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/DoNotExtendJavaLangError</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/DoNotThrowExceptionInFinally</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidThrowingNewInstanceOfSameException</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidCatchingGenericException</ruleset>
        <ruleset>/rulesets/java/strictexception.xml/AvoidLosingExceptionInformation</ruleset> -->
    </rulesets>
</configuration>

我的问题是:使用maven中的pmd规则集,如何选择单个规则?

1 个答案:

答案 0 :(得分:0)

maven不能直接使用。

我建议您创建自定义规则集文件,并在pom.xml中引用此单个规则集。

自定义规则集文件:http://pmd.sourceforge.net/pmd-5.2.3/customizing/howtomakearuleset.html

示例(文件名custom-ruleset.xml):

<?xml version="1.0"?>
<ruleset name="Custom ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
  <description>
    Only some selected rules for bad exception patterns
  </description>

  <rule ref="rulesets/java/strictexception.xml/AvoidCatchingThrowable" />
  <rule ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException" />
  <rule ref="rulesets/java/strictexception.xml/ExceptionAsFlowControl" />
  <rule ref="rulesets/java/strictexception.xml/AvoidCatchingNPE" />
  <rule ref="rulesets/java/strictexception.xml/AvoidThrowingRawExceptionTypes" />
  <rule ref="rulesets/java/strictexception.xml/AvoidThrowingNullPointerException" />
  <rule ref="rulesets/java/strictexception.xml/AvoidRethrowingException" />
  <rule ref="rulesets/java/strictexception.xml/DoNotExtendJavaLangError" />
  <rule ref="rulesets/java/strictexception.xml/DoNotThrowExceptionInFinally" />
  <rule ref="rulesets/java/strictexception.xml/AvoidThrowingNewInstanceOfSameException" />
  <rule ref="rulesets/java/strictexception.xml/AvoidCatchingGenericException" />
  <rule ref="rulesets/java/strictexception.xml/AvoidLosingExceptionInformation" />

</ruleset>

请注意,您需要指定原始规则集名称而不使用前导斜杠,否则PMD将无法找到规则集。 使用自定义规则集还可能有其他一些技巧,例如:导入完整的规则集,但exclude one or more rules

Maven PMD配置:

<configuration>
    <rulesets>
        <ruleset>custom-ruleset.xml</ruleset>
    </rulesets>
</configuration>