11

我想在我的 IDE (Eclipse) 中使用与我的 Sonar 配置文件相同的规则集。

我从 Sonar Permalinks 获得了 PMD XML 规则集,并希望将其导入我的 PMD Eclipse 插件,但是当我尝试这样做时,“确定”按钮被停用......

无法在 PMD Eclipse 插件中导入 PMD 规则集

有人能帮我吗 ?

4

6 回答 6

13

问题可能是 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属性。一个简单的查找和替换所有将适合您。

于 2013-06-05T15:12:55.790 回答
3

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>
于 2014-03-04T06:22:40.780 回答
1

我还必须将语言添加到我的 XPath 规则中,例如:

<rule name="DontUseDate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" ...></rule>
于 2016-05-04T08:53:37.110 回答
0

伊万尼科洛夫的回答是正确的。您的问题是不同版本的规则集格式。就我而言,我试图将从 PMD 插件版本 3.2.6 导出的规则集导入插件版本 4.0.0。以下是我的发现:

  • 所有规则都需要 Ivan Nikolov 提到的路径更改。要小心,因为并非所有规则都属于 java(我还看到了 XML 的规则,...)。我对 PMD 没有那么深入,所以我不知道在哪个版本中引入了其他技术的规则。
  • 导出 PMD 插件 4.0.0 的默认规则集以与旧的自定义规则集文件进行比较很有用。通过这种方式,您可以解决可以在下一点发现的问题。
  • 在您的行为期间引发了未可视化的异常。此异常可以在最后的工作区日志文件 (/.metadata/.log) 中看到。这可以给你一个线索,还有什么需要改变的。使用前一点了解如何解决问题。就我而言,它已将一条规则的路径从更改为

在这些更改之后,我能够将手动迁移的自定义规则集文件导入 PMD 插件 4.0.0。

希望这可以帮助。

顺便说一句:我发现 4.0.0 版本的 PMD 插件非常有问题,所以我降级回 3.2.6

于 2013-07-31T16:51:43.057 回答
0

除了别人说的,你可能还得找到这个:

net.sourceforge.pmd.rules.XPathRule

并将其替换为:

net.sourceforge.pmd.lang.rule.XPathRule
于 2014-07-21T10:32:15.793 回答
-1

为什么不使用Sonar Eclipse?您无需担心规则集的同步。

于 2013-04-08T12:04:15.523 回答