Chapter 26. The Sonar Plugin

The Sonar plugin provides integration with Sonar, a web-based platform for monitoring code quality. The plugin adds a sonar task to the project, which analyzes the project's source code and stores the results in Sonar's database.

The sonar task is a standalone task that needs to be executed explicitly. By default, it gets configured to analyze the Java code in the main source set. In a typical setup, the task would be run once per day on a build server.

Only projects which have the Java plugin applied (possibly by way of another plugin) are affected by the Sonar plugin. Other projects can still declare a task of type Sonar and configure it manually.

26.1. Usage

At a minimum, the Sonar plugin has to be applied to the project.

Example 26.1. Using the Sonar plugin

build.gradle

apply plugin: "sonar"

Typically, it is also necessary to configure connection settings for the Sonar server and database.

Example 26.2. Configuring connection settings

build.gradle

sonar {
    serverUrl = "http://my.server.com"

    globalProperty "sonar.jdbc.url", "jdbc:mysql://my.server.com/sonar"
    globalProperty "sonar.jdbc.driverClassName", "com.mysql.jdbc.Driver"
    globalProperty "sonar.jdbc.username", "myusername"
    globalProperty "sonar.jdbc.password", "mypassword"
}

For a complete documentation of all Sonar-specific configuration properties, see the Sonar documentation.

.

26.2. Tasks

The Sonar plugin adds the following tasks to the project.

Table 26.1. Sonar plugin - tasks

Task name Depends on Type Description
sonar - Sonar Analyzes the project's source code and stores results in Sonar's database.

26.3. Limitations

  • The projects of a multi-project build are currently analyzed independently. This means that no aggregated view will be available.