Project Lombok Tutorial


In this tutorial, we will learn how Lombok is used to reduce the boilerplate code for model/data objects.

Project Lombok

Project Lombok is a Java library that automatically plugs into your editor and build tools and helps reduce the boilerplate code. Let’s see how Lombok project works first.

How does Java Project Lombok work?

Lombok has various annotations which can be used within our code that is being processed during the compile time and appropriate code expansion would take place based on the annotation used.

Lombok only does the code reduction in view time, after compiling the byte code is injected with all the boilerplate. This helps to keep our codebase small, clean and easy to read and maintain.

Project Lombok Maven

Follow below steps to setup Lombok in your Java projects:
  1. Create a simple maven project using - How to Create a Simple Maven Project in Eclipse article.
  2. Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.4</version>
    <scope>provided</scope>
</dependency>

Adding the Lombok Plugin in IDE (Eclipse)

Here are the installation steps to setup eclipse for Windows:
  • Downloaded jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
  • Execute command in terminal: java -jar lombok.jar
  • This command will open window as shown in the picture below, install and quit the installer and restart eclipse.

Project Lombok Articles

1. Project Lombok - @NonNull Annotation Example - In this post, we use @NonNull on the parameter of a method or constructor to have Lombok generate a null-check statement for us.

2. Project Lombok - Getter, Setter and Constructor Example - In this post, we will see how to reduce getter, setter and constructor boilerplate code using Lombok annotations.

3. Project Lombok - toString(), equals() and hashCode() Method Example - In this post, I will show you how to generate the toString()equals() and hashCode() automatically in Java projects using Project Lombok annotations - @ToString@EqualsAndHashCode.

4. Project Lombok - @Data Annotation Example - In this post, I will show you how to reduce boilerplate code for the below methods using @Data annotation.
  1. getters for all fields
  2. setters for all non-final fields,
  3. toString method
  4. equals and hashCode implementations that involve the fields of the class
  5. constructor that initializes all final fields
5. Project Lombok - Builder Pattern using @Builder Annotation - In this post, I will show you how to reduce the boilerplate code of complex builder APIs.

6. Project Lombok - Logging @Slf4j Annotation Example - In this post, we will learn how to do blogging using Lombok annotations.

7. Project Lombok - Automatic Resource Management using @Cleanup - In this post, We use Project Lombok provided a @Cleanup annotation to ensure a given resource is automatically cleaned up before the code execution path exits your current scope.

8. Project Lombok - @Synchronized Annotation Example - In this example, we will see how to use @Synchronized annotation to make Java methods synchronized.

GitHub Repository


You can view the source code of all these articles on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorial

The official Lombok website can be found here: https://projectlombok.org/

Comments