Running all PMD rulesets from command line

Angelo picture Angelo · Apr 16, 2014 · Viewed 7.3k times · Source

I want to know if there is a way to run all PMD rulesets from command line.

I've used PMD integrated with Eclipse IDE and Maven. But now I need to run it from CLI. I've checked this page http://pmd.sourceforge.net/pmd-5.1.0/running.html and it says you can run it from CLI, but with specified rulesets:

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

In that example, you just get results for Java unused code rule and I'm trying to achieve something like:

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

and get results for all rules in Java rulesets.

Answer

Csuki picture Csuki · Apr 25, 2014

You can define a config file which includes the rulesets you wish to run. You can give this file as a parameter after the -R argument on the command line.

An example file is here (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>

The command line arguments would look like this:

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