SonarQube上未显示自定义PMD Java规则违规

时间:2015-11-11 18:06:36

标签: java sonarqube pmd sonarqube5.1

我试图在SonarQube上运行我的自定义PMD规则,但到目前为止,没有成功。

我创建了一个从sonar-pmd-plugin扩展的插件。在这个插件中,我有我的PMD规则集文件(custom_rules.xml),一个Sonar规则文件(pmd-extensions.xml)和我的自定义规则的Java类。

SonarQube识别我的规则,并且我已在默认质量配置文件中启用它们。最后,当我对给定项目运行声纳分析时,我发现我的自定义规则已正确执行,并且他们在正在分析的项目中发现违规行为。

但是,这些违规行为永远不会出现在SonarQube上的项目仪表板上。

SonarQube I使用的版本是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);
    }   
}

在对包含类&#34; AssertViolation.java&#34;的项目运行时,控制台上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] ------------------------------------------------------------------------

从控制台消息&#34;发现违规&#34;我可以看到规则已正确执行,但SonarQube仍然显示0个问题。

有什么想法? 感谢

0 个答案:

没有答案