![](/img/trans.png)
[英]ClassNotFoundException: Using custom java rule for PMD ruleset
[英]Custom PMD Java rule violations not showing on SonarQube
我正試圖在SonarQube上運行我的自定義PMD規則,但到目前為止,沒有成功。
我創建了一個從sonar-pmd-plugin擴展的插件。 在這個插件中,我有我的PMD規則集文件(custom_rules.xml),一個Sonar規則文件(pmd-extensions.xml)和我的自定義規則的Java類。
SonarQube識別我的規則,我已在默認質量配置文件中啟用它們。 最后,當我對給定項目運行聲納分析時,我發現我的自定義規則已正確執行,並且他們在正在分析的項目中發現違規。
但是,這些違規行為永遠不會顯示在SonarQube上的項目儀表板上。
我使用的SonarQube版本是5.1.1。 PMD插件的版本是2.4.1。 我為此問題創建了一個最小的示例,只有一個自定義規則。
custom_rules.xml:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="My custom rules" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<rule
language="java"
name="RuleJavaAssert"
message="Avoid assert in production"
class="br.gov.tcu.rules.RuleJavaAssert">
<description>Production code should not use the assert command</description>
<priority>3</priority>
</rule>
</ruleset>
PMD-extensions.xml:
<rules>
<rule>
<key>br.gov.tcu.rules.RuleJavaAssert</key>
<name>Avoid assert in production</name>
<category name="Maintainability" />
<priority>BLOCKER</priority>
<description>Production code should not use the assert command</description>
<configKey>br/gov/tcu/rules/custom_rules.xml/RuleJavaAssert</configKey>
</rule>
</rules>
RuleJavaAssert.java:
public class RuleJavaAssert extends AbstractJavaRule {
@Override
public Object visit(ASTAssertStatement node, Object data) {
System.err.println("Found violation");
addViolation(data, node);
return super.visit(node, data);
}
}
AssertViolation.java:
public class AssertViolation {
public static void testMethod() {
String test = "test";
assert(test != null);
}
}
當對包含“AssertViolation.java”類的項目運行時,控制台上SonarQube分析的輸出:
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building teste-pmd 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- sonar-maven-plugin:2.7.1:sonar (default-cli) @ teste-pmd ---
[INFO] User cache: D:\Users\x02315941199\.sonar\cache
[INFO] SonarQube version: 5.1.1
(...)
[INFO] [15:48:17.564] Sensor PmdSensor
[INFO] [15:48:17.564] Execute PMD 5.3.1...
[INFO] [15:48:17.580] Java version: 1.7
[INFO] [15:48:17.595] PMD configuration: D:\Users\x02315941199\Documents\PMD\workspace\teste-pmd\target\sonar\pmd.xml
Found violation
[INFO] [15:48:17.815] PMD configuration: D:\Users\x02315941199\Documents\PMD\workspace\teste-pmd\target\sonar\pmd-unit-tests.xml
[INFO] [15:48:17.815] Execute PMD 5.3.1 done: 251 ms
[INFO] [15:48:17.971] Sensor PmdSensor (done) | time=407ms
(...)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.185 s
[INFO] Finished at: 2015-11-11T15:48:19-02:00
[INFO] Final Memory: 72M/741M
[INFO] ------------------------------------------------------------------------
從控制台消息“發現違規”我可以看到該規則已正確執行,但仍然SonarQube指示0問題。
有什么想法嗎? 謝謝
SonarQube沒有顯示違規的原因是因為sonar-pmd-plugin中的違規記錄器通過其密鑰搜索規則。
因此, pmd-extensions.xml
文件中的key
屬性必須等於custom_rules.xml
規則的name
屬性
通過將pmd-extensions.xml內容更改為以下內容來修復提供的示例:
<rules>
<rule>
<key>RuleJavaAssert</key>
<name>Avoid assert in production</name>
<category name="Maintainability" />
<priority>BLOCKER</priority>
<description>Production code should not use the assert command</description>
<configKey>br/gov/tcu/rules/custom_rules.xml/RuleJavaAssert</configKey>
</rule>
</rules>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.