我想在我的 IDE (Eclipse) 中使用与我的 Sonar 配置文件相同的规则集。
我从 Sonar Permalinks 获得了 PMD XML 规则集,并希望将其导入我的 PMD Eclipse 插件,但是当我尝试这样做时,“确定”按钮被停用......
有人能帮我吗 ?
我想在我的 IDE (Eclipse) 中使用与我的 Sonar 配置文件相同的规则集。
我从 Sonar Permalinks 获得了 PMD XML 规则集,并希望将其导入我的 PMD Eclipse 插件,但是当我尝试这样做时,“确定”按钮被停用......
有人能帮我吗 ?
问题可能是 Sonar 正在以 v4.x 格式导出您的规则集,而您的 Eclipse 插件期望它们采用 v5.x 格式。
尝试更改您的规则:
<rule ref="rulesets/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
到
<rule ref="rulesets/java/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
请注意ref属性。一个简单的查找和替换所有将适合您。
Ivan Nikolov 的建议帮助了我,但我还必须更改一条规则:
<rule ref="rulesets/java/controversial.xml/UnusedModifier">
<priority>5</priority>
</rule>
到
<rule ref="rulesets/java/unusedcode.xml/UnusedModifier">
<priority>5</priority>
</rule>
这是我来自声纳的 PMD 配置文件,它适用于 PMD-plugin 4.0.2:
<?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="pmd-eclipse"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<rule ref="rulesets/java/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/ConstructorCallsOverridableMethod">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingNPE">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidRethrowingException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceEnumerationWithIterator">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/AvoidArrayLoops">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedFormalParameter">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/ClassCastExceptionWithToArray">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidThrowingNullPointerException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedPrivateField">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/CompareObjectsWithEquals">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseIndexOfChar">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BigIntegerInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/FinalFieldCouldBeStatic">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousEqualsMethodName">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/design.xml/InstantiationToGetClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousHashcodeMethodName">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/coupling.xml/LooseCoupling">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedLocalVariable">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UnnecessaryCaseChange">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/SingularField">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/CloseResource">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/AvoidCatchingThrowable">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/basic.xml/CollapsibleIfStatements">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UselessOperationOnImmutable">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneMethodMustImplementCloneable">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UselessOverridingMethod">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/unusedcode.xml/UnusedModifier">
<priority>5</priority>
</rule>
<rule ref="rulesets/java/design.xml/PreserveStackTrace">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArraysAsList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/EqualsNull">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BrokenNullCheck">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/logging-jakarta-commons.xml/UseCorrectExceptionLogging">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/InefficientStringBuffering">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/sunsecure.xml/ArrayIsStoredDirectly">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeOverloaded">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/controversial.xml/DontImportSun">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/imports.xml/DontImportJavaLang">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/design.xml/MissingStaticMethodInNonInstantiatableClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringBufferInstantiationWithChar">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/optimizations.xml/UseArrayListInsteadOfVector">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/StringToString">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/SimplifyConditional">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceVectorWithList">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssMethodCount">
<priority>3</priority>
<properties>
<property name="minimum" value="50" />
</properties>
</rule>
<rule ref="rulesets/java/logging-java.xml/AvoidPrintStackTrace">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/MethodWithSameNameAsEnclosingClass">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/naming.xml/SuspiciousConstantFieldName">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/codesize.xml/NcssTypeCount">
<priority>3</priority>
<properties>
<property name="minimum" value="800" />
</properties>
</rule>
<rule ref="rulesets/java/design.xml/AvoidInstanceofChecksInCatchClause">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/IntegerInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidAssertAsIdentifier">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/UnnecessaryLocalBeforeReturn">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/AvoidCallingFinalize">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/BooleanInstantiation">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/UnconditionalIfStatement">
<priority>2</priority>
</rule>
<rule ref="rulesets/java/clone.xml/CloneThrowsCloneNotSupportedException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/ReplaceHashtableWithMap">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/basic.xml/AvoidDecimalLiteralsInBigDecimalConstructor">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/SignatureDeclareThrowsException">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/EmptyFinalizer">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/design.xml/IdempotentOperations">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strictexception.xml/ExceptionAsFlowControl">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/finalizers.xml/FinalizeDoesNotCallSuperFinalize">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UseStringBufferLength">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/migrating.xml/AvoidEnumAsIdentifier">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/logging-java.xml/SystemPrintln">
<priority>3</priority>
</rule>
<rule ref="rulesets/java/strings.xml/UselessStringValueOf">
<priority>4</priority>
</rule>
<rule ref="rulesets/java/strings.xml/AvoidDuplicateLiterals">
<priority>3</priority>
</rule>
</ruleset>
我还必须将语言添加到我的 XPath 规则中,例如:
<rule name="DontUseDate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" ...></rule>
伊万尼科洛夫的回答是正确的。您的问题是不同版本的规则集格式。就我而言,我试图将从 PMD 插件版本 3.2.6 导出的规则集导入插件版本 4.0.0。以下是我的发现:
在这些更改之后,我能够将手动迁移的自定义规则集文件导入 PMD 插件 4.0.0。
希望这可以帮助。
顺便说一句:我发现 4.0.0 版本的 PMD 插件非常有问题,所以我降级回 3.2.6
除了别人说的,你可能还得找到这个:
net.sourceforge.pmd.rules.XPathRule
并将其替换为:
net.sourceforge.pmd.lang.rule.XPathRule
为什么不使用Sonar Eclipse?您无需担心规则集的同步。