SonarQube 不会在质量配置文件中显示导入的规则 [英] SonarQube does not show imported rules in quality profile

查看:98
本文介绍了SonarQube 不会在质量配置文件中显示导入的规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将规则(适用于 Java 和其他语言)从 .xml 文件导入 SonarQube.我使用了一个示例插件来做到这一点.该示例下载自 https://github.com/SonarSource/sonar-examples - 项目 声纳参考插件.确实创建了质量配置文件,但没有显示任何规则.(屏幕图像 SonarQube 个人资料页面)

这是 Override 方法,它应该使用 RulesDefinitionXmlLoader 类加载规则:

private void defineRulesForLanguage(Context context, String repositoryKey, String repositoryName, String languageKey) {NewRepository repository = context.createRepository(repositoryKey, languageKey).setName(repositoryName);InputStream rulesXml = this.getClass().getResourceAsStream("/resources/rules-TESTE.xml");如果(规则Xml != null){RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8.name());}存储库完成();}@覆盖公共无效定义(上下文上下文){String repositoryKey = FooLintRulesDefinition.getRepositoryKeyForLanguage(FooLanguage.KEY);String repositoryName = FooLintRulesDefinition.getRepositoryNameForLanguage(FooLanguage.KEY);defineRulesForLanguage(上下文,repositoryKey,repositoryName,FooLanguage.KEY);}

我已经尝试了包含规则的 .xml 文件的不同路径,以便智取任何可能的问题.那个 .xml 文件现在看起来像这样(现在它也很简单,只有一个规则可以使它更容易应用):

我使用的是本地 SonarQube 5.3,但我尝试了不同的版本但没有成功.提前致谢.

解决方案

原来规则没有显示在质量配置文件"(QP) 下的原因是因为在导入后,它们仍然没有与任何 QP 相关联,这是必须要做的.所以导入过程现在正常工作.一些建议:

  • 包含规则的 .xml 文件的强制性标签是(不正确的文件结构可能导致 SonarQube 在启动过程中失败):

    <规则><键></key><名称></名称><说明></描述></规则></规则>

  • 在我的例子中,包含规则的 .xml 文件的路径也引起了一些问题.正确的是(位于 src\main\resources 下):

    InputStream rulesXml = this.getClass().getResourceAsStream("/rules-TESTE.xml");

更详细的解释可以在这里找到:https://stackoverflow.com/a/36375145/5560215>

I'm trying to import rules (for Java and other languages) from a .xml file to SonarQube. I've used an exemple plugin to do so. That sample was downloaded from https://github.com/SonarSource/sonar-examples - project sonar-reference-plugin. The quality profile is indeed created but doesn't show any rules on it. (screen image SonarQube profile page)

This is the Override method where it supposedly would load the rules, by using the RulesDefinitionXmlLoader class:

private void defineRulesForLanguage(Context context, String repositoryKey, String repositoryName, String languageKey) {
     NewRepository repository = context.createRepository(repositoryKey, languageKey).setName(repositoryName);

    InputStream rulesXml = this.getClass().getResourceAsStream("/resources/rules-TESTE.xml");
    if (rulesXml != null) {
      RulesDefinitionXmlLoader rulesLoader = new RulesDefinitionXmlLoader();
      rulesLoader.load(repository, rulesXml, StandardCharsets.UTF_8.name());
    }

    repository.done();
  }

  @Override
  public void define(Context context) {
    String repositoryKey = FooLintRulesDefinition.getRepositoryKeyForLanguage(FooLanguage.KEY);
    String repositoryName = FooLintRulesDefinition.getRepositoryNameForLanguage(FooLanguage.KEY);
    defineRulesForLanguage(context, repositoryKey, repositoryName, FooLanguage.KEY);
  }

I have already tried different paths to the .xml file containing the rules in order to outwit any possible issues. That .xml file looks like this right now (also it is now very simple and has only one rule to make it easier to apply):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<rules>
  <rule>
    <key>da-rule-key</key>
    <name>Nome da regra</name>
  </rule>
</rules>

I'm using local SonarQube 5.3 but I've unsuccessfully tried different versions. Thanks in advance.

解决方案

Turns out the reason for the rules not being shown under "Quality Profiles" (QP) was because after being imported, they weren't still associated with any QP, which has to be done. So the import process is now working properly. A few piece of advice:

  • Mandatory tags for the .xml file containing the rules are (the incorrect file structure may cause SonarQube to fail during launch):

    <rules>
         <rule>
            <key> </key>
            <name> </name>
            <description> </description>    
        </rule>
    </rules>
    

  • Also in my case the path to the .xml file containing the rules caused some problems too. The correct is (by being located under src\main\resources):

    InputStream rulesXml = this.getClass().getResourceAsStream("/rules-TESTE.xml");
    

More detailed explanation can be found here: https://stackoverflow.com/a/36375145/5560215

这篇关于SonarQube 不会在质量配置文件中显示导入的规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆