Running PMD via command line

Basic usage

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

  • C:\tmp\pmd-5.0.0\pmd\bin>pmd c:\data\pmd\pmd\test-data\Unused1.java xml 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-5.0.0\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-5.0.0\pmd>java -Djava.ext.dirs=lib 
                         net.sourceforge.pmd.PMD
                         c:\j2sdk1.4.1_01\src\java\lang xml
                         rulesets/java/imports.xml
           

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

  • /home/user/tmp/pmd-5.0.0/pmd/bin>./run.sh pmd /home/user/data/pmd/pmd/test-data/Unused1.java xml 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-5.0.0/pmd/bin>
    				

Other options

  • Notice that in this case the output is in XML, so you can redirect it to a file and optionally transform it via XSLT
  • You can also use shortened names to refer to the built-in rulesets, like this:
  • java net.sourceforge.pmd.PMD /path/to/source text java-basic,java-imports,java-unusedcode
           			
  • The command line interface also accepts the following optional arguments:
    • -version - specify version of a language PMD should use (example: -version java 1.7 or -version cpp).
    • -debug - prints a stacktrace if an error is encountered
    • -shortnames - puts shortened names in the report. This only works if the filename argument is a single directory
    • -encoding Cp1252 - uses the specified encoding for reading the source code files
    • -excludemarker NOPMDFORME - uses the specified string of characters as the marker for PMD to ignore. The default string is "NOPMD".
    • -linkprefix - path to HTML source, for summary html renderer only.
    • -lineprefix - custom anchor to affected line in the source file, for summary html renderer only.
    • -minimumpriority - The rule priority threshold; rules with lower priority than they will not be used.
    • -xslt FILENAME - This option overrides the default XSL stylesheet used by nicehtml renderer.

Renderers

PMD comes with four different renderer types:

  • text, provides a basic text output.
  • xml, provides report output in XML format.
  • html, provides report as a basic HTML page.
  • nicehtml, provides an enhanced HTML output page. As this feature use an XSLT transformation, it also possible to override the default stylesheet using the -xslt parameter.

Here is a simple example of 'nicehtml' feature usage:

java net.sourceforge.pmd.PMD /path/to/source nicehtml java-basic,java-imports,java-unusedcode -xslt my-own-stylesheet.xsl
       			
Note that the stylesheet should be a readable file for PMD.