Using the Sonar plugin

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:

Property

Type

Default value

Description

baseDir

File

project.projectDir

Base directory to do analysis on.

binaryDirs

List<File>

sourceSets.main.output.classesDir

Directory with the compiled source code to analyzed.

cloverReportPath

File

null

Path to Clover XML report file.

coberturaReportPath

File

null

Path to Cobertura XML report file.

date

String

current date

Date of analysis with the format "yyyy-mm-dd".

description

String

project.description

Description of the project used in Sonar.

dynamicAnalysis

String

reuseReports

Dynamic analysis includes the analysis of test and coverage results. We can set the value to reuseReports, so reports from testReports, cloverReportPath, and coberturaReportPath are used for analysis. The value can be set to false so no dynamic analysis is performed, or true (which is not supported by Gradle) so that Sonar will produce the test and coverage reports.

importSource

boolean

true

Makes the source code available in the Sonar web interface.

java

SonarJavaSettings

 

Specific settings for Java source code, such as source compatibility.

key

String

project.group:project.name

Identifier of the project in Sonar.

language

String

java

Language that needs to be analyzed by Sonar. Only one language per project can be analyzed.

libraries

FileCollection

sourceSets.main.compileClasspath + Jvm.current().runtimeJar

Classpath with libraries that are used by the project.

name

String

project.name

Name of the project.

propertyProcessors

List<Closure>

empty

List of post-processors of Sonar properties. See also the methods withGlobalProperites() and withProjectProperties().

skip

boolean

false

Skip this project for analysis.

skipDesignAnalysis

boolean

false

Skip design analysis by Sonar.

sourceDirs

List<File>

sourceSets.main.allSource.srcDirs

Directories with source files to be analyzed by Sonar.

sourceEncoding

String

JVM's platform encoding

Character encoding of the project source files.

sourceExclusions

String

null

Pattern of source files to be excluded from analysis. The pattern can be an ANT matching pattern. For example, **/*Fixture.java.

testDirs

List<File>

sourceSets.test.allSource.srcDirs

Directories with test source code to be analyzed.

testReportPath

File

test.testResultsDir

Directory with the JUnit XML report.

version

String

project.version

Version of the project.

workDir

File

project.builDir/sonar

Working directory for analysis.

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
    }
  }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.111.87