How to install PMD and CPD
Requirements
-
A zip archiver, e.g.:
pmd designer
) using OpenJDK or Java 11+, you need additionally JavaFX. Download it, extract it and set the environment variable JAVAFX_HOME pointing at that directory.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.
It’s highly recommended (but not required) to include it to your PATH
.
On Linux you can do this by adding PATH=$PATH:*path_to_pmd*/bin/
to your ~/.bashrc
/ ~/.zshrc
file.
On Windows this is achieved by:
- On the Start menu, right-click Computer.
- On the context menu, click Properties.
- In the System dialog box, click Advanced system settings.
- On the Advanced tab of the System Properties dialog box, click Environment Variables
- In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.
- Click the lower of the two Edit buttons in the dialog box.
- In the Edit System Variable dialog box, scroll to the end of the string in the Variable value box and add a semicolon (;).
- Add the proper value for
*path_to_pmd*/bin/
after the semicolon. - Click OK in three successive dialog boxes, and then close the System dialog box.
Shell completion
PMD ships with built-in completion support for Bash / Zsh.
To enable it, simply add source *path_to_pmd*/shell/pmd-completion.sh
to your ~/.bashrc
/ ~/.zshrc
file.
Running PMD via command line
pmd
(pmd.bat
under Windows), located inside the bin/
directory of the PMD distribution. The first argument is the name of the utility you want
to execute (‘check’, ‘designer’, …), e.g. PMD is launched via pmd check
. The rest of
the arguments are specific to the utility used.Running a PMD analysis (pmd check
or pmd.bat check
) requires at least one option and a list of 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
.<source> …
: path to the sources to analyse. This can be a file name, a directory, or a jar or zip file containing the sources. Alternatively You can use the-d
or--dir
flag, which is equivalent.
rulesets/java/basic.xml
) are deprecated,
though you can still use them. PMD includes a quickstart ruleset for some languages (currently, Java)
as base configurations, which you can reference as e.g. rulesets/java/quickstart.xml
. You’re strongly
encouraged to create your own ruleset from the start though.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 basictext
format (default) orxml
format. The supported formats are documented here.--aux-classpath <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:
Running CPD via command line
Like for PMD, CPD is started on Unix by pmd cpd
and on Windows by pmd.bat cpd
, and it requires one option and a list of sources:
--minimum-tokens <number>
: the minimum token length which should be reported as a duplicate.<source> …
: path to the sources to analyse. This can be a file name, a directory, or a jar or zip file containing the sources. Alternatively You can use the-d
or--dir
flag, which is equivalent.
Sample usage
The following shows a sample run of CPD with the text
format: