How do I set task properties in a Gradle Plugin

2k Views Asked by At

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?

2

There are 2 best solutions below

3
Rylander On BEST ANSWER

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'
        }
    }
}
0
Lucy On

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"
                }
    }
}