Search code examples
gradlesonarqubesonar-runnergradle-custom-plugin

How do I set task properties in a Gradle Plugin


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?


Solution

  • 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'
            }
        }
    }