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):
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):
More detailed explanation can be found here: https://stackoverflow.com/a/36375145/5560215