0

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.

jonypera
  • 446
  • 2
  • 13
  • 23
  • To verify: is the plugin the rules are from loaded and running in your SQ instance? I.E. could you create this profile manually via the interface? Also, are these rules from a rule template? – G. Ann - SonarSource Team Mar 15 '16 at 13:04
  • I'm not sure if I understand the question but I have created the rules file from a database, via a "rule-crawler" component in Java. So I have a small Java program that is accessing a database, extracting the rules and writing the .xml file. I am following the template that appears to be correct from what I have seen online (example of which I posted above). Also, I would say I can create the profile as it is right now manually since it does not contain any rules; what is shown in the SonarQube panel is just an empty profile for an generic language name (for purposes of testing only). – jonypera Mar 15 '16 at 16:39

1 Answers1

0

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

Community
  • 1
  • 1
jonypera
  • 446
  • 2
  • 13
  • 23