Fork me on GitHub

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.

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.4.3/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.4.3/pmd/bin>

Basic usage for Windows

You can find PMD’s starter batch file pmd.bat in the bin subdirectory.

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.4.3\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.4.3\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.

Options

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

Option Description Required Applies for language
-rulesets / -R Comma separated list of ruleset names to use yes
-dir / -d Root directory for sources yes
-format / -f Report format type. Default format is `text`. no
-auxclasspath Specifies the classpath for libraries used by the source code. This is used by the type resolution. Alternatively a `file://` URL to a text file containing path elements on consecutive lines can be specified. no
-uri / -u Database URI for sources. If this is given, then you don't need to provide `-dir`. no plsql
-debug / -verbose / -D / -V Debug mode. Prints more log output. no
-help / -h / -H Display help on usage. no
-encoding / -e Specifies the character set encoding of the source code files PMD is reading (i.e. UTF-8). Default is `UTF-8`. no
-threads / -t Sets the number of threads used by PMD. Default is `1`. Set threads to '0' to disable multi-threading processing. no
-benchmark / -b Benchmark mode - output a benchmark report upon completion; defaults to System.err no
-stress / -S Performs a stress test. no
-shortnames Prints shortened filenames in the report. no
-showsuppressed Report should show suppressed rule violations. no
-suppressmarker Specifies the string that marks the line which PMD should ignore; default is `NOPMD`. no
-minimumpriority / -min Rule priority threshold; rules with lower priority than configured here won't be used. Default is `5` - which is the lowest priority. no
-property / -P `{name}={value}`: Define a property for a report format. no
-reportfile / -r Send report output to a file; default to System.out no
-version / -v Specify version of a language PMD should use. no
-language / -l Specify a language PMD should use. no
-failOnViolation {true|false} By default PMD exits with status 4 if violations are found. Disable this option with '-failOnViolation false' to exit with 0 instead and just write the report. no

Exit Status

Please note that if PMD detects any violations, it will exit with status 4 (since 5.3). This behavior has been introduced to ease PMD integration into scripts or hooks, such as SVN hooks.

0 Everything is fine, now violations found
1 Couldn't understand command line parameters or PMD exited with an exception
4 At least one violation has been detected unless '-failOnViolation false' is set.

Supported Languages

Available Report Formats / Renderers

PMD comes with many different renderer types:

  • codeclimate: Renderer for Code Climate JSON format.

  • csv: Comma-separated values tabular format.

    Properties:

    • problem: Include problem column. Default: true.
    • package: Include package column. Default: true.
    • file: Include file column. Default: true.
    • priority: Include priority column. Default: true.
    • line: Include line column. Default: true.
    • desc: Include description column. Default: true.
    • ruleSet: Include Rule set column. Default: true.
    • rule: Include Rule column. Default: true.
  • emacs: GNU Emacs integration.

  • html: HTML format.

    Properties:

    • linePrefix: Prefix for line number anchor in the source file.
    • linkPrefix: Path to HTML source.
  • ideaj: IntelliJ IDEA integration.

    Properties:

    • classAndMethodName: Class and method name, pass ‘.method’ when processing a directory.
    • sourcePath:
    • fileName:
  • summaryhtml: Summary HTML format.

    Properties:

    • linePrefix: Prefix for line number anchor in the source file.
    • linkPrefix: Path to HTML source.
  • text: Text format.

  • textcolor: Text format, with color support (requires ANSI console support, e.g. xterm, rxvt, etc.).

    Properties:

    • color: Enables colors with anything other than ‘false’ or ‘0’. Default: yes.
  • textpad: TextPad integration.

  • vbhtml: Vladimir Bossicard HTML format.

  • xml: XML format.

    Properties:

    • encoding: XML encoding format, defaults to UTF-8.
  • xslt: XML with a XSL transformation applied.

    Properties:

    • encoding: XML encoding format, defaults to UTF-8.
    • xsltFilename: The XSLT file name.
  • yahtml: Yet Another HTML format.

    Properties:

    • outputDir: Output directory.