2013-04-05 43 views
11

Tôi muốn sử dụng cùng một Ruleset trong IDE (Eclipse) mà hồ sơ Sonar của tôi.Không thể nhập Bộ quy tắc PMD trong Eclipse

tôi đã ruleset XML PMD từ Permalinks Sonar và muốn nhập nó vào PMD Eclipse Plugin của tôi, nhưng khi tôi cố gắng để làm điều đó, nút "OK" được desactivated ...

Can't import a PMD Ruleset in PMD Eclipse Plugin

Ai đó có thể giúp tôi không?

Trả lời

13

Vấn đề có thể là Sonar đang xuất khẩu ruleset của bạn cho định dạng v4.x và plugin Eclipse của bạn mong đợi chúng ở định dạng v5.x.

Hãy thử thay đổi các quy tắc của từ:

<rule ref="rulesets/basic.xml/UnusedNullCheckInEquals"> 
    <priority>3</priority> 
</rule> 

để

<rule ref="rulesets/java/basic.xml/UnusedNullCheckInEquals"> 
    <priority>3</priority> 
</rule> 

Xin lưu ý ref thuộc tính. Một tìm kiếm đơn giản và thay thế tất cả sẽ làm việc tốt cho bạn.

+0

Chỉ cần thử với SonarQube 3.7 và Eclipse Kepler và nó không hoạt động. Mọi cập nhật về câu trả lời này? –

+0

Ngoài ra, bạn cũng có thể phải sử dụng câu trả lời Dove và KrishPrabakar bên dưới. Ngoài ra, những điều sau cũng đã thay đổi - Các quy tắc UnusedPrivateField, UnusedLocalVariable, UnusedPrivateMethod, UnusedFormalParameter, UnusedModifier được chuyển từ controversial.xml sang unusedcode.xml. Quy tắc design.xml/UseSingleton đã thay đổi thành design.xml/UseUtilityClass –

-1

Tại sao bạn không sử dụng Sonar Eclipse? Bạn sẽ không cần phải lo lắng về việc đồng bộ hóa các bộ quy tắc.

+1

Nhưng với Sonar Eclipse i t sẽ mất nhiều thời gian để chạy thử nghiệm hoàn thành. Ở đây tôi muốn sử dụng 3 plugin cá nhân (PMD, CheckStyle, Findbugs) vì tôi có thể chạy nó trên một gói cụ thể .. (Cảm ơn sự giúp đỡ của bạn) –

+1

Vâng, ok ... Xin lỗi, tôi không thể giúp bạn nhiều hơn về điều này như là PMD Eclipse nằm ngoài phạm vi của tôi. Cũng giống như một thông tin: chúng tôi đang làm việc chăm chỉ để cải thiện Sonar Eclipse để có thể chạy các phân tích gia tăng :) –

+1

Nó thực sự sẽ là một tính năng tuyệt vời! Cảm ơn bạn đã giúp đỡ! Nó cũng có thể là thú vị để có Sonar "Cảnh báo" trực tiếp trong IDE của mình (Eclipse). Bởi vì nó thực sự nhàm chán để xem một vấn đề trên Sonar WebServer và sau đó phải tìm kiếm các dòng có liên quan của mã trong IDE của mình ... :-) –

0

Câu trả lời của Ivan Nikolov là chính xác. Vấn đề của bạn là các phiên bản khác nhau của định dạng ruleset. Trong trường hợp của tôi, tôi đã cố gắng để nhập khẩu ruleset xuất khẩu từ PMD plugin phiên bản 3.2.6 vào phiên bản plugin 4.0.0. Dưới đây là những phát hiện của tôi:

  • Thay đổi đường dẫn mà Ivan Nikolov đề cập là cần thiết cho tất cả các quy tắc. Hãy cẩn thận vì không phải tất cả các quy tắc đều thuộc về java (tôi cũng thấy các quy tắc cho XML, ...). Tôi không phải là sâu vào PMD, vì vậy tôi không biết trong đó phiên bản là quy tắc cho các công nghệ khác được giới thiệu.
  • Rất hữu ích khi xuất các quy tắc mặc định của plugin PMD 4.0.0 để so sánh với tệp ruleset tùy chỉnh cũ. Bằng cách này bạn có thể khắc phục các vấn đề có thể được phát hiện tại điểm tiếp theo.
  • Trong khi hành vi của bạn bị ném ngoại lệ không được hiển thị. Ngoại lệ này có thể được nhìn thấy trong tệp nhật ký không gian làm việc của bạn (/.metadata/.log) ở cuối. Điều này có thể cung cấp cho bạn một đầu mối những gì khác cần phải được thay đổi. Sử dụng điểm trước đó để tìm hiểu cách khắc phục sự cố. Trong trường hợp của tôi nó đã thay đổi con đường của một quy tắc từ để

Sau những thay đổi này tôi đã có thể nhập bằng tay di cư tập tin tùy chỉnh ruleset vào PMD Plugin 4.0.0.

Hy vọng điều này sẽ hữu ích.

BTW: Tôi tìm 4.0.0 phiên bản của PMD cắm rất buggy vì vậy tôi hạ cấp trở lại 3.2.6

3

Lời khuyên từ Ivan Nikolov đã giúp tôi, nhưng tôi cũng đã thay đổi một quy tắc từ:

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

để

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

đây là tập tin cấu hình PMD của tôi từ sonar mà đang làm việc cho tôi với 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> 
+0

Điều đó đã làm cho tôi, cảm ơn rất nhiều. –

0

Ngoài những gì người khác nói, bạn có thể cũng phải tìm thấy điều này:

net.sourceforge.pmd.rules.XPathRule 

và thay thế nó bằng cách này:

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

tôi đã có thêm ngôn ngữ để (các) quy tắc XPath của tôi, ví dụ:

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