Sums up the first steps to set up a CLI installation and get started using PMD
Edit me

How to install PMD and CPD

Requirements

  • Java JRE, OpenJDK from Azul or AdoptOpenJDK 1.7 or higher.

    Note: For analyzing Apex, JavaScript, Scala or VisualForce or running the Designer at least Java 8 is required.

  • A zip archiver, e.g.:

Installation

PMD is distributed as a zip archive, which includes both PMD and CPD. You can download the latest binary distribution from the github releases page.

Unzip it into any directory, optionally add the bin subdirectory in your PATH, and you’re good to go!

Running PMD via command line

PMD comes with several command line utilities, like CPD, the rule designer or PMD itself. On Unix, you can run any of them using the script run.sh, located inside the bin/ directory of the PMD distribution. The first argument is the name of the utility you want to execute (‘pmd’, ‘designer’, …), e.g. PMD is launched via run.sh pmd. The rest of the arguments are specific to the utility used.

On Windows, each utility has its own startup script, e.g. pmd.bat, cpd.bat.

The PMD command (pmd.bat or run.sh pmd) requires two options:

  • -d <path>: path to the sources to analyse. This can be a file name, a directory, or a jar or zip file containing the sources.
  • -R <path>: the ruleset file you want to use. PMD uses xml configuration files, called rulesets, which specify which rules to execute on your sources. You can also run a single rule by referencing it using its category and name (more details here). For example, you can check for unnecessary modifiers on Java sources with -R category/java/codestyle.xml/UnnecessaryModifier.

Additionally, the following options, are specified most of the time even though they’re not required:

  • -f <format>: report format. PMD supports many report formats out of the box. You may want to start with the basic text format (default) or xml format. The supported formats are documented here.
  • -auxclasspath <classpath>: class path containing the compiled class files of the analysed Java sources, if any. Setting this up correctly allows PMD to do much deeper analysis using reflection. Some rules, such as MissingOverride, require it to function properly.

Sample usage

The following shows a sample run of PMD with the text format:

~ $ cd ~/bin/pmd-bin-7.0.0-SNAPSHOT/bin
~/.../bin $ ./run.sh pmd -d ../../../src/main/java/ -f text -R rulesets/java/quickstart.xml
  
  .../src/main/java/com/me/RuleSet.java:123  These nested if statements could be combined
  .../src/main/java/com/me/RuleSet.java:231  Useless parentheses.
  .../src/main/java/com/me/RuleSet.java:232  Useless parentheses.
  .../src/main/java/com/me/RuleSet.java:357  These nested if statements could be combined
  .../src/main/java/com/me/RuleSetWriter.java:66     Avoid empty catch blocks
C:\ > cd C:\pmd-bin-7.0.0-SNAPSHOT\bin
C:\...\bin > .\pmd.bat -d ..\..\src\main\java\ -f text -R rulesets/java/quickstart.xml
      
  .../src/main/java/com/me/RuleSet.java:123  These nested if statements could be combined
  .../src/main/java/com/me/RuleSet.java:231  Useless parentheses.
  .../src/main/java/com/me/RuleSet.java:232  Useless parentheses.
  .../src/main/java/com/me/RuleSet.java:357  These nested if statements could be combined
  .../src/main/java/com/me/RuleSetWriter.java:66     Avoid empty catch blocks

Running CPD via command line

Like for PMD, CPD is started on Unix by run.sh cpd and on Windows by cpd.bat.

There are two required parameters:

  • --files <path>: path to the sources to analyse. This can be a file name, a directory or a jar or zip file containing the sources.
  • --minimum-tokens <number>: the minimum token length which should be reported as a duplicate.

Sample usage

The following shows a sample run of CPD with the text format:

~ $ cd ~/bin/pmd-bin-7.0.0-SNAPSHOT/bin
~/.../bin $ ./run.sh cpd --minimum-tokens 100 --files /home/me/src

  Found a 7 line (110 tokens) duplication in the following files:
  Starting at line 579 of /home/me/src/test/java/foo/FooTypeTest.java
  Starting at line 586 of /home/me/src/test/java/foo/FooTypeTest.java

          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
C:\ > cd C:\pmd-bin-7.0.0-SNAPSHOT\bin
C:\...\bin > .\cpd.bat --minimum-tokens 100 --files c:\temp\src

  Found a 7 line (110 tokens) duplication in the following files:
  Starting at line 579 of c:\temp\src\test\java\foo\FooTypeTest.java
  Starting at line 586 of c:\temp\src\test\java\foo\FooTypeTest.java

          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());
          assertEquals(Boolean.TYPE, expressions.get(index++).getType());