2013-04-05 78 views
11

我想在我的Sonar配置文件的IDE(Eclipse)中使用相同的規則集。無法在Eclipse中導入PMD規則集

我從聲納永久鏈接的PMD的XML規則集,並希望將其導入到我的PMD Eclipse插件,但是當我嘗試這樣做,「確定」按鈕desactivated ...

Can't import a PMD Ruleset in PMD Eclipse Plugin

有人可以幫我嗎?

回答

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> 

請注意:裁判屬性。一個簡單的查找和替換都可以爲你工作。

+0

剛剛與SonarQube 3.7和Eclipse開普勒一起嘗試過,它沒有奏效。此答案的任何更新? – 2014-02-11 13:04:33

+0

此外,您可能還必須使用下面的Dove和KrishPrabakar答案。此外,以下內容也發生了變化 - 規則UnusedPrivateField,UnusedLocalVariable,UnusedPrivateMethod,UnusedFormalParameter,UnusedModifier從controversial.xml移至unusedcode.xml。規則design.xml/UseSingleton更改爲design.xml/UseUtilityClass – 2015-03-13 13:38:37

-1

爲什麼不使用Sonar Eclipse?您不需要擔心規則集的同步。

+1

但是與Sonar Eclipse i噸將花費很長時間來運行完成測試。在這裏,我想使用3個人插件(PMD,CheckStyle,Findbugs),因爲我可以在特定的軟件包上運行它。(感謝您的幫助) – 2013-04-19 08:58:51

+1

嗯,好的...很抱歉,我無法幫助您更多因爲PMD Eclipse不在我的範圍之內。就像一個信息:我們正在努力改進Sonar Eclipse,以便能夠運行增量分析:) – 2013-04-19 10:53:39

+1

這真的是一個很棒的功能!非常感謝您的幫助!在他的IDE(Eclipse)中直接安裝聲吶「警告」也很有趣。因爲在Sonar WebServer上看到一個問題,然後必須在他的IDE中搜索相關的代碼行,這真的很無聊...... :-) – 2013-04-19 13:48:28

0

Ivan Nikolov的回答是正確的。你的問題是規則集格式的不同版本。在我的情況下,我試圖將從PMD插件版本3.2.6導出的規則集導入插件版本4.0.0。這裏是我的發現:

  • Ivan Nikolov提到的路徑變化是所有規則都需要的。要小心,因爲並不是所有規則都屬於java(我也看到了XML的規則,...)。我對PMD並不那麼深入,所以我不知道在哪個版本中引入了其他技術的規則。
  • 爲了與舊的自定義規則集文件進行比較,導出PMD插件4.0.0的默認規則集非常有用。這樣你就可以解決下一個問題。
  • 在你的行爲被拋出的異常是不可視化的。該異常可以在工作區日誌文件(/.metadata/.log)中看到。這可以給你一個線索,還有什麼需要改變。使用上一點來了解如何解決問題。在我的情況下,它被改變路徑一個規則從 到

這些變化,我能夠導入手動遷移自定義的規則集文件到PMD插件4.0.0之後。

希望這會有所幫助。

BTW:我覺得版本4.0.0 PMD的插件非常錯誤,所以我降級到3.2.6

3

伊萬·尼科洛夫的建議幫助我,但我不得不改變也從一個規則:

<rule ref="rulesets/java/controversial.xml/UnusedModifier"> 
    <priority>5</priority> 
</rule> 

<rule ref="rulesets/java/unusedcode.xml/UnusedModifier"> 
    <priority>5</priority> 
</rule> 

下面是它爲我工作與PMD-插件4.0.2聲納我PMD的配置文件。:

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

這對我來說,非常感謝。 – 2014-03-13 13:03:23

0

除了別人怎麼說,你也必須找到這樣的:

net.sourceforge.pmd.rules.XPathRule 

並以此替換:

net.sourceforge.pmd.lang.rule.XPathRule 
1

我不得不把語言添加到我的XPath規則也是如此,例如:

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