简体   繁体   English

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

[英]Custom PMD Java rule violations not showing on SonarQube

I'm trying to run my custom PMD rules on SonarQube but, so far, without success. 我正试图在SonarQube上运行我的自定义PMD规则,但到目前为止,没有成功。

I have created a plugin which extends from the sonar-pmd-plugin. 我创建了一个从sonar-pmd-plugin扩展的插件。 In this plugin I have my PMD ruleset file (custom_rules.xml), a Sonar rules file (pmd-extensions.xml) and the Java classes of my custom rules. 在这个插件中,我有我的PMD规则集文件(custom_rules.xml),一个Sonar规则文件(pmd-extensions.xml)和我的自定义规则的Java类。

SonarQube identifies my rules, and I have enabled them in my default quality profile. SonarQube识别我的规则,我已在默认质量配置文件中启用它们。 Finally, when I run the sonar analysis on a given project, I see that my custom rules are properly executed and that they find violations in the project under analysis. 最后,当我对给定项目运行声纳分析时,我发现我的自定义规则已正确执行,并且他们在正在分析的项目中发现违规。

However, these violations are never shown on the project dashboard on SonarQube. 但是,这些违规行为永远不会显示在SonarQube上的项目仪表板上。

The version of SonarQube I'm using is 5.1.1. 我使用的SonarQube版本是5.1.1。 The version of the PMD plugin is 2.4.1. PMD插件的版本是2.4.1。 I created a minimal example for this issue, with only one custom rule. 我为此问题创建了一个最小的示例,只有一个自定义规则。

custom_rules.xml: 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: 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: 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: AssertViolation.java:

public class AssertViolation {

    public static void testMethod() {
        String test = "test";
        assert(test != null);
    }   
}

The output of SonarQube analysis on the console, when run against a project which contains the class "AssertViolation.java": 当对包含“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] ------------------------------------------------------------------------

From the console message "Found violation" I can see that the rule was executed properly, but still SonarQube indicates 0 issues. 从控制台消息“发现违规”我可以看到该规则已正确执行,但仍然SonarQube指示0问题。

Any thoughts? 有什么想法吗? Thanks 谢谢

The reason SonarQube doesn't show the violations is because the violation recorder in the sonar-pmd-plugin searches for the rule by its key. SonarQube没有显示违规的原因是因为sonar-pmd-plugin中的违规记录器通过其密钥搜索规则。

Therefore, the key attribute in the pmd-extensions.xml file must equal the name attribute of the rule in custom_rules.xml 因此, pmd-extensions.xml文件中的key属性必须等于custom_rules.xml规则的name属性

The provided example would be fixed by changing the pmd-extensions.xml content to: 通过将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM