Gradle multi-project configuration for Jacoco coverage

I have a multiproject gradle java project. The structure is:

my-service
–my-service-src
–my-service-bb-test

The src sub-project has the all source code and unit test for the service and bb-test (black box test) has some Junit test which runs on the my-service-src project for black box testing of the service.

I want to measure the code coverage for my-service-src project with Junit running in same project and also in the my-service-bb-test as well. I am able to generate the 2 different Jacoco reports and exec file in each sub-project. But I am not able to create the 2 different measure at Sonarqube 7.9 server. I have tried many ways using the snoar.mudule properties. But no success, as every time there is only one project with no modules in that.
If there is any limitation with not to have any module in the snoarqube server, then is it possible to have 2 different project each for src and bb-test?

Thanks

Hi,

Welcome to the community!

Modules are still supported on the analysis side, but the server no longer makes a distinction for them except as directories. In the Code page you can see metrics for each directory and file, so that may help.

If that’s not what you want, then you may need to analyze your two sub-projects separately. Note that that could cause problems with accessing blame data, since the necessary SCM files/data may not be available from the module directories.

 
HTH,
Ann

Thanks for reply.

Can you please tell how it can be done? I have been trying to find the way but no success.

Hi,

I am able to analyze the both sub-project separately. But the issue is I am still not able get the coverage. Due to the fact that, my second sub-project “my-service-bb-test” does not have any main java code, it has only tests in form of junit for “my-service-src” sub-project. With this, Sonar cant produce the report. What property I can use in sonar to set the source for this sub-project as the source of other sub-project also?

my-service
build.gradle
–my-service-src
––build.gradle
––src
–––main (Actual code for service)
–––test (Unit tests in Junit for service)
–my-service-bb-test
––build.gradle
––src
–––test (Black box test in Junit for above service)

Thanks

Hi,

Okay, I realize I didn’t read well the first time. Of your two modules, only 1 contains code. You’re not going to be able to get different metrics for two different sets of tests on the same code. In the same project. Realistically, your choices are:

  • combine the two test reports and live with unified metrics
  • analyze twice, once as projectA, with source code and the first test report and again as projectB with source code and the second test report.

 
:woman_shrugging:
Ann

I have tried that. But I am not able to get the source of projectA in the projectB. These are my build.gradle files for all 3 projects.

build.gradle(my-service)

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url "https://clojars.org/repo/" }
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
    }
}
subprojects {
    
    apply plugin: 'jacoco'
	apply plugin: 'java'
	apply plugin: "org.sonarqube"
   
   // Commented this since sonar analysis is being done at subproject level
   /*sonarqube {
        properties {
            property "sonar.projectName", rootProject.name
            property "sonar.projectKey", "src:${rootProject.name}"
            property "sonar.branch.name", "master"

        }
    }*/	
}

build.gradle(my-service-src)

sonarqube {
    properties {
        property "sonar.projectName", rootProject.name
        property "sonar.projectKey", "src:${rootProject.name}"
		property "sonar.branch.name", "master"
   }
}
jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
    }

    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it, exclude: exclusions)
        })
    }
}

build.gradle(my-service-bb-test)

sonarqube {
    properties {
        property "sonar.projectName", project.name
        property "sonar.projectKey", "BB:${rootProject.name}"
        property "sonar.branch.name", "master"
		
		//How to refrer the source of my-project-src

    }
}

jacocoTestReport {
    reports {
        xml.enabled true
        html.enabled true
    }
	//evaluting source of my-project-src against the test in this module.
    afterEvaluate {
            additionalSourceDirs files(project(':my-project-src').sourceSets.main.java.srcDirs)
            additionalClassDirs files(project(':my-project-src').sourceSets.main.output)
    }
}

What should be properties I should set with values to include the source from my-service-src into the my-service-bb-test. I have provided the same value as provided in jacocoTestReport against the sonar.source. But it did not work.
Can you please help me with proper sonar properties and value?

Hi,

You’re probably going to have to do some scripting outside of Gradle to move the files to make this work. Oh, and you’ll need to run the tests only after you’ve moved the files around.

 
Ann

Thanks Ann. I will try.