Excluding Generated code from Jacoco using Gradle

898 views
Skip to first unread message

Jeff Bennett

unread,
Apr 30, 2023, 2:27:52 AM4/30/23
to JaCoCo and EclEmma Users
I am banging my head on what is seemingly a normal use-case, but find myself stuck and need to ask for advice.  In essence, within a Gradle build, I am trying to use a code-generator (openapi-generator) to create some code, but have that code ignored by Jacoco.  I have been studying the solutions proposed, but they seem to go around in a circle, and I cannot successfully break out.

The openapi-generator applies either javax.annotation.Generated or jakarta.annotation.Generated (possibly a few other alternatives), but all of these have Retention = SOURCE.  I have been unsuccessful in figuring out how to get openapi-generator to apply a custom annotation. There is an issue here proposing that change:  https://github.com/OpenAPITools/openapi-generator/issues/9752.   If the Jacoco maintainers feel strongly that a different @Generated should be allowed by code-gens, could you please comment thereupon.

In the Jacoco space, two options exist to ignore code.  The 'easy' answer is to apply a @Generated with RETENTION=(CLASS or RUNTIME), but (as above) that's not how the standard javax and jakarta annotations are configured.  As the specifications behind those implementations are already final, it is likely a large lift to get that changed therein.  It does not seem likely that javax.annotation.Generated (or jakarta) is likely to change.

The second option in Jacoco space is to use the 'afterEvaluate' hack (e.g. https://www.baeldung.com/jacoco-report-exclude#2-gradle-configuration).  As discussed in threads that led to the discovery of this approach (e.g. https://stackoverflow.com/questions/29887805/filter-jacoco-coverage-reports-with-gradle), this is thought to be the only way to get jacoco to ignore classes (which might still be regarded as a bug in jacoco, idk).   Nevertheless, this does not work for generated code because 'afterEvaluate' is called prior to the code being generated - the classes do not yet exist, and they cannot be removed (yet).   If there is a different way to make Jacoco ignore (generated-code) based on package (or class) name, I have not figured out how to do so.

Intuitively, I should be able to set "excludes = ['**/generated/**/*']" in my jacoco Rule, but this does not work.

Marc Mazas

unread,
Jul 31, 2023, 4:28:18 AM7/31/23
to JaCoCo and EclEmma Users
Hi Jeff
I believe it is possible: I use an ant script - and I assume you can do the same with Gradle
- I activate or not jacoco recording on java executions through the jvmarg enabled set to true or without the jvmarg

<jacoco:agent property="jacocoagentvmparam"

enabled="true"

destfile="target/site/jacoco/jacoco.exec"

classdumpdir="target/site/jacoco/dump"

append="true"

/>


<target name="run_tpt">

<java failonerror="true"

fork="true"

classname="..."

classpath="..."

>

<!-- jacocoagent vm arguments -->

<jvmarg value="${jacocoagentvmparam}" />

</java>


<java classname="${jtb_tool}" classpath="${jtb_ok_jar}" fork="true" failonerror="true">

<arg value="-e" />

<!-- input file -->

<arg value="..." />

<!-- no code coverage on current JTB version -->

</java>


- I filter unwanted classes on jacoco reporting :

<group name="Test and examples grammars">

<classfiles>

<fileset dir="${jacoco_dump_dir}/grammars">

<exclude name="**/*ControlSignatureProcessor*"/>

<exclude name="${jacoco_dump_dir}/grammars.x*" />

</fileset>

</classfiles>

<sourcefiles encoding="UTF-8">

<fileset dir="${jtb_proj_dir}/src/main/java" />

<fileset dir="${jtb_proj_dir}/target/generated-sources/jtb">

<exclude name="**/*ControlSignatureProcessor*"/>

</fileset>

<fileset dir="${jtb_proj_dir}/target/generated-tests/jj">

<exclude name="${jacoco_dump_dir}/grammars.x*" />

</fileset>

</sourcefiles>

</group>

Marc

Reply all
Reply to author
Forward
0 new messages