Search code examples
osgigradlesonarqubebndrunner

Make The Sonar Runner Gradle Task Depend On One Of My Tasks


I am trying out the new Sonar Runner task recently released in gradle 1.5. What I would like to do is be able to make the sonar runner task dependent on another task so that I can set the Sonar properties correctly for this project (i.e. sonar.sources, sonar.binaries, sonar.libraries, sonar.java.source, sonar.java.target).

Specifically I am using an osgi build tool called bnd which will provide these values when an ant init task is executed (note that whilst I include the default bnd build.xml file, my complete build is really being done using gradle).

I thought I would be able to customize the sonar runner task by doing this (this is a multi-module build):

subprojects {
    sonarRunner.dependsOn init
}

Eventually adding something like this (from what I understand of the bnd ant variables):

subprojects {
    sonarRunner {
        sonarProperties {
            property "sonar.java.source", ant.property["project.sourcepath"]
            property "sonar.java.target", ant.property["project.output"]
            property "sonar.sources", ant.property["project.allsourcepath"]
            property "sonar.libraries", ant.property["project.buildpath"]
        }
    }

    sonarRunner.dependsOn init
}

Unfortunately when I try to add the dependsOn I get the error:

* What went wrong:
A problem occurred evaluating root project 'myproject'.
> Could not find property 'init' on project ':com.company.myproject.mymodule'.

If I try to make sonarRunner depend on a gradle task I get the following error:

* What went wrong:
A problem occurred evaluating root project 'myproject'.
> Could not find method dependsOn() for arguments [task ':gradletask'] on org.gradle.api.sonar.runner.SonarRunnerExtension_Decorated@c4d7c0c.

Am I missing something obvious here? If someone could point me in the right direction it would be a big help.


Solution

  • The root project gradle file is evaluated before the child project gradle files, that means init does not exist on the location you try to address it.

    A workaround if you want to declare dependencies in the root project is to use afterEvaluate as described in http://www.gradle.org/docs/current/userguide/build_lifecycle.html, try:

    subprojects {
        afterEvaluate{
            sonarRunner.dependsOn init
        }
    }
    

    Another solution would be to add the dependency in the sub projects, directly or by applying another root gradle file.

    apply from: '../sonardependency.gradle'