2

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.

2
  • It might be better to declare source directories etc. on the Gradle side. In that case, the sonar-runner plugin will automatically use the correct values. Note that Gradle also supports bnd via its osgi plugin. Apr 8, 2013 at 7:35
  • The ide integration is easier with a separate file for specifying dependencies, whether it be an ivy, maven or bnd file (I am specifying my dependencies in the bnd file allowing me to use bndtools in Eclipse). The gradle osgi plugin I believe is similar to the bnd maven plugin - it only uses the manifest creation function of bnd.
    – Gareth
    Apr 11, 2013 at 22:52

3 Answers 3

10

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
3
  • For better or worse, it's a common pattern for a plugin to provide an extension and a task with the same name. Other examples are the IDE and code quality plugins. Apr 8, 2013 at 7:32
  • Thanks for the information! Marked up. It doesn't solve my problem though. If I add this in my subprojects block I get "Could not find property 'sonarRunner' on task set.". When I run it on my top-level project it finds the task, but doesn't do what I need (I really want it to run the specific init task in the subproject before setting the sonar properties for that subproject).
    – Gareth
    Apr 11, 2013 at 23:05
  • Sounds like you are trying to add the task dependency before you've applied the sonar-runner plugin. Usually it's too late to change configuration at task execution time, but since the sonarRunner block is evaluated lazily (as per the docs), it might work in this particular case. Apr 12, 2013 at 7:12
5

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'
2
  • I marked you up as you resolved the first part of my problem (why init was not found). Thanks! However it didn't solve the second part ("Could not find method for dependsOn()").
    – Gareth
    Apr 7, 2013 at 0:54
  • You answered my specific question so I will give it to you.
    – Gareth
    Apr 11, 2013 at 23:10
1

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;
            }
        }
    }
}
1
  • 2
    You can remove the afterEvaluate, as the sonarRunner block is evaluated lazily anyway (as per the docs). Apr 12, 2013 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.