Project Lombok is a Java library that aims to reduce boilerplate code. By adding simple annotations to your code, Lombok automatically generates getters, setters, constructors, and more, making your code cleaner and more readable. This tutorial will walk you through the steps to set up Project Lombok using Maven and IntelliJ IDEA.
Before proceeding with the tutorial, you must have installed both Maven and the IDE on your device (the free community edition is also fine) which you can download from the links above.
Creating a new Lombok project with Maven
Fortunately, the latest versions of IntelliJ are already natively compatible with Project Lombok and do not require the use of external plugins. This is why we chose to use this IDE for our tutorial. If you prefer to use other tools such as Eclipse, the procedure is similar except that it will be necessary to install the appropriate plugin.
To create a new Maven project using IntelliJ you can follow the following steps:
- Select the menu item “File -> New -> Project”
- Select Maven as your dependency management system and click “Next”
- Give the project a name and select a local directory to save the files
Once the empty project has been created, the first thing to do is to import the Lombok dependency into the “pom.xml” file:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
Example of using Lombok
Once the project is initialized, we are ready to do our first test using Lombok. So let’s suppose we need to develop a program that manages colors according to the RGB scale. Each color will therefore have 3 numeric variables (int) which represent the shades of red, yellow and green.
Using Lombok it is possible to write the class very briefly as follows:
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class RgbColor {
private int red;
private int green;
private int blue;
}
We used two of the most common annotations from Lombok in the example:
- @AllArgsConstructor: automatically defines a constructor on the class that takes as input the values of all the variables contained within it.
- @Getter: Automatically defines all getter methods for class variables.
If we wanted to write the same class without using Lombok, the code would have been much more verbose and repetitive:
public class RgbColor {
private int red;
private int green;
private int blue;
public RgbColor(int r, int g, int b) {
this.red = r;
this.green = g;
this.blue = b;
}
public int getRed() {
return red;
}
public int getGreen() {
return green;
}
public int getBlue() {
return blue;
}
}
We can immediately appreciate some of the most important advantages of this library, namely the ability to make the code much more readable as well as speed up development. Regarding the second point, it must be said that some of the most popular IDEs, including IntelliJ, have features that automatically generate methods starting from the class variables, so the issue of speed is quite relative.
To test the code we can create a second class with an example main:
public class Main {
public static void main (String[] args) {
RgbColor color = new RgbColor(30, 22,55);
System.out.println("red: " + color.getRed());
System.out.println("gren: " + color.getGreen());
System.out.println("blue: " + color.getBlue());
}
}
Of course, we could use both the constructor and getter methods exactly as if we had implemented them the traditional way.
Compile-time operation
Lombok is a “particular” library because it does not work during the runtime of the application but during the compilation phase. In other words, it extends the operation of the Java compiler and injects the missing parts into the code before generating the bytecode which is then executed during program execution.
Precisely for this reason the Maven dependency was imported with “provided” scope. This means that the dependency is only needed when building and testing the project, but will not be included in the final application package (for example, the generated JAR or WAR file).
Project Lombok is an essential tool for any Java developer who wants to reduce boilerplate code and improve the readability of their code. With simple configuration and the use of annotations, Lombok can automate many repetitive tasks, allowing developers to focus on what really matters: the application logic.
If you haven’t tried Lombok yet, I encourage you to do so in your next Java project. It will not only simplify your code but also save you valuable time.