Lombok

Lombok is a library that works with an annotation processor and will generate code for you to save you some time and reduce the amount of boilerplate code in your project. Lombok can generate getter and setter, equals methods, automate your logging variables for your classes, and more. Follow the list of all the features provided by Lombok to get an overview.

Lombok Dependency

To get access to the Lombok library just add the following dependency to the POM.xml.

The Lombok dependency:

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<version>1.18.20</version>
</dependency>

To get Lombok working with your current IDE you should also install the Lombok addon. Follow the Eclipse installation guide, there are also guides for other supported IDEs.

Lombok with Mapstruct

MapStruct takes advantage of generated getters, setters, and constructors from Lombok and uses them to generate the mapper implementations. Lombok is also an annotation processor and since version 1.18.14 both frameworks are working together. Just add the lombok-mapstruct-binding to your POM.xml.

The Lombok annotation processor and the lombok-mapstruct-binding

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok-mapstruct-binding</artifactId>
	<version>0.2.0</version>
</dependency>

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-compiler-plugin</artifactId>
	<version>3.8.1</version>
	<configuration>
		<source>1.8</source>
		<target>1.8</target>
		<annotationProcessorPaths>
			<path>
				<groupId>org.projectlombok</groupId>
				<artifactId>lombok</artifactId>
				<version>1.18.4</version>
			</path>
			<path>
				<groupId>org.projectlombok</groupId>
				<artifactId>lombok-mapstruct-binding</artifactId>
				<version>0.2.0</version>
			</path>
		</annotationProcessorPaths>
	</configuration>
</plugin>

In our quarkus reference project you can get a look into the usage of both frameworks.

Lombok Usage

Lombok can be used like any other annotation processor and will be shown in the simple example below to generate getter and setter for a Product Entity.

@Getter
@Setter
public class Product{

    private String title;
    private String description;
    private BigDecimal price;
}

For advanced Lombok usage follow the Baeldung Lombok guide or just read the Lombok javadoc

Last updated 2023-11-20 10:37:01 UTC