Can you exclude the source file for a specific PMD rule?

When defining a PMD rule set, can you exclude the source file from a specific rule?

I want to do something like the following:

<rule ref=rulesets/java/logging-java.xml> <exclude name="Ignore.java" /> </rule> 

It seems that an exception is thrown only for rule names. Is there something similar for source files?

+6
source share
3 answers

Remember to write your own rule, which adds logic to the exception by file name.

I think the best approach in your scenario is to run PMD in two passes - one with a large set of rules against all code. And one with a smaller set of rules against the code you want to test.

This has the disadvantage of generating two reports. But this is a simpler problem than the one you created (or creating a custom rule). PMD can generate XML output. You can combine them programmatically and then call the PMD code to create an HTML report at the end. Or you can just have two reports and do it right away.

+4
source

PMD seems to only support exception files in the RuleSet level .

We have the same requirement that exclude files in the Rule level ..

Finally, we write the wrapper lab client and the bypass logic of the controller file ourselves to solve this problem.

But for a common PMD task. We can make the same rule together as a rule Set and exclude it at the ruleSet level.

UPDATE: we found that extends net.sourceforge.pmd.lang.rule.XPathRule , and adding an exclude file is simple. For the Java Code Rule we can use the same method to exclude the file in the rule definition.

+1
source

If you use the maven-pmd-plugin tool to run PMD, you can include a properties file that lists the classes and rules to ignore.

exclude-pmd.properties

 org.apache.maven.ClassA=UnusedPrivateField,EmptyCatchBlock org.apache.maven.ClassB=UnusedPrivateField,UnusedFormalParameter,UnusedPrivateMethod 

pom.xml

 <project> ... <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId> <version>3.8</version> <executions> <execution> <goals> <goal>check</goal> </goals> <configuration> <excludeFromFailureFile>exclude-pmd.properties</excludeFromFailureFile> </configuration> </execution> <execution> <goals> <goal>cpd-check</goal> </goals> <configuration> <excludeFromFailureFile>exclude-cpd.properties</excludeFromFailureFile> </configuration> </execution> </executions> </plugin> </plugins> </build> ... </project> 

More details: https://maven.apache.org/plugins/maven-pmd-plugin/examples/violation-exclusions.html

+1
source

Source: https://habr.com/ru/post/921275/


All Articles