@codacy/tools-pmd-7
v0.21.0
Published
PMD 7 adapter — CLI-mode multi-language static analyzer
Readme
@codacy/tools-pmd-7
Table of Contents
Overview
Multi-language static analysis tool using the PMD CLI. Uses the CLI execution strategy -- spawns the PMD shell script via spawnTool() and parses its JSON output.
| Property | Value |
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Tool ID | PMD7 |
| Codacy UUID | ed7e8287-707d-485a-a0cb-e211004432c2 |
| Strategy | CLI |
| Languages | Java, Javascript, Kotlin, Swift, PLSQL, XML, Apex, Velocity, VisualForce, JSP |
| Binary | pmd (shell script from PMD distribution zip) |
| File patterns | **/*.java, **/*.js, **/*.jsx, **/*.kt, **/*.kts, **/*.swift, **/*.sql, **/*.pls, **/*.plb, **/*.pkb, **/*.pks, **/*.xml, **/*.pom, **/*.wsdl, **/*.xsl, **/*.cls, **/*.trigger, **/*.vm, **/*.page, **/*.component, **/*.jsp |
Updating patterns
# Re-fetch pattern metadata from the Codacy API
pnpm prefetch
# Commit the result
git add src/patterns.jsonPattern IDs follow the format PMD7_category_{lang}_{categoryDir}_{RuleName}, e.g. PMD7_category_java_errorprone_EmptyCatchBlock.
Updating the PMD version
- Update
PMD_VERSIONinsrc/adapter.ts - Update
PMD_DOWNLOAD_URLandPMD_EXTRACTED_DIRconstants accordingly - Run
pnpm prefetchto check for new/removed rules - Run
pnpm testto verify compatibility - If the major version changes, create a new adapter package (
pmd-8/)
Development
pnpm build # Build with tsup
pnpm test # Run tests (requires Java + PMD in PATH or installed)To install PMD locally for testing, download from https://github.com/pmd/pmd/releases and add bin/ to your PATH. Java 8+ is required.
Notes for maintainers
Preset mappings (
src/pattern-mappings.json): same shape as PMD 6, with one upstream difference that matters. PMD 7 renamed theJUnit*best-practice rules toUnitTest*to make them test-framework-agnostic, so those are claimed byjunit,junit-5andtestng, while the rules that are still JUnit-version-specific go to one key each.jakarta-eecovers the EJB rules, and the two Apache Commons rules use their own tags (lib:apache-commons-logging,lib:apache-commons-fileupload). That rename is exactly the driftpackages/runner/test/mapping-drift.test.tsexists to catch — a prefix copied from PMD 6 would match nothing here and fail silently.The test-framework keys gate on
lib:junit/lib:junit-5/lib:testngtags, not on prefixes, and that is deliberate: PMD renames this family more often than any other, so a prefix list is a standing drift liability. It has already happened twice. The first time was theJUnit*→UnitTest*rename above. The second wasJUnit5TestShouldBePackagePrivate→JUnitJupiterTestShouldBePackagePrivate, which silently killed thePMD7_category_java_bestpractices_JUnit5prefix and with it the wholejunit-5gate for that rule. The tags name the framework rather than the rule, so they survive both renames. Prefixes are still right forjakarta-ee, whose EJB rules carry no tag.Detection had to learn
spring-boot-starter-test(which pulls JUnit 5 transitively and is the only test declaration most Spring projects have), thejunit-jupiter-/junit-platform-per-role artifacts, and Gradle'stestRuntimeOnlyverb — without those, gating these default-on rules would have lost them for every Spring Boot repo.PMD requires Java 8+ as a runtime. The adapter uses
ensureJre()from@codacy/toolingto download Adoptium Temurin JDK 17 if no suitable Java is found on the system.The PMD distribution is downloaded as a zip from GitHub releases and extracted to
toolInstallDir/pmd-bin-{version}/.The
JAVA_HOMEenvironment variable is set when invoking PMD if using a non-system Java binary.PMD's JSON output format provides
beginline/begincolumn/endline/endcolumn, all 1-based. No offset conversion needed.PMD priority (1-5, 1=highest) maps to numeric confidence: 1-2 -> 3 (high), 3 -> 2 (medium), 4-5 -> 1 (low).
Pattern ID to rule reference mapping:
PMD7_category_java_errorprone_EmptyCatchBlock->category/java/errorprone.xml/EmptyCatchBlock.Config file detection supports
ruleset.xmlandapex-ruleset.xmlin the repository root.When patterns are specified, a generated ruleset XML is written to the generated config dir and passed via
-R.When no patterns or local config is specified, PMD runs with the built-in
rulesets/java/quickstart.xml.The
--no-fail-on-violationflag ensures PMD exits 0 even when violations are found.The
--file-listoption is used to pass target files via a temp file rather than command-line arguments, avoiding argument length limits.Processing errors (unparseable files) are mapped to
AnalysisErrorwith level "warning".Configuration errors (invalid rules) are mapped to
AnalysisErrorwith level "warning".
