gpt4 book ai didi

java - 自定义 PMD 规则集不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:06:32 25 4
gpt4 key购买 nike

我创建了一个自定义 PMD 规则集 xml 文件来排除某些规则检查。

<?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>

我使用以下命令来运行具有此规则集的 PMD:

pmd -d C:\Users\leo\Documents\workspace\SampleProject\src\Sample.java -f html -R CustomRuleset.xml>Report.html

它运行良好,但排除 xml 中提到的规则不起作用。它也采用那些排除的规则。

我在这里做错了什么?

最佳答案

一切看起来都很好,除了您将一些规则集导入两次:)

例如rulesets/java/coupling.xml 导入一次:

<rule ref="rulesets/java/coupling.xml">
<exclude name="LawOfDemeter"/>
</rule>

稍后再次,没有排除:

<rule ref="rulesets/java/coupling.xml"/>

如果删除重复的规则集 rulesets/java/coupling.xmlrulesets/java/design.xmlrulesets/java/unnecessary.xml,问题就会得到解决。

PMD 只是从上到下读取您的自定义规则集,然后愚蠢地执行文件中的内容:从 Coupling.xml 导入除 LawOfDemeter 之外的所有规则,然后再次从 Coupling.xml 导入所有规则,这次包括 LawOfDemeter。

有趣的是,如果规则导入两次,PMD 不会出现问题。这个案子被默默地忽略了。但也许 PMD 应该引发错误? WDYT?

关于java - 自定义 PMD 规则集不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com