0

Does anyone maybe know how to disable some rules from Apex PMD, for example, I want to disable this one "ClassNamingConventions". I don't want to show me warning related to that rule in .csv file downloaded after running sfdx scanner? Thanks in advance!

1 Answer 1

2

If you want to exclude it from a particular class/method you can use the @SupressWarnings annotation. For your particular scenario it should be:

@SuppressWarnings('PMD.ClassNamingConventions')
public class Bar {
    void bar() {
        int foo;
    }
}

If you want to exclude the rule for all your codebase then you should:

  1. Create a PMD ruleset xml file;
  2. Add that rule as an exclusion on your pmd-ruleset.xml file. Something such as:
<rule ref="category/apex/codestyle.xml" >
    <exclude name="ClassNamingConventions"/>    
</rule>
  1. Run the sfdx:scanner command including the --pmdconfig flag to point out the location of PMD rule reference XML file.
4
  • And I suppose I need to create pmd-ruleset.xml file because I can not find it now. But where to put that file? And is there any other config I have to setup in order to PMD can read (use) that file?
    – mhdev
    Jul 27, 2021 at 13:07
  • Correct. Updated my original answer to include a hyperlink to the documentation regarding rulesets. Regarding the other config you have to setup is to include the --pmdconfig flag on the sfdx:scanner command to include the ruleset file locaton. Here for more details: forcedotcom.github.io/sfdx-scanner/en/scanner-commands/run Jul 27, 2021 at 13:09
  • I have one more problem, this is my rule set xml: <ruleset name="myruleset" 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>My ruleset</description> <rule ref="category/apex/codestyle.xml" > <exclude name="ClassNamingConventions"/> </rule> </ruleset> but now it exclude not only "ClassNamingConventions" but, for example, "ApexDoc", "CyclomaticComplexity" and lot of others.
    – mhdev
    Jul 27, 2021 at 14:57
  • @mhdev you need to define all the categories and/or individual rules which you want to include in the file as well. As an example: to include all the rules which belong to the BestPractices category you need to add to your ruleset <rule ref="category/apex/bestpractices.xml"/> You can see all the rules here: pmd.github.io/latest/pmd_rules_apex.html Jul 27, 2021 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.