Question

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.

Was it helpful?

Solution 2

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'

OTHER TIPS

Your problem with not being able to call dependsOn() on sonarRunner task comes from the fact that the plugin defines both both sonarRunner extension and a sonarRunner task. It looks like extensions take precedence over tasks when objects are resolved by name in a gradle build file, hence your stacktrace points out that you are trying to call dependsOn() on an instance of org.gradle.api.sonar.runner.SonarRunnerExtension_Decorated instead of caling it on a SonarRunner task instance.

I think that if you retrieved the task from the task container explicitly you should be ok:

tasks.sonarRunner.dependsOn init

If anyone is interested, this is one way of getting the bnd information to be set correctly in Sonar for each subproject (I am sure there are better ways):

subprojects {
    afterEvaluate {
        sonarRunner {
            sonarProperties {
                ant.taskdef(resource:"aQute/bnd/ant/taskdef.properties",
                   classpath: "../cnf/plugins/biz.aQute.bnd/biz.aQute.bnd-2.0.0.jar");
                def projectDir = project.rootDir.toString() + "/" + project.name;
                ant.bndprepare(basedir:projectDir,print:"false",top:null);

                def binaries = ant.properties['project.buildpath'].split(':') as ArrayList;
                binaries.remove(0);
                def binariesString = binaries.join(',');
                properties["sonar.java.source"] = ant.properties['javac.source'];
                properties["sonar.java.target"] = ant.properties['javac.target'];
                properties["sonar.binaries"] = ant.properties['project.output'].replace(':',',');
                properties["sonar.sources"] = ant.properties['project.sourcepath'].replace(':',',');
                properties["sonar.libraries"] = binariesString;
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top