【问题标题】:Can't import PMD Ruleset in Eclipse无法在 Eclipse 中导入 PMD 规则集
【发布时间】:2013-04-05 08:45:41
【问题描述】:

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

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

有人可以帮我吗?

【问题讨论】:

    标签: import eclipse-plugin sonarqube rules pmd


    【解决方案1】:

    问题可能是 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 属性。一个简单的查找并替换所有内容对您来说效果很好。

    【讨论】:

    • 刚刚用 SonarQube 3.7 和 Eclipse Kepler 尝试过,但没有成功。这个答案有什么更新吗?
    • 另外,您可能还必须使用下面的 Dove 和 KrishPrabakar 答案。此外,以下内容也发生了变化 - UnusedPrivateField、UnusedLocalVariable、UnusedPrivateMethod、UnusedFormalParameter、UnusedModifier 的规则从争议性.xml 移至 usedcode.xml。规则 design.xml/UseSingleton 更改为 design.xml/UseUtilityClass
    【解决方案2】:

    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>
    

    【讨论】:

      【解决方案3】:

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

      <rule name="DontUseDate" language="java" class="net.sourceforge.pmd.lang.rule.XPathRule" ...></rule>
      

      【讨论】:

        【解决方案4】:

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

        net.sourceforge.pmd.rules.XPathRule
        

        并用这个替换它:

        net.sourceforge.pmd.lang.rule.XPathRule
        

        【讨论】:

          【解决方案5】:

          Ivan Nikolov 的回答是正确的。您的问题是不同版本的规则集格式。就我而言,我试图将从 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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

            • 但是使用 Sonar Eclipse 运行完整的测试需要很长时间。在这里我想使用 3 个单独的插件(PMD、CheckStyle、Findbugs),因为我可以在特定的包上运行它..(感谢您的帮助)
            • 好吧,好吧...很抱歉,我无法在这方面为您提供更多帮助,因为 PMD Eclipse 超出了我的范围。作为一个信息:我们正在努力改进 Sonar Eclipse 以便能够运行增量分析:)
            • 这将是一个很棒的功能!非常感谢您的帮助!在他的 IDE (Eclipse) 中直接使用 Sonar“警告”也很有趣。因为在 Sonar WebServer 上看到问题然后不得不在他的 IDE 中搜索相关的代码行真的很无聊...... :-)
            • Euuuh,你不是在说这个吗:docs.codehaus.org/display/SONAR/…
            • Sonar Eclipse 也有问题。分析时间太长,因为它们是针对整个项目执行的。拥有自动和增量分析真的很棒。你知道它是否已经在实施吗?关于何时可用的任何预测?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-08-25
            • 2018-02-06
            • 2011-08-09
            • 2013-02-14
            • 2012-11-03
            相关资源
            最近更新 更多