Sonar is a complete platform to monitor code quality in a project. Sonar has a web-based dashboard where code quality can be monitored in due time, so we can see if our code has improved over time by using Sonar. Gradle has a Sonar plugin to work with Sonar.
This plugin requires Sonar 2.9 or higher. When we apply the plugin, a new task—sonarAnalyze
- is added to our project. This task is not a dependency task for the check
task, but is a standalone task. The task can analyze not only class files, but also test results, so we can make sure that the build
task is executed before the sonarAnalyze
task, to add a dependency on the build
task to the sonarAnalyze
task.
In the following example build file, we will apply the Sonar plugin, and if Sonar is running locally, we can simply execute the sonarAnalyze
task:
apply plugin: 'java' apply plugin: 'sonar' sonarAnalyze.dependsOn 'build'
If we run Sonar locally, we don't have to configure anything. Gradle will use the default settings to access the locally running Sonar server. Usually, a Sonar server runs on a remote machine. We can configure the Sonar plugin to use a different address and database settings with the sonar{}
script block. The script block accepts a configuration closure with several sections for the server and its database properties.
The following build file has different settings for the Sonar server URL and database properties:
apply plugin: 'java' apply plugin: 'sonar' sonarAnalyze.dependsOn 'build' sonar { server.url = 'http://sonar.company' database { url = 'jdbc:mysql://database.server/sonar' driverClassName = 'com.mysql.jdbc.Driver' username = 'sonar' password = 'sonar' } }
We can further customize the Sonar settings with a
project()
method and configuration closure. For example, we can change the directory where the Sonar client library files are stored after being downloaded from the Sonar server. We can define the location of the Clover
or Cobertura coverage XML result file and much more. The plugin already uses a lot of default values from project properties. For example, the version
and group
properties of a project are used to identify the project in Sonar. The following table shows all the properties we can set via the
project()
method configuration closure:
The following example build file shows several properties that we need to change:
apply plugin: 'java' apply plugin: 'sonar' version = '2.0-SNAPSHOT' group = 'gradle.sample' sonarAnalyze.dependsOn 'build' sonar { project { // Change directory to store Sonar client library files. bootstrapDir = file("${buildDir}/sonarClient") // Set Sonar profile to be used. profile = 'quality' // Set path to Cobertura results. coberturaReportPath = file("${reporting.baseDir}/cobertura/cobertura.xml") } }
We can add custom properties with the method
withGlobalProperties()
for properties that are global for Sonar, or the method withProjectProperties()
to define a property specific for a project. Both methods accept a closure as the parameter. A map of the properties is the argument of the closure. In the following build file, we will see how we can use this mechanism to further customize the Sonar plugin:
apply plugin: 'java' apply plugin: 'sonar' version = '2.0-SNAPSHOT' group = 'gradle.sample' sonarAnalyze.dependsOn 'build' sonar { withGlobalProperties { properties -> properties['sonar.verbose'] = true } project { withProjectProperties { projectProperties -> projectProperties['sonar.showSql'] = true } } }
18.220.111.87