从命令行运行所有PMD规则集

时间:2014-04-16 17:21:14

标签: pmd command-line-interface

我想知道是否有办法从命令行运行所有PMD规则集。

我使用了与Eclipse IDE和Maven集成的PMD。但现在我需要从CLI运行它。 我已经检查了此页面http://pmd.sourceforge.net/pmd-5.1.0/running.html,它说你可以从CLI运行它,但是使用指定的规则集:

C:\tmp\pmd-bin-5.1.0\pmd\bin>pmd -d c:\data\pmd\pmd\test-data\Unused1.java -f xml -R rulesets/java/unusedcode.xml

在该示例中,您只是获得了 Java unused code 规则的结果,并且我试图实现以下目标:

C:\tmp\pmd-bin-5.1.0\pmd\bin>pmd -d c:\data\pmd\pmd\test-data\Unused1.java -f xml -R rulesets/java/*.xml

并获取Java规则集中所有规则的结果。

1 个答案:

答案 0 :(得分:8)

您可以定义包含您希望运行的规则集的配置文件。您可以在命令行上的-R参数之后将此文件作为参数提供。

这里有一个示例文件(MyRules.xml):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ruleset name="PMD.rul" 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>This ruleset was created from PMD.rul</description>

  <rule ref="rulesets/java/basic.xml">
    <exclude name="UnnecessaryFinalModifier"/>
  </rule>

  <rule ref="rulesets/java/braces.xml"/>
  <rule ref="rulesets/java/clone.xml"/>
  <rule ref="rulesets/java/comments.xml"/>

  <rule ref="rulesets/java/controversial.xml">
    <exclude name="AtLeastOneConstructor"/>
    <exclude name="UnnecessaryParentheses"/>
  </rule>

  <rule ref="rulesets/java/design.xml">
    <exclude name="GodClass"/>
  </rule>

  <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/javabeans.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/naming.xml"/>
  <rule ref="rulesets/java/optimizations.xml"/>
  <rule ref="rulesets/java/strictexception.xml"/>
  <rule ref="rulesets/java/strings.xml"/>
  <rule ref="rulesets/java/sunsecure.xml"/>
  <rule ref="rulesets/java/typeresolution.xml"/>

  <rule ref="rulesets/java/unnecessary.xml">
    <exclude name="UnnecessaryFinalModifier"/>
    <exclude name="UnnecessaryReturn"/>
    <exclude name="UselessParentheses"/>
  </rule>

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

</ruleset>

命令行参数如下所示:

C:\tmp\pmd-bin-5.1.0\pmd\bin>pmd -d c:\data\pmd\pmd\test-data\Unused1.java -f xml -R MyRules.xml