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.3.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.3.3/pmd/bin>
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.3.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.3.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.
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 |
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.
PMD comes with many different renderer types:
csv: Comma-separated values tabular format.
Properties:
emacs: GNU Emacs integration.
html: HTML format.
Properties:
ideaj: IntelliJ IDEA integration.
Properties:
summaryhtml: Summary HTML format.
Properties:
text: Text format.
textcolor: Text format, with color support (requires ANSI console support, e.g. xterm, rxvt, etc.).
Properties:
textpad: TextPad integration.
vbhtml: Vladimir Bossicard HTML format.
xml: XML format.
Properties:
xslt: XML with a XSL transformation applied.
Properties:
yahtml: Yet Another HTML format.
Properties: