2

I am creating a gradle plugin to apply the sonar-runner plugin and default many of the values such as the sonar host URL and the sonar JDBC URL. I cannot figure out how to set the properties though.

When I set this up in build.gradle I use:

apply plugin: 'sonar-runner'

sonarRunner {
    sonarProperties {
        property 'sonar.host.url', 'http://mySonar.company.com'
        property 'sonar.jdbc.url', 'jdbc:mysql://127.0.0.1:1234/sonar'
    }
}

My gradle plugin looks like:

class MySonarPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.apply plugin: 'sonar-runner'
        project.configurations {
            sonarRunner {
                sonarProperties {
                    property 'sonar.host.url', 'http://mySonar.company.com'
                    property 'sonar.jdbc.url', 'jdbc:mysql://127.0.0.1:1234/sonar'
                }
            }
        }
    }
}

With this setup I get a No signature of method exception. How should I be setting these properties?

ben75
  • 29,217
  • 10
  • 88
  • 134
Rylander
  • 19,449
  • 25
  • 93
  • 144

2 Answers2

3

I discovered that I could use project.getExtensions().sonarRunner.sonarProperties{ ... } to set the sonar properties. See example below.

class MySonarPlugin implements Plugin<Project> {
    @Override
    void apply(Project project) {
        project.apply plugin:'sonar-runner'
        project.getExtensions().sonarRunner.sonarProperties {
            property 'sonar.host.url', 'http://mySonar.company.com'
            property 'sonar.jdbc.url', 'jdbc:mysql://127.0.0.1:1234/sonar'
        }
    }
}
Rylander
  • 19,449
  • 25
  • 93
  • 144
  • worked like a charm!!! I tried the following that did NOT work before running into this. ` //project.tasks['sonarqube'].setProperty('sonar.projectName', 'testplugindev') //project.tasks['sonarqube'].properties.putAt('sonar.projectName', 'testplugindev') //project.tasks['sonarqube'].properties.put('sonar.projectName', 'testplugindev') //project.task('sonarqube').properties.putIfAbsent("sonar.projectName", 'testplugindev')` – ravikanth Sep 05 '16 at 06:33
  • 1
    just realized; since I was using sonarqube scanner and not the runner; i have used `project.getExtensions().sonarqube.properties { property 'sonar.projectName', 'testplugindev' }` – ravikanth Sep 05 '16 at 15:27
  • The following source method addGradleDefaults in SonarQubePlugin.java is setting the defaults. https://github.com/SonarSource/sonar-scanner-gradle/blob/master/src/main/java/org/sonarqube/gradle/SonarQubePlugin.java – ravikanth Sep 05 '16 at 16:14
0

Thank you @mikerylander and @ravikanth! I also had tried the setProperty and .properties solutions but they didn't work for me.

The really tricky thing was that autocomplete did not find the "sonarqube" portion of project.getExtensions().sonarqube.properties for me so I never got to this solution without your post.

I wrote a custom Gradle plugin to run sonarqube for a multi-module Android project and your post helped me. Below is my full custom plugin. Since the plugin is designed to be included in the build.gradle of any submodule of my Android project I prepended "my_product" ${project.path} but of course you can use any values here.

Here is my complete plugin code in case its helpful:

package com.example.gradle.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class MySonarCodeCoveragePlugin implements Plugin<Project> {
    private Project project

    void apply(Project project) {
        this.project = project

        project.apply plugin: 'org.sonarqube'

        project.getExtensions().sonarqube.properties
                {
                    property "sonar.sources", "${project.projectDir}/src/main"
                    property "sonar.organization", "my_org"
                    property "sonar.projectKey", "my_product${project.path}"
                    property "sonar.projectName", "my_product${project.path}"
                    property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml"
                    property "sonar.scanner.metadataFilePath", "${project.buildDir}/sonar/report-task.txt"
                }
    }
}
Lucy
  • 436
  • 5
  • 8