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 1.7 or higher
-
A zip archiver, e.g.:
Note: For executing the Designer (./run.sh designer) using OpenJDK or Java 11, you need additionally OpenJFX. Download it, extract it and set the environment variable JAVAFX_HOME.
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
On Windows, each utility has its own startup script, e.g.
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
.
Note: At the moment the formerly provided rulesets (eg
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.-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.
Tip: A full CLI reference, including report formats, is available under PMD CLI Reference
Sample usage
The following shows a sample run of PMD with the text
format:
Running CPD via command line
Note: CPD supports Java, JSP, C, C++, C#, Fortran and PHP source code, among other languages.
For the full list, see Supported Languages.
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.
Tip: CPD’s command-line reference, Ant task usage, and many examples are documented in the
CPD documentation page
Sample usage
The following shows a sample run of CPD with the text
format: