Main programmatic API of PMD. This is not a CLI entry point, see module
pmd-cli
for that.
Usage overviewCreate and configure a PMDConfiguration
, then use create(PMDConfiguration)
to obtain an instance. You can perform additional configuration on the instance, e.g. adding files to process, or additional rulesets and renderers. Then, call performAnalysis()
or one of the related terminal methods.
PMDConfiguration config = new PMDConfiguration();
config.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11"));
config.addInputPath(Path.of("src/main/java"));
config.prependClasspath("target/classes");
config.setMinimumPriority(RulePriority.HIGH);
config.addRuleSet("rulesets/java/quickstart.xml");
config.setReportFormat("xml");
config.setReportFile("target/pmd-report.xml");
try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
// note: don't use `config` once a PmdAnalysis has been created.
// optional: add more rulesets
pmd.addRuleSet(pmd.newRuleSetLoader().loadFromResource("custom-ruleset.xml"));
// optional: add more files
pmd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));
// optional: add more renderers
pmd.addRenderer(renderer);
pmd.performAnalysis();
}
Rendering reports
If you just want to render a report to a file like with the CLI, you should use a Renderer
. You can add a custom one with addRenderer(Renderer)
. You can add one of the builtin renderers from its ID using PMDConfiguration.setReportFormat(String)
.
If you want strongly typed access to violations and other analysis events, you can implement and register a GlobalAnalysisListener
with addListener(GlobalAnalysisListener)
. The listener needs to provide a new FileAnalysisListener
for each file, which will receive events from the analysis. The listener's lifecycle happens only once the analysis is started (performAnalysis()
).
If you want access to all events once the analysis ends instead of processing events as they go, you can obtain a Report
instance from performAnalysisAndCollectReport()
, or use Report.GlobalReportBuilderListener
manually. Keep in mind collecting a report is less memory-efficient than using a listener.
If you want to process events in batches, one per file, you can use Report.ReportBuilderListener
. to implement GlobalAnalysisListener.startFileAnalysis(TextFile)
.
Listeners can be used alongside renderers.
Specifying the Java classpathJava rules work better if you specify the path to the compiled classes of the analysed sources. See PMDConfiguration.prependAuxClasspath(String)
.
The analysis reports messages like meta warnings and errors through a PmdReporter
instance. To override how those messages are output, you can set it in AbstractConfiguration.setReporter(PmdReporter)
. By default, it forwards messages to SLF4J.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4