Lombok
Lombok is a Java library that helps to reduce boilerplate code. It does this by providing annotations that can be used to generate code at compile time. This can help to reduce the amount of code that needs to be written and maintained.
Maven Dependency
To use Lombok in a Maven project, the following dependency needs to be added to the pom.xml
file:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>
Another important step that needs to be included is the build plugin must also be configured when using Java 9+ as described in the Lombok Setup Guide
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>17</release>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Code Coverage
Lombok is used to generate code such as constructors and getters, when unit testing the code these are challenging to test. This can be worked around by configuring Lombok to include generated annotations.
This configuration is done by adding a file src/lombok.config
with the following content:
# 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