MickKOZA74
MickKOZA74

Reputation: 51

Inject external parameters with Sonar-Runner

I have searched for a long time and I haven't find the answer yet. The following problem is : I want to pass external informations to my sonar runner like a config file for example.

My first question is to know if it is possible to do that ?

For example, I want to build a project and I want to pass in external parameters the data base credentials :

# Required metadata
sonar.projectKey=javascript_spine
sonar.projectName=Simple Spine js
sonar.projectVersion=1.1

# For example database parameters
sonar.dbUserName = "..."
sonar.dbPassword = "xxxx"
sonar.hostName = "ip"

It's how I get parameters in my plugin :

@Properties(@Property(key = "my.property", name = "My property", defaultValue = "20"))
public class MyJavaRulesPlugin extends SonarPlugin {

    private PrintWriter ecri;

    public MyJavaRulesPlugin(Settings settings) {

        if(settings != null) {
            String value = settings.getString("my.property"); // default value is 20
            System.out.println("La valeur de value est :" + value);
            // Ecrire dans un fichier texte sur le Desktop
            try {
                ecri = new PrintWriter(new FileWriter("/home/michael/test.txt"));
                ecri.print("La valeur de value est :" + value);
                ecri.flush();
                ecri.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

Upvotes: 1

Views: 341

Answers (1)

Simon Brandhof
Simon Brandhof

Reputation: 5136

As documented in http://docs.sonarqube.org/display/DEV/Coding+a+Plugin#CodingaPlugin-HowtoGetConfiguration, plugins can get property values through the Java component org.sonar.api.config.Settings.

Note that Settings should not be used from org.sonar.api.SonarPlugin instances (lifecycle reason, settings are initialized later), but from extensions (returned by SonarPlugin#getExtensions())

Upvotes: 1

Related Questions