Running PMD via command line

On Linux and other UNIX based operating system...

PMD comes with several command line utilities. Previously, each of them had its own start up script, but this has been greatly simplified since PMD 5.0... at least for Unix systems. There is now only one script, called "run.sh", inside the bin/ directory of PMD distribution.

The first argument is the name of the utility you want to execute ('pmd', 'designer',...) and the other arguments are specific to the utility used.

$ ./bin/run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/basic.xml  -version 1.7 -language java
.../src/main/java/net/sourceforge/pmd/RuleSet.java:123  These nested if statements could be combined
.../src/main/java/net/sourceforge/pmd/RuleSet.java:231  Useless parentheses.
.../src/main/java/net/sourceforge/pmd/RuleSet.java:232  Useless parentheses.
.../src/main/java/net/sourceforge/pmd/RuleSet.java:357  These nested if statements could be combined
.../src/main/java/net/sourceforge/pmd/RuleSetWriter.java:66     Avoid empty catch blocks
.../src/main/java/net/sourceforge/pmd/RuleSetWriter.java:269    Useless parentheses.
   

Basic usage for Windows

  • Windows: Type "pmd -d [filename|jar or zip file containing source code|directory] -f [report format] -R [ruleset file]", i.e:

  • C:\tmp\pmd-bin-5.1.2\pmd\bin>pmd -d c:\data\pmd\pmd\test-data\Unused1.java -f xml -R rulesets/java/unusedcode.xml
    <?xml version="1.0"?><pmd>
    <file name="c:\data\pmd\pmd\test-data\Unused1.java">
    <violation line="5" rule="UnusedLocalVariable">
    Avoid unused local variables such as 'fr'
    </violation>
    </file></pmd>
    
    C:\tmp\pmd-bin-5.1.2\pmd\bin>
    				

    You can pass a file name, a directory name, or a jar or zip file name containing Java source code to PMD.

    Also, the PMD binary distribution includes the ruleset files inside the jar file - even though the "rulesets/java/unusedcode.xml" parameter above looks like a filesystem reference, it's really being used by a getResourceAsStream() call to load it out of the PMD jar file. And the same applies to the example below.

  • If you want to run PMD without the batch file, you can do a:
  • C:\tmp\pmd-bin-5.1.2\pmd>java -Djava.ext.dirs=lib
                         net.sourceforge.pmd.PMD
                         -d c:\j2sdk1.4.1_01\src\java\lang
                         -f xml
                         -R rulesets/java/imports.xml
           

  • Linux: Type "./run.sh pmd -d [filename|jar or zip file containing source code|directory] -f [report format] -R [ruleset file]", i.e:

  • /home/user/tmp/pmd-bin-5.1.2/pmd/bin>./run.sh pmd -d /home/user/data/pmd/pmd/test-data/Unused1.java -f xml -R rulesets/java/unusedcode.xml
    <?xml version="1.0"?><pmd>
    <file name="/home/user/data/pmd/pmd/test-data/Unused1.java">
    <violation line="5" rule="UnusedLocalVariable">
    Avoid unused local variables such as 'fr'
    </violation>
    </file></pmd>
    
    /home/user/tmp/pmd-bin-5.1.2/pmd/bin>
    				

Other options

The tool comes with a rather extensive help text, simply running with --help !

Renderers

PMD comes with four different renderer types:

  • csv, provides a Comma Separated Values.
  • text, provides a basic text output.
  • xml, provides report output in XML format.
  • html, provides report as a basic HTML page.
  • ... and many more ! See the tool help text...