Sonarqube add multiple test sources - gradle

I have my sonarqube configured the following way on a multi-module gradle project.
sonarqube {
properties {
property("sonar.projectKey", "key")
property("sonar.organization", "org")
property("sonar.host.url", "https://sonarcloud.io")
}
}
This configuration is on the root build.gradle.kts.
I now have a integration test folder that I added using the jvm-test-suite plugin and I want these sources to show up as well on Sonarqube.
I've added this config:
property("sonar.tests", "src/test, src/integrationTest")
But made no difference. They don't show up.
Any ideas? Thanks

Related

Gradle Idea plugin - issues with specifing test sources

I'm trying to create a custom source set and mark its contents in Intellij Idea as a Test Sources Root. I tried to use idea plugin and do it according to the gradle official website but it is not clear for me how it works.
First of all the documentation specifies the following configuration setup
idea {
module {
testSources.from(sourceSets["intTest"].java.srcDirs)
}
}
When I try to use it i receive Unresolved reference: testSources. Where is it coming from?
Then I tried to use:
idea {
module {
testSourceDirs = intTest.java.srcDirs
}
}
it works fine as long as I use only Java. After applying Kotlin plugin however, both kotlin and java + resources folder are again treated as Sources Root not Test Sources. To fix that I had to change from:
testSourceDirs = intTest.java.srcDirs
to:
testSourceDirs = intTest.kotlin.srcDirs
and now all folders are Test Source Root again. Since kotlin.srcDirs also includes java.srcDirs it looks like you have to specify all, otherwise it is ignored...
Now the real issue came when I used gradle-avro-plugin. Applying it made my folders marked as Sources Root again. I believe it is because it adds another avro directory, but just to main source set.
Does anyone know how to make it marked as Test Sources having both kotlin and avro plugin applied? Am I doing something wrong here? Beacause this beheviour seems to be buggy in the first place.
Tested with:
IntelliJ IDEA 2022.3.1 (Ultimate Edition)
Gradle 6.8.3 and 7.4.2
Plugin id("com.github.davidmc24.gradle.plugin.avro") version "1.5.0"
Plugin kotlin("jvm") version "1.7.0"

Does sonarcloud.io support groovy?

I cannot get SonarQube to scan my .groovy files on sonarcloud.io. I added the following to my build.gradle:
sonarqube {
properties {
property 'sonar.host.url', 'https://sonarcloud.io'
property 'sonar.organization', System.getenv('SONARQUBE_ORG')
property 'sonar.login', System.getenv('SONARQUBE_LOGIN')
property 'sonar.inclusions', '**/*.groovy,**/*.java'
}
}
It reports indexing 5 files, but there are 0 lines of code in sonarcloud.io.
My source is in the 'sonarqube' branch at here
My sonar cloud project is at here
Here is a project that is scanning groovy, but I can't find the source code to determine how it is configured: Link
SonarCloud does not support Groovy analysis.
The project you found is an old one, not analyzed for more than a year. It's a left-over from the former version of the service (called SonarQube.com at that time) that had some sort of support for Groovy.

sonarqube ignores sonar.junit.reportPaths

I have a gradle project which applies the sonarqube gradle plugin, version 2.6.
I run it against my team's sonarqube server, version 6.4 (build 25310).
According to documentation, new versions of sonarqube accept the property sonar.junit.reportPaths instead of sonar.junit.reportsPath.
My build runs 2 test tasks: test and integrationTest. Each test task outputs its xml into a different directory: build/test-results/test and build/test-results/integrationTest respectively.
I configured the sonarqube plugin to pick up both these directories:
project.sonarqube {
properties {
property 'sonar.junit.reportsPath', ['build/test-results/test',
'build/test-results/integrationTest']
// configure additional integration test properties that seem to be required
Collection<File> integrationTestSourceDirs = project.sourceSets.integrationTest.allJava.srcDirs.findAll { File dir -> dir.exists() }
properties['sonar.tests'] += integrationTestSourceDirs
Collection<File> integrationTestsClasses = project.sourceSets.integrationTest.output.classesDirs.files.findAll { File file -> file.exists() }
properties['sonar.java.test.binaries'] += integrationTestsClasses
}
}
This does not work. In sonarqube UI I only see unit tests (from the test directory) and don't see any integration tests.
I made sure that my integrationTest directory contains valid test reports, and it does.
It seems like sonarqube still uses the old parameter sonar.junit.reportsPath (which by default is assigned by the gradle plugin with the value build/test-results/test). I can tell this because if I remove this parameter I don't see any unit tests at all in the UI. This is how I removed the old parameter:
project.sonarqube {
properties {
properties.remove("sonar.junit.reportsPath")
}
}
As a workaround, I configured my integrationTest task to put its output into the same directory as unit tests: build/test-results/test. After doing this, all tests, including integration tests are picked up by sonarqube, and I can see them all in the UI.
However, I would prefer to keep outputs of different test tasks in different directories.
Is the described behavior intentional, or is it a bug?
Your SonarJava plugin is too old. The new property is only available from 4.11 on. In 4.10 only the old property is evaluated, so the new one is ignored. The Gradle plugin just sets the properties. The evaluation happens in the code that is downloaded from the SonarQube server and thus ignored.

Project with both java and javascript in sonarqube using gradle

We have a pretty standard web project using Java, which contains also some javascript code in the standard src/main/webapp folder. We are using Gradle 2.14 as our build tool.
We have just installed a brand new Sonarqube 6.0.1 on a fresh server, checked that both the Java and Javascript plugins are installed, and modified the build.gradle file as recommended on the Sonarqube documentation:
plugins {
id 'org.sonarqube' version '2.0.1'
}
sonarqube {
properties {
property 'sonar.projectName', 'Our-Project'
property 'sonar.projectKey', 'com.ourcompany:our-project'
}
}
This doesn't work as expected: the java code is analyzed correctly and we can browse the results on sonar, but the javascript code isn't analyzed.
What are we doing wrong? Thanks.
I have managed to fix our issues by adding this property inside the sonarqube block:
property 'sonar.sources', 'src/main'
Are you sure the sonar.sources is including the js files in the analysis? Can you add the module's gradle config? The file above looks like the app level gradle file. You should have at least one module for your app that also has a gradle file.
Android Studio Project Structure
You need to add the sources to the module's gradle file. Here's an example:
project(":app") {
sonarqube {
properties {
property "sonar.analysis.mode", "publish"
property "sonar.sources", "src/main/java"
property "sonar.java.binaries",
"build/intermediates/classes/debug"
property "sonar.junit.reportsPath",
"build/test-results/developDebug"
property "sonar.android.lint.report",
"build/outputs/lint-results-developDebug.xml"
property "sonar.test", "src/test/java"
}
}
}

Exclude packages from Jacoco report using Sonarrunner and Gradle

Is there a way to exclude packages from SonarQube(instrumented by gradle + sonar-runner) coverage reports(generated by jacoco) without excluding them completely from the project ?
Below is what i tried so far:
Version information
SonarQube 4.5.1
Gradle 2.2.
Jacoco configuration
// JaCoCo test coverage configuration
tasks.withType(Test) { task ->
jacoco {
append = false
// excluded classes from coverage defined in above configuration
excludes = excludedClasses()
}
jacocoTestReport {
doFirst {
classDirectories = fileTree(dir: "${buildDir}/classes/main/").exclude(excludedClasses())
}
}
}
Sonarrunner configuration
Property setting to exclude package from Sonar analysis. Adding this to my configuration lead to the situation that the packages do not show-up at all in Sonar.
property 'sonar.exclusions', excludedClasses().join(',')
Property setting to exclude packages from jacoco. Setting this leads to the situation that packages are excluded from coverage analysis however show up having 0% which accumulates to bad total scores.
property 'sonar.jacoco.exclusions', excludedClasses().join(',')
I have managed to exclude particular packages from coverage reports by using sonar.coverage.exclusions property in sonar-project.properties. Property is described in official documentation
To combine #Mikalai's answer and #pavel's comment into something that's a bit easier to copy and paste:
To exclude a package or class from all Sonar checks (coverage, code smells, bugs etc), add the following to build.gradle:
sonarqube {
properties {
property 'sonar.exclusions', "**/com/some/package/**"
}
}
To exclude a package or class from only Sonar code coverage checks, add the following to build.gradle:
sonarqube {
properties {
property 'sonar.coverage.exclusions', "**/com/some/package/**"
}
}
To exclude res, assets, custom packages and auto generated classes from sonar code coverage for android project.
Create Exclusion list like below
exclusionList = [
//Res and Assets
"src/main/res/**/*.*",
"src/main/assets/**/*.*",
//Auto-Generated
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/*Manifest.*',
'android/**/*.*',
'androidx/**/*.*',
// excluded packages
**/com/<your-package-path>/**/*]
Provide the exclusion list to sonar coverage property
property 'sonar.coverage.exclusions', exclusionList
NOTE:
Keep the full path to exclude the full directory
**/* includes all the files and sub directories under the parent directory Refer Here
*Bean includes all the class names contains "Bean".
**/ starting with it covers the parent directories for the particular package.
Run Jacoco command and check sonar portal's coverage section after doing above changes.
If you are using Gradle 5+ with Kotlin DSL, you can exclude the files from coverage like this:
// configure the SonarQube plugin
sonarqube {
val exclusions = listOf(
"**/com/some/package/**",
"**/all/files/under/package/*",
"**/com/some/package/OneClass.kt"
)
// exclude the directories only from coverage (but not from other analysis)
// https://docs.sonarqube.org/latest/analysis/scan/sonarscanner-for-gradle/
properties {
property("sonar.coverage.exclusions", exclusions)
}
}
exclude multiple module or class from Sonarqube. add below code to
build.gradle:
example :
package name= com.student.result.detail , com.customer.order ,com.student
sonarqube {
properties {
property 'sonar.exclusions', "**/com/student/result/details/**",
"**/com/customer/order/**",
"**/com/student/**";
}
}
This worked for me with gradle 6, sonarqube plugin 2.8
property "sonar.coverage.exclusions" , "**/com/some/package/entity/**/*," + "**/com/some/package/config/**/*,**/com/some/package/dto/**/*,**/com/some/package/dao/**/*"
NOTE: packages are comma separated but in a single string
Most of the answer I have seen here didn't work for me, resulting on the following error:
Execution failed for task ':sonarqube'.
> Could not find method property() for arguments [sonar.coverage.exclusions, **/package/path/Class1**, **/package/path/Class2**, **/package/path/Class3**, **/package/path/Class4**] on object of type org.sonarqube.gradle.SonarQubeProperties.
The reason is that sonar is asking for a string instead of several comma-separated strings as property value. So instead of:
sonarqube {
properties {
property 'sonar.exclusions', "**/com/student/result/details/**",
"**/com/customer/order/**",
"**/com/student/**";
}
}
The solution for me was:
sonarqube {
properties {
property 'sonar.exclusions', "**/com/student/result/details/**,"+
"**/com/customer/order/**,"+
"**/com/student/**";
}
}

Resources