Gradle - jacoco doesn't generate coverage data


(Caesar) #1

I have muti-projects structure as follows.

root
– sub-project1
– sub-project2
– sub-project3

And I have some junit tests in the subprojects. I would like to generate jacoco report, but my build.gradle in my subproject does not generate coverage data.

Here is my build.gradle in one of the subprojects.

apply plugin: 'java’
apply plugin: "jacoco"
apply plugin: 'maven’
apply plugin: 'com.github.jacobono.jaxb’
apply plugin: “io.spring.dependency-management”

jaxb {
xsdDir = “subproject/xml/schema"
bindingsDir = “subproject/xml/schema"
bindings = [“bindings.xjb”]
System.setProperty(‘JaxbPackagingPlugin.jaxb.properties’, ‘true’)
xjc {
destinationDir = 'build/generated’
args = [”-extension”, “-no-header”, “-episode”, “${buildGenerated}/META-INF/sun-jaxb.episode”]
}
}

test
{
jacoco {
destinationFile = file("${projectDir}/build/jacoco/jacoco.exec")
classDumpFile = file("${projectDir}/build/classes/jacoco")
}

systemProperties ‘basedir’: projectDir
systemProperties ‘testdir’: "${projectDir}/src/test/java"
systemProperties ‘resourcedir’: "${projectDir}/src/test/resources"
systemProperties ‘in-memory’: "false"
systemProperties ‘derby.system.home’: ‘target/dbtest’

include ‘**/TestLocalDb
}

And I execute 'gradle sub-project1:test sub-project1:jacocoTestReport.
I want to generate the coverage data (.exec) first, but it doesn’t. Do I need to define something in the build.gradle in the root? Or is there something wrong with my command? I want to get one subproject working first and apply same thing to other subprojects.

I googled the solution and copied/pasted some samples for the past few dasy, but no luck.


(Sterling Greene) #2

I don’t see anything obviously missing. You shouldn’t need to configure the jacoco extension at all (the default is to put the execution data in build/jacoco/test.exec).

Are you actually running any tests?


(Caesar) #3

Yes. When I run it, it runs the tests. I see the html report and xml output file generated from junit. Actually, I just made it generate reports and jacoco coverage data like this.

build.gradle in root.

allprojects {
apply plugin: 'jacoco'

task jacocoMerge(type: JacocoMerge) {
    executionData = files(executionData.findAll({ it.exists() }))

   subprojects.each { subproject ->
      executionData subproject.tasks.withType(Test)
   }
}

tasks.withType(JacocoReport) {
    dependsOn jacocoMerge

    reports {
        xml.enabled false
        html.enabled true
        html.destination "${buildDir}/jacoco"
    }
    classDirectories = fileTree(
            dir: 'build/classes/main',
            excludes: ['**/R.class'])

    sourceDirectories = fileTree(
            dir: 'src/main/java')

	executionData = files("${buildDir}/jacoco/jacocoMerge.exec")
}

}

And I changed build.gradle in sub-project1 project as follows.

apply plugin: 'java'
apply plugin: "jacoco"
apply plugin: 'maven'
apply plugin: 'com.github.jacobono.jaxb'
apply plugin: "io.spring.dependency-management"

jaxb {
	    	xsdDir = "subproject/xml/schema"
		bindingsDir = "subproject/xml/schema"
		bindings = ["bindings.xjb"]
		System.setProperty('JaxbPackagingPlugin.jaxb.properties', 'true')
		xjc {
			destinationDir = 'build/generated'
			args = ["-extension", "-no-header", "-episode", "${buildGenerated}/META-INF/sun-jaxb.episode"]
		}
	}

	test
	{
		systemProperties 'basedir': projectDir
		systemProperties 'testdir': "${projectDir}/src/test/java"
		systemProperties 'resourcedir': "${projectDir}/src/test/resources"
		systemProperties 'in-memory': "false"
		systemProperties 'derby.system.home': 'target/dbtest'

		include '*/TestLocalDb*'
	}

	jacocoTestReport
	{
	        executionData = files("${buildDir}/jacoco/jacocoMerge.exec")
	}

It generates coverage data and reports, but it shows 0%. I see all the java class files compiled under build/classes/main and have all the java source files under src/main/java directory in my sub-projects

Anything wrong with my gradle files?

Thanks.