How to integrate Sonar with Gradle

Recently, I have got a fruitful chance to work on an integration task of a multi module Gradle project with Sonar. This was the first time; I worked on Gradle and find it really cool with multi project build support, powerful dependency management system, and already enriched domain model. Before this, I generally worked on ant, maven build systems but I have find gradle a bit easy as you can easily build logic inside the Gradle task by doing simple coding- yes you read it right; no godzilla sized xmls, shell scripts….just groovy code that runs on JVM.

But again, the efficacy of Gradle compare to other available build tool options is not our intent to discuss here.

So let’s see in few simple steps-how to integrate Sonar with gradle based projects.

Before start, we need to confirm if the current gradle version (and related sonar plugin) installed in our system supports multi project build and project setting configuration, if not, it’s time to migrate to the latest version.

Now go to your parent build.gradle file and put below lines, the second line will apply the Sonar plugin and include all related tasks into your build system.

Third line is here to apply cobertura.gradle file, to generate code coverage. remember the parent build.gradle and cobertua.gradle should be placed at the same level.

Now let’s define the sonar specific configuration for each sub-project, add sonar URL and project related configuration detail under “project” as shown below. Unless Sonar is run locally and with default settings, it is also necessary to configure connection settings for the Sonar server and database.

We can see here- under project element, we are declaring cobertura report path in xml format, and this will be utilise by sonar to show code coverage.

So now we are pretty much done with the sonar integration with our multi project build system; but hey we have not created the code coverage files yet, just given the coverage file path. Here comes the choices, you can use some available open source plug-ins for coverage report generation or you can do generate with your own code as we are going to do in next few lines.

The groovy code(cobertura.gradle) given below is to instrument classes and create coverage reports. Reports in html and xml will be created at “build/reports”.

So finally we are done with the set-up, just run cobertura and sonar task defined in the files, once the coverage reports and sonar tasks are done, launch the URL given above and you are ready to analyse your project statistics🙂.

Feedback and Suggestions are welcome and please contact me if you have any difficulties or leave a comment below to help out other users.

Manu

Like this:

LikeLoading...

Related

1 Response to "How to integrate Sonar with Gradle"

Nice post.Useful information.Thanks for that.
I have a problem with integration of gradle with normal project.
I am using STS for my development.I have created a normal project and added build.gradle to that.I am not able to run it.STS is not identifying it as Gradel file.Can you please help me with this.