I'm trying to access a Property
in the class extending RulesDefinition
of my SonarQube plugin.
I define that Property in the class extending SonarPlugin
with:
@Properties(
@Property(key="sonar.root.path", name="Path to installation", description = "Root directory of the Master plugin installation directory")
)
the Property is properly created and I set it's value in SQ's configuration page, but somehow I can't access it from the class extending RulesDefinition
with this code in the overridden define(Context context)
method:
// Get access to the Properties
Settings settings = new Settings(new PropertyDefinitions(new MyPlugin()));
if(settings.hasKey("sonar.root.path")) {
// Never enters here
String path = settings.getString("sonar.root.path");
} else {
// If always returns false and enters here
LOG.info("No property defined with the provided key.");
}
// To double-check
LOG.info("The value: " + settings.getString("sonar.root.path")); // Returns null
LOG.info("Has default value: " + settings.hasDefaultValue("sonar.root.path"));
// Returns false, or true if I provide a default value, proving it can
// access the property - so the if condition above should have returned true
The strange thing is that I've checked the Property via the REST Web Service and can confirm that the values shown are the ones set in the web page, but if I provide a default value (like stated just above) the log shows the default value and not the value entered in the web page (and shown via the Web Service).
Maybe the problem is in the way I get the Settings
object. Would appreciate any help provided.
Thanks in advance.
The component Settings is instantiated by core and must be injected in your object through a constructor parameter:
public class YourRulesDefinition implements RulesDefinition {
private final Settings settings;
public YourRulesDefinition(Settings s) {
this.settings = s;
}
void yourMethod() {
if(settings.hasKey("sonar.root.path")) {
// ...
}
}
}
Note that you should never instantiated core classes. They are always available via constructor injection.