问题
I have created a customized PMD ruleset xml file to exclude some rule check.
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
name="full-pmd-ruleset"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>Full 5.1.1 PMD rule set</description>
<rule ref="rulesets/java/coupling.xml">
<exclude name="LawOfDemeter"/>
</rule>
<rule ref="rulesets/java/design.xml">
<exclude name="GodClass"/>
</rule>
<rule ref="rulesets/java/unnecessary.xml">
<exclude name="UnnecessaryFinalModifier"/>
<exclude name="UnnecessaryReturn"/>
</rule>
<rule ref="rulesets/java/android.xml"/>
<rule ref="rulesets/java/basic.xml"/>
<rule ref="rulesets/java/braces.xml"/>
<rule ref="rulesets/java/clone.xml"/>
<rule ref="rulesets/java/codesize.xml"/>
<rule ref="rulesets/java/comments.xml"/>
<rule ref="rulesets/java/controversial.xml"/>
<rule ref="rulesets/java/coupling.xml"/>
<rule ref="rulesets/java/design.xml"/>
<rule ref="rulesets/java/empty.xml"/>
<rule ref="rulesets/java/finalizers.xml"/>
<rule ref="rulesets/java/imports.xml"/>
<rule ref="rulesets/java/j2ee.xml"/>
<rule ref="rulesets/java/junit.xml"/>
<rule ref="rulesets/java/logging-jakarta-commons.xml"/>
<rule ref="rulesets/java/logging-java.xml"/>
<rule ref="rulesets/java/javabeans.xml"/>
<rule ref="rulesets/java/migrating.xml"/>
<rule ref="rulesets/java/naming.xml"/>
<rule ref="rulesets/java/optimizations.xml"/>
<rule ref="rulesets/java/sunsecure.xml"/>
<rule ref="rulesets/java/strictexception.xml"/>
<rule ref="rulesets/java/strings.xml"/>
<rule ref="rulesets/java/unnecessary.xml"/>
<rule ref="rulesets/java/basic.xml"/>
<rule ref="rulesets/java/unusedcode.xml"/>
<rule ref="rulesets/plsql/codesize.xml"/>
<rule ref="rulesets/plsql/TomKytesDespair.xml"/>
<rule ref="rulesets/plsql/dates.xml"/>
</ruleset>
And I am using the following command to run PMD with this ruleset:
pmd -d C:\Users\leo\Documents\workspace\SampleProject\src\Sample.java -f html -R CustomRuleset.xml>Report.html
It works well but exclusion of the rules that are mentioned in the xml are not working. It is taking those excluded rules as well.
What am I doing wrong here?
回答1:
All looks well, except that you are importing some rulesets twice :)
E.g. rulesets/java/coupling.xml
is imported once:
<rule ref="rulesets/java/coupling.xml"> <exclude name="LawOfDemeter"/> </rule>
And later again, without the excludes:
<rule ref="rulesets/java/coupling.xml"/>
If you remove the duplicated rulesets rulesets/java/coupling.xml
, rulesets/java/design.xml
, and rulesets/java/unnecessary.xml
it will fix the problem.
PMD is just reading your custom ruleset from top to down and stupidly executing, what's in the file: Import all rules from coupling.xml except for LawOfDemeter and later on import again all rules from coupling.xml, this time including LawOfDemeter.
Interestingly, PMD does not have a problem if the rules are imported twice. This case is silently ignored. But maybe PMD should raise an error instead? WDYT?
来源:https://stackoverflow.com/questions/40299075/custom-pmd-ruleset-not-working