Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to exclude classes from pmd rule by regexp

Tags:

java

pmd

Is there a way to exclude classes from PMD rule by class name pattern?

I'm looking for sth like:

<rule ref="rulesets/java/coupling.xml/ExcessiveImports">
    <exclude-pattern>.*Test\.class</exclude-pattern>
</rule>

I'm aware of violationSuppressXPath approach but I find it too ugly/complex.

Thanks!

like image 991
Kragh Avatar asked Oct 13 '17 11:10

Kragh


People also ask

How do I get rid of PMD violations?

To do this you have to open the Problems View , select one of the PMD problems and use the Quick Fix entry from the context menu. This opens the Quick Fix Dialog where you can click on the button Select All to select all occurences of the PMD problem and finally click Finish to resolve all those PMD problems.

What are PMD violations?

PMD violations are assigned a priority from 1 (most severe) to 5 (least severe) according the the rule's priority. Violations at or less than this priority level are considered failures and will fail the build if failOnViolation=true and the count exceeds maxAllowedViolations .


1 Answers

Here's a rule that'll ignore a specific rule if the class name ends with DTO:

  <rule ref="category/java/bestpractices.xml/UnusedPrivateField">
    <properties>
      <!--Ignore UnusedPrivateField on classes where the class name ends with DTO-->
      <property name="violationSuppressXPath" value="//ClassOrInterfaceDeclaration['.*DTO']"/>
    </properties>
  </rule>

Ref https://github.com/pmd/pmd/issues/1142.

like image 83
L42 Avatar answered Sep 28 '22 04:09

L42