I am using JaCoCo and it is considering methods generated by Lombok (generated in the bytecode, not trace of them in the source code). How can I configure JaCoCo to ignore them?
4 Answers
Also another way to exclude lombok generated classes since jacoco 0.8.0 and lombok 1.16.14.
Luckily, beginning with version 0.8.0, Jacoco can detect, identify and ignore Lombok-generated code. The only thing you as the developer have to do is to create a file named lombok.config
in your directory’s root and set the following flag:
lombok.addLombokGeneratedAnnotation = true
This adds the annotation lombok.@Generated
to the relevant methods, classes and fields. Jacoco is aware of this annotation and will ignore that annotated code.
Please keep in mind that you require at least version 0.8.0 of Jacoco and v1.16.14 of Lombok.
-
26I wish we could configure this in build.gradle and avoid having another file lingering in our repos. – k.schroeder31 Feb 16 '19 at 00:04
-
1A reminder that would had save me time, the code has to be generated again. In eclipse use project clean, in maven user mvn clean ... – MiguelSlv Mar 28 '23 at 10:34
Use the excludes tag provide by jacoco.
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.4.201502262128</version>
<configuration>
<excludes>
<exclude>**/config/**</exclude>
<exclude>**/model/**</exclude>
<exclude>**/item/**</exclude>
</excludes>
</configuration>

- 160
- 2
- 5
-
4This only works partially as those exclude filters only filter classes. I think you would exclude all generated code not only classes. Should work for `@Builder` but not for methods generated by `@Data` - still in discussion, see https://github.com/jacoco/jacoco/wiki/FilteringOptions. As lombok add `@Generated` to the generated code JaCoCo should filter that out but this is a missing feature. – Arne Burmeister Jun 23 '16 at 16:22
-
2@ArneBurmeister That is because `@Generated` has retention == SOURCE but JaCoCo analyzes only the bytecode, therefore missing that information. – namero999 Jul 07 '16 at 08:28
-
1@namero999 you are right, overseen that, but a annotation filter would be great as lombok adds its own `lombok.Generated` annotation (not that from `javax.annotation`) . – Arne Burmeister Jul 07 '16 at 15:55
-
2Jacoco can do this now, have a look: https://github.com/jacoco/jacoco/pull/513 (already merged into master) – Joergi May 11 '17 at 22:27
-
1Not only is there a Lombok specific solution now, this method can lead to classes that need testing not getting tested. – TastyWheat Oct 16 '20 at 15:02
As has already been answered, adding lombok.config
in the root directory of the project solves the problem, but if you're using Maven and want to avoid adding lombok.config
to your repository, you can use the Apache Maven AntRun Plugin in order to generate it automatically on build:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>lombok-config</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<propertyfile file="lombok.config">
<entry key="config.stopBubbling" value="true" />
<entry key="lombok.addLombokGeneratedAnnotation" value="true" />
</propertyfile>
</target>
</configuration>
<?m2e execute?> <!-- Optional: enable this execution in Eclipse -->
</execution>
</executions>
</plugin>
</plugins>
Don't forget to instruct your SCM to ignore this autogenerated file.

- 1,129
- 2
- 14
- 25
To fix this, we need a way to tell JaCoCo to ignore lombok generated code. This is possible by instructing lombok to annotate all the generated methods with @lombok.generated, which JaCoCo will ignore automatically.
Create a lombok.config file with the following:
# This tells lombok this directory is the root,
# no need to look somewhere else for java code.
config.stopBubbling = true
# This will add the @lombok.Generated annotation
# to all the code generated by Lombok,
# so it can be excluded from coverage by jacoco.
lombok.addLombokGeneratedAnnotation = true
And put it either at the root of your repo, or in the src/ folder. The only requirement is that all code with lombok annotations is found below or alongside this file.

- 11
- 2