pmd
PMD is an extensible cross-language static code analyzer that finds common programming flaws and code quality issues. It focuses on detecting potential bugs, inefficient code patterns, and maintainability issues rather than just style violations.
Key Features:
- Multi-Language Support: Analyzes Java, JavaScript, Apex, Swift, Kotlin, and 16 other languages with 400+ built-in rules
- Advanced Bug Detection: Identifies unused variables, empty catch blocks, unnecessary object creation, and potential null pointer exceptions
- Code Quality Analysis: Detects complex code patterns, inefficient algorithms, and design principle violations
- Copy-Paste Detection: Includes CPD (Copy-Paste Detector) to find duplicated code across multiple languages
- AST-Based Analysis: Uses JavaCC and ANTLR parsers to analyze abstract syntax trees for deep code understanding
- Custom Rule Support: Extensible with custom rules written in Java or XPath queries
- Comprehensive Reporting: Supports multiple output formats including SARIF for modern toolchain integration
pmd documentation
- Version in MegaLinter: 7.15.0
- Visit Official Web Site
- See How to configure pmd rules
- If custom
java-pmd-ruleset.xml
config file isn't found, java-pmd-ruleset.xml will be used
- If custom
- See How to disable pmd rules in files
- See Index of problems detected by pmd
Configuration in MegaLinter
- Enable pmd by adding
JAVA_PMD
in ENABLE_LINTERS variable - Disable pmd by adding
JAVA_PMD
in DISABLE_LINTERS variable
Variable | Description | Default value |
---|---|---|
JAVA_PMD_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
JAVA_PMD_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
JAVA_PMD_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Include every file |
JAVA_PMD_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
JAVA_PMD_CLI_LINT_MODE | Override default CLI lint mode - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
list_of_files |
JAVA_PMD_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".java"] |
JAVA_PMD_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
JAVA_PMD_PRE_COMMANDS | List of bash commands to run before the linter | None |
JAVA_PMD_POST_COMMANDS | List of bash commands to run after the linter | None |
JAVA_PMD_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling JAVA_PMD and its pre/post commands | None |
JAVA_PMD_CONFIG_FILE | pmd configuration file nameUse LINTER_DEFAULT to let the linter find it |
java-pmd-ruleset.xml |
JAVA_PMD_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
JAVA_PMD_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
JAVA_PMD_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
JAVA_PMD_CLI_EXECUTABLE | Override CLI executable | ['/usr/bin/pmd/bin/pmd'] |
MegaLinter Flavors
This linter is available in the following flavors
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 126 | |
cupcake | MegaLinter for the most commonly used languages | 87 | ||
java | Optimized for JAVA based projects | 54 |
Behind the scenes
How are identified applicable files
- File extensions:
.java
How the linting is performed
- pmd is called once with the list of files as arguments (
list_of_files
CLI lint mode)
Example calls
pmd --rulesets java-pmd-ruleset.xml --dir myfile.java
pmd --rulesets java-pmd-ruleset.xml --file-list /tmp/list-off-files-generated-by-megalinter.txt
pmd --rulesets java-pmd-ruleset.xml --dir /path/to/sources
Help content
Usage: pmd [-hV] [COMMAND]
-h, --help Show this help message and exit.
-V, --version Print version information and exit.
Commands:
check The PMD standard source code analyzer
cpd Copy/Paste Detector - find duplicate code
designer The PMD visual rule designer
cpd-gui GUI for the Copy/Paste Detector
Warning: May not support the full CPD feature set
ast-dump Dumps the AST of parsing source code
generate-completion Generate bash/zsh completion script for pmd.
Exit Codes:
0 Successful analysis, no violations found
1 An unexpected error occurred during execution
2 Usage error, please refer to the command help
4 Successful analysis, at least 1 violation found
Installation on mega-linter Docker image
- Dockerfile commands :
# Parent descriptor install
ENV JAVA_HOME=/usr/lib/jvm/java-21-openjdk
ENV PATH="$JAVA_HOME/bin:${PATH}"
# Linter install
# renovate: datasource=github-tags depName=pmd/pmd extractVersion=^pmd_releases/(?<version>.*)$
ARG PMD_VERSION=7.15.0
RUN wget --quiet https://github.com/pmd/pmd/releases/download/pmd_releases%2F${PMD_VERSION}/pmd-dist-${PMD_VERSION}-bin.zip && \
unzip pmd-dist-${PMD_VERSION}-bin.zip || echo "Error unzipping" && \
rm pmd-dist-${PMD_VERSION}-bin.zip || echo "Error rm" && \
mv pmd-bin-${PMD_VERSION} /usr/bin/pmd || echo "Error mv" && \
chmod +x /usr/bin/pmd/bin/pmd || echo "Error chmod"