In this guide we are going to see what is project Lombok, setting up with IDE eclipse / STS, a simple java Lombok maven example and project Lombok main features.
Project Lombok is a java library that plugs into your editor or IDE like eclipse, STS, IntelliJ etc, also plugs into build tools like maven, gradle, ant etc. Lombok library simply tells to the IDE , to generate all the boilerplate code for you silently. And this is done during compile time, so you usually don’t have to care about lombok being on your runtime path at all. The main aim of project Lombok is reduce boilerplate headaches out of the developer’s task list. It can be useful for making your code more concise, and speeding up development time.
Below image illustrates how java project Lombok works:
- Plugin Lombok java library to your IDE.
- Use Lombok annotations to write your POJO classes.
- Lombok will generates the boiler plate code (gettters, setters, toString(),…etc) automatically, which happens at compile time, so developer no need to write setters, getters etc code manually.
- If we’re using Eclipse IDE or Spring tool Suite (STS), we need to get the Lombok jar first. The latest version is located on Maven Central repository. For our example, we’re using lombok-1.18.10.jar.
- Double click on downloaded lombok.jar file.
- Installer UI open.
- Specify location your IDE Eclipse.exe / STS.exe path, click on Install/Update button.
If the installation is successful, we can exit the installer.
After installing the plugin, we need to restart the IDE and ensure that Lombok is correctly configured. We can check this in eclipse go to help option click About eclipse:
The last remaining part is to ensure that Lombok binaries are on the compiler classpath. Using Maven, we can add the dependency to the pom.xml.
You can annotate any field with
@Setter, to let lombok generate the default getter/setter automatically. The following example illustrates how to add the @Getter and/or @Setter Lombok annotations to the top of the class and how it reduce the boiler plate from sources file.
Lets have a look into plain Java POJO class that without using Lombok and POJO with the usage of Lombok, in below example technically both right hand side and left hand side code is equal, when we use Lombok in POJO class developer no need to write boiler plate code.