Using Sonarqube with Kotlin

The state of kotlin and sonarqube

There currently is no offical plugin for sonarqube to support kotlin. In the meantime, the detekt can be used to analyze kotlin projects. Please double check that this is still the case before continuing.

Getting a local Sonarqube running for testing

Setting up Sonarqube via docker with their official image and adding the detekt plugin. The lazy way without a dockerfile:

docker pull sonarqube:<version>
docker run -d --name sonarqube -p 9000:9000 -p 9092:9092 sonarqube:<verson>
docker exec -it sonarqube bash
wget https://github.com/arturbosch/sonar-kotlin/releases/download/0.4.2/sonar-kotlin-0.4.1.jar
chown sonarqube:sonarqube sonar-kotlin-0.4.1.jar
mv sonar-kotlin-0.4.1.jar /opt/sonarqube/extensions/plugins/
docker restart sonarqube

Setting up configuration via gradle

Added changes needed to root build.gradle file Specific notes: 1. This is a multiproject example, if you have just one project just remove the configure(subjects) wrapping 2. Get a copy of the detekt.yml by running ./gradlew detektGenerateConfig or by copying default-detekt-config.yml. 3. Jacoco is for code coverage

buildscript {
  ext.detektVersion = '1.0.0.RC7'
  dependencies {
    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2'
    classpath "gradle.plugin.io.gitlab.arturbosch.detekt:detekt-gradle-plugin:$detektVersion"
  }
}

apply plugin: 'org.sonarqube'
apply plugin: 'io.gitlab.arturbosch.detekt'

detekt {
  version = $detektVersion
  defaultProfile {
    input = "$projectDir"
    config = "$project.rootDir/detekt.yml" // if you'd like to override the default rules
  }
}

configure(subprojects) { project ->
  apply plugin: 'jacoco'
  jacoco {
    toolVersion = '0.7.5.201505241946'
    reportsDir = file("$buildDir/jacocoReports")
  }

  sonarqube {
    properties {
      property "detekt.sonar.kotlin.config.path", "$project.rootDir/detekt.yml"
      property "detekt.sonar.kotlin.filters", ".*/test/.*,.*/resources/.*,.*/build/.*,.*/target/.*"
    }
  }
}

Side notes

Detekt filtering, make sure this is correct or you’ll get 0 kotlin files were analyzed. Examples of what might be filtered for the following .*/test/.*,.*/resources/.*,.*/build/.*,.*/target/.*:

/build/project-name/src/main/kotlin/com/exmaple/App.kt
/project-name/build/lib/app.jar
/project-name/src/test/kotlin/com/example/AppTest.kt
/project-name/build/target/App.kt
/project-name/target/App.kt
/project-name/src/main/resources/App.kt