How do you override sourceSets in the Jacoco plugin (to exclude generated sources)?


(phraktle) #1

I’m trying to override the sourceSets used by the Jacoco plugin, so it does not automatically include my generated sources files – which are defined separately from the main java sources, as below:

sourceSets {

generated {

java {

srcDir ‘src-gen’

}

}

main {

java {

srcDir ‘src’

}

}

}

However, it seems no matter what you set jacocoTestReport. sourceDirectories to, it will always include all java sources (including the generated ones). The offending line seems to be:

https://github.com/gradle/gradle/blob/master/subprojects/jacoco/src/main/groovy/org/gradle/testing/jacoco/tasks/JacocoReport.groovy#L189

which explicitly adds sourceSet.allJava…

So how can I exclude the generated source directory from the coverage report?

Thanks,

Viktor


(René Groeschke) #2

Hello Viktor, How do you use the JacocoPlugin? In combination with the Java Plugin? In this case only the main sourceset should be configured to be included in the report. In the code snippet you’re pointing to “sourceSet.allJava” means all javacode IN the sourceset not all java sourcesets. your “generated” sourceset should not be included.

How do you try to override the sourceSets included in the JacocoReport task? Maybe you can provide a selfcontained example that demos your problem.

cheers, René


(phraktle) #3

Hi Rene,

Yes, I’m indeed using it along with the java plugin. The main java code depends on the generated code and both need to be distributed in the same package (i.e. jar). The skeleton of the build is something like this:

apply plugin: ‘java’

apply plugin: ‘jacoco’

task generateStuff {

// … into src-gen

}

sourceSets {

generated {

java {

srcDir ‘src-gen’

}

}

}

compileJava.dependsOn generateStuff

compileJava.source sourceSets.generated.java, sourceSets.main.java

test.finalizedBy jacocoTestReport

Thanks for looking into this,

Viktor


(René Groeschke) #4

I think the reason why you see the generated classes also in your jacoco report is, because you compile your generated sources together with your main sources. A side effect of this is, that your generated classes end up in the output directory of your main sourceSet which is inspected by the jacoco report task.

Instead you should compile your generated sourceSet seperately and add a classpath dependency to the main sourceSet. try this snippet below instead

sourceSets {
   generated {
       java {
           srcDirs = ['src-gen']
          }
      }
 main {
       compileClasspath =
compileClasspath + sourceSets.generated.output
     }
    }
  jar{
 from sourceSets.generated.allSource
}
  test.finalizedBy jacocoTestReport

(phraktle) #5

Hi Rene,

Thanks for the hint, with some additional changes I ended up with the following, which works (notably jar should reference the generated output, not allSource). It’s not quite as clean as I would like: had to add to testClasspath for tests to run, and have a duplicate dependency for generatedCompile and compile (since the generated code needs the protobuf library, and I also want the protobuf lib to be included in the installDist lib)… Any tips on making this nicer?

Thanks,

Viktor

apply plugin: ‘java’

apply plugin: ‘java-library-distribution’

apply plugin: ‘jacoco’

task proto {

// compile protos to src-gen

}

sourceSets {

generated {

java {

srcDir ‘src-gen’

}

}

}

compileGeneratedJava.dependsOn proto

compileJava.dependsOn compileGeneratedJava

clean.dependsOn cleanProto

test.finalizedBy jacocoTestReport

dependencies {

generatedCompile ‘com.google.protobuf:protobuf-java:2.5.0’

compile ‘com.google.protobuf:protobuf-java:2.5.0’

// compile … (further dependencies)

testCompile ‘junit:junit:4.11’

}

sourceSets {

main {

compileClasspath += generated.compileClasspath + sourceSets.generated.output

}

test {

compileClasspath += generated.compileClasspath + sourceSets.generated.output

runtimeClasspath += generated.runtimeClasspath

}

}

jar {

from sourceSets.generated.output

}