Note! These instructions are for Salesforce Code Scanner 4.x. The rule configurations will change in 5.x
For running specific PMD Apex rules with Salesforce Code Scanner you need to provide an pmd config file where the rules are defined (pmd_ruleset.xml) and the engine list needs to include the pmd engine.
sf scanner run --target src --engine pmd --pmdconfig pmd_ruleset.xml
Each rule can be customized in the pmd config file. Read more about the rules here and how to customize them. You can even write your own rules, see example in Mitch Spanos excellent blog posts, Custom PMD Rules: Part 1 of 2 and Custom PMD Rules: Part 2 of 2
In the below example the ApexDoc rule have been excluded from the Documentation category (https://docs.pmd-code.org/latest/pmd_rules_apex_documentation.html)
<?xml version="1.0"?>
<ruleset name="Custom Rules"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>Custom APEX rules</description>
<rule ref="category/apex/bestpractices.xml">
</rule>
<rule ref="category/apex/codestyle.xml">
</rule>
<rule ref="category/apex/performance.xml">
</rule>
<rule ref="category/apex/errorprone.xml">
</rule>
<rule ref="category/apex/security.xml">
</rule>
<rule ref="category/apex/design.xml">
</rule>
<rule ref="category/apex/documentation.xml">
<exclude name="ApexDoc"/>
</rule>
</ruleset>
For running specific ESLint and ESLint LWC rules see Running specific ESLint rules with Salesforce Code Analyzer

Leave a comment