Federico Tomassetti
Federico Tomassetti

Reputation: 2248

JaCoCo: exclude generated methods (using it with Lombok)

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?

Upvotes: 70

Views: 53276

Answers (4)

Rahul Patel
Rahul Patel

Reputation: 11

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.

Upvotes: 1

Robert Hume
Robert Hume

Reputation: 1171

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.

Upvotes: 4

ebonnet
ebonnet

Reputation: 1250

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.

Upvotes: 114

Naidu
Naidu

Reputation: 170

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>

Upvotes: 7

Related Questions