I have configured Sonar for my project and it is working fine, but I would like to create a custom task in the Gradle file to generate reports and run sonar locally directly.
For that, I have tried to create this custom task:
task sonarLocalReport(dependsOn: ['jacocoTestReport', 'lint']) {
group = "Reporting"
description = "Generate Sonar reports for local check"
doLast {
project.extensions.getByName("sonar").ext.extraProperties = [
"sonar.host.url": "http://localhost:9000",
"sonar.login": "KEY"
]
}}
I tried to add extra properties to my sonar task:
sonar {
properties {
property "sonar.projectKey", "App-Android"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "android"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.sources", "${project.projectDir}/src/main/java"
property "sonar.tests", "${project.projectDir}/src/test/java"
property "sonar.junit.reportPaths", "${project.buildDir}/test-results/testDebugUnitTest"
property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml"
property "sonar.android.lint.reportPaths", "${project.buildDir}/reports/lint-results-debug.xml"
}
}
When I run my custom task sonarLocalReport, I get nothing new locally.
I donĀ“t know if this is not the way to add new properties to the sonar task or if I need to run sonar task after adding these new properties.
I found the solution and it will be something like this:
sonar {
properties {
property "sonar.projectKey", "App-Android"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "android"
...
}
task sonarLocal(type: GradleBuild) {
tasks = ['lint', 'jacocoTestReport']
sonarLocal.finalizedBy('sonar')
sonar {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.login", "YOUR_KEY_GENERATED"
}
}
}
Then you can run your task like ./gradlew app:sonarLocal and it will run after lint, jacocoTestReport and sonar, adding some properties to be able to send data to your local sonar server.
Related
I'm getting an Authorisation Error while executing "gradlew sonarqube"
I've added these properties in app level gradle
sonarqube {
properties {
property 'sonar.sourceEncoding', 'UTF-8'
property 'sonar.projectName', 'ToDoTest'
property 'sonar.projectKey','967dcfc8faf76abdbb560889d5087245faa69a5b'
property "sonar.language", "kotlin"
property "sonar.sources", "app/src/main/java"
property "sonar.binaries", "app/build"
property "sonar.java.binaries", "target/classes,app/build/tmp/kotlin-classes"
property "sonar.tests", "app/src/test/java, app/src/androidTest/java"
property "sonar.java.test.binaries", "app/build/intermediates/classes/debug"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "app/build/test-results/testDebugUnitTest"
property "sonar.android.lint.report", "app/build/reports/lint-results.xml"
property "sonar.jacoco.reportPath","app/build/jacoco/testDevDebugUnitTest.exec"
}
}
These I kept in gradle.properties
systemProp.sonar.host.url=https://sonarcloud.io
systemProp.sonar.login=967dcfc8faf76abdbb560889d5087245faa69a5b
Getting this exception in terminal;
* What went wrong:
Execution failed for task ':app:sonarqube'.
Not authorized. Please check the properties sonar.login and sonar.password.
The configuration you posted is missing the sonar.organization required property.
Btw, all the other properties in sonarqube { properties { ... } } in your posted example should not be necessary, the SonarQube plugin for Gradle should detect and include them automatically in the analysis.
I'm trying to do Sonar Setup with Jacoco for Kotlin to generate Code Coverage report but it's not showing any code coverage. While checking Sonar Console it showing following error. Anyone has faced this issue before, any suggestion what could be miss.
Meta info
plugin using sonarqube version "2.6.1"
gradleVersion = '3.0.1'
kotlinVersion = '1.2.21'
Sonarqube version = Version 6.7.1 (build 35068) - LGPL v3
Frustrating part is, my setup project generating blank code coverage report :(. PFA.
Edit : Please find project structure snap.
I'm adding sonar & Jacoco gradle file setup I'm using to generate sonar-matrix report.
Here is sonar.gradle file:
sonarqube {
properties {
property "sonar.projectKey", "jacoco.sonar.test"
property "sonar.projectName", "Sonar Jacoco Test"
property "sonar.projectVersion", "1.1"
property "sonar.java.source", "7"
property "sonar.android.lint.report", "build/outputs/lint-results.xml"
property "sonar.java.binaries", "build/tmp/kotlin-classes"
property "sonar.java.test.binaries", "build/intermediates/classes/test/,build/tmp/kotlin-classes/devDebugUnitTest"
property "sonar.tests","src/test/java"
property "sonar.sources","src/main/java"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPaths","build/jacoco/testDevDebugUnitTest.exec"
property "sonar.junit.reportsPath","build/test-results/testDevDebugUnitTest"
}
}
and here is jacoco.gradle file
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.9"
reportsDir = file("${project.projectDir}/app/build/reports")
}
task jacocoTestReport(type: JacocoReport, dependsOn: "app:testDevDebugUnitTest") {
group = "Reporting"
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*$MembersInjector*.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*']
classDirectories = fileTree(
dir: "${project.projectDir}/app/build/intermediates/classes/dev",
excludes: fileFilter
) + fileTree(
dir: "${project.projectDir}/app/build/tmp/kotlin-classes/devDebug",
excludes: fileFilter
)
// sources
sourceDirectories = files(["${project.projectDir}/app/src/main/java"])
executionData = files("${project.projectDir}/app/build/jacoco/testDevDebugUnitTest.exec")
}
Following gradle commands I'm using to generate Jacobo report & then soar report.
./gradlew clean jacocoTestReport sonarqube
I observed following I'm getting, must be issue some path.
Coverage information was not collected. Perhaps you forget to include
debug information into compiled classes?
I'm sorry, if this is looking bit length; but this is best I found to summaries in one place. Please also note I tried similar setup with Java class instead Kotlin it's generating report with code coverage.
If you're using the android test orchestrator this is likely the problem.
I had the same issue today and after disabling the android test orchestrator the coverage is working again.
Bug report: https://issuetracker.google.com/issues/72758547
I'm not sure how Android Kotlin builds are configured, but in my Android Java build.gradle I had to comment out the test orchestrator like so:
android {
...
testOptions {
// temporarily disable the orchestrator as this breaks coverage: https://issuetracker.google.com/issues/72758547
//execution 'ANDROID_TEST_ORCHESTRATOR'
...
}
}
There are few things you need to do to make it work for kotlin.
Ensure your sonarqube > 7.5
If your sonarqube ver < 7.5, have the admin install sonar-jacoco plugin. Check the plugin compatibility version if sonarqube < 5.5.
plugins {
id "jacoco"
id "org.sonarqube" version "2.7.1"
}
Follow this url: https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151
Add properties in your build.gradle for it to search for jacoco results.
property 'sonar.coverage.jacoco.xmlReportPaths', "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
Follow this url for properties: https://docs.sonarqube.org/7.5/analysis/analysis-parameters/
I am integrating Sonar qube with gradle. I have mentioned plugin and required properties in app level build.gradle file.But command execution failed with exception. Please clarify what else required?
Build.gradle :
plugins {
id "org.sonarqube" version "2.6.1"
}
sonarqube {
properties {
property "sonar.host.url", "http://192.168.22.12:9000/"
property "sonar.projectName", "ourValues-Android"
property "sonar.projectKey", "1234567"
property "sonar.projectVersion","8.0"
property "sonar.sources","src/main/java"
property "sonar.exclusions","build/**,**/*.png"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.report.export.path", "sonar-report.json"
property "sonar.issuesReport.json.enable", "true"
property "sonar.import_unknown_files", true
property "sonar.android.lint.report", "./build/outputs/lint-
results.xml"
}
}
Exception is when running command gradlew sonarqube :
Execution failed for task ':app:sonarqube'.
You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
Please check the structure of project...
I am trying to configure our Android Gradle build to publish code coverage results (generated by Cobertura) to Sonar.
We have a modular project structure where each module is built as an apk-lib or apk. The project is structured like so:
build.gradle (top-level gradle build script)
settings.gradle (top-level gradle settings)
android_lib.gradle (used by module's build.gradle to tell it to build module as apk-lib)
android_app.gradle (as above but apk)
|
|__module1
|____build.gradle
|
|
|__module2
|____build.gradle
|
|
|__module3
|____build.gradle
|
|__etc...
I have had some luck generating the coverage using Cobertura. I can see the html and xml coverage reports in build/reports/cobertura/coverage.xml. This is how I've configured it:
android_lib.gradle:
apply plugin: 'com.android.library'
apply plugin: 'net.saliman.cobertura'
cobertura {
coverageFormats = ['html', 'xml']
coverageIgnoreTrivial = true
coverageIgnores = ['org.slf4j.Logger.*']
coverageReportDir = new File("$buildDir/reports/cobertura")
}
android {
... //android specific config goes here
}
After I run gradlew cobertura the coverage reports are generated and exist in build/reports/cobertura/coverage.xml
Now I would like to be able to publish these results to a local Sonar (v4.3) server I have running.
I have tried applying the sonar-runner plugin to my android_lib.gradle file but I receive lots and lots of errors and do not see any coverage results in Sonar.
android_lib.gradle:
apply plugin: 'sonar-runner'
sonarRunner {
sonarProperties {
//property "sonar.projectKey", "coverage-example"
//property "sonar.projectName", "Coverage Example"
//property "sonar.projectVersion", "1.0"
property "sonar.sources", "src/main/java"
property "sonar.binaries", "build"
//property "sonar.test", "src/androidTest/java"
property "sonar.profile", "Sonar way"
property "sonar.language", "java"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.junit.reportsPath", "build/test-results/debug"
property "sonar.cobertura.reportPath", "build/reports/cobertura/coverage.xml"
property "sonar.java.coveragePlugin", "cobertura"
property "sonar.host.url", "http://localhost:9000"
}
}
Is there something I am missing, has anyone had luck with this setup?
I am using:
Android Studio 1.2
SonarQube 5.1
...and I want to force the project build of Android app before passing SonarQube, maybe modifying gradle configuration file.
I have added the next lines to build.gradle file:
apply plugin: "sonar-runner"
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.analysis.mode", "incremental"
property 'sonar.sourceEncoding', 'UTF-8'
property 'sonar.language', 'java'
property "sonar.sources", "src/main"
property 'sonar.profile', 'Android Lint'
property 'sonar.import_unknown_files', 'true'
}
}
subprojects {
sonarRunner {
sonarProperties {
property "sonar.sources", "src/main"
}
}
}
What should I change in order to achieve the desired behaviour?
Simply add a new line to module build.gradle file, just after sonarRunner task declaration:
sonarRunner {
tasks.sonarRunner.dependsOn build // <- This new line
sonarProperties {