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.

  1. What is Project Lombok?

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:

  1. Plugin Lombok java library to your IDE.
  2. Use Lombok annotations to write your POJO classes.
  3.  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.

java lombok workflow

2. Lombok in Eclipse

  1. 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.
  2. Double click on downloaded lombok.jar file.
  3. Installer UI open.
  4. Specify location your IDE Eclipse.exe / STS.exe path, click on Install/Update button.

lombok eclipse installer ui

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:

lombok eclipse setup success

3. Adding Lombok to Maven

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.

Copy to Clipboard

4. A simple Java lombok example:

You can annotate any field with @Getter and/or @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.

A no Lombok java Pojo

Copy to Clipboard

A Lombok java Pojo

Copy to Clipboard

5. Demo Lombok Pojo class

Copy to Clipboard

6. Output results

Copy to Clipboard

References