Configure Lombok in eclipse

1. What is Project Lombok?

Project Lombok is a java library that automatically plugs into your editor and builds tools, does a lot of work for you to speed up your programming in Java.

It is a great library that can help you avoid writing boilerplate code and saves you precious time.

2. Advantages of using Project Lombok

  • Lombok saves you time and money.
  • Lombok can make your code cleaner.
  • It covers boilerplate code under the hood.
  • Lombok is opensource project, its free to use.
  • Lombok has stood the test of time.

You don't need to write another getter or equals and toString method again, if we add new fields in our POJO or DTO classes. With one annotation your class has a fully-featured builder.It covering boilerplate code under the hood.

In this article, I will show you how to setup lombok with your STS/eclipse based IDE

If you're using Lombok in your eclipse and pom.xml you not need to create a logging variable. Lombok takes care of it.

eg. before Lombok, you have to do logging like

 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example {

 private static final Logger logger = LoggerFactory.getLogger(Example.class);
  public static void main(String[] args) {
       logger.info("Hello World!!!");
    }
}
  

Now You need only do like

 
@Slf4j
public class Example {

  public static void main(String[] args) {
        logger.info("Hello World!!!"); // no need to create logger object
    }
}

@Slf4j annotation of Lombok does it for you.

3. Steps to install it in eclipse

1). Download Lombok Jar File
It's better to have a Maven/Gradle dependency in your application. The maven dependency can be found here:

2). Start Lombok Installation
Once the jar downloaded in Local repository, goto the jar location from the command prompt and run the following command java -jar lombok-1.18.12.jar or in the eclipse locate where eclipse.ini file located, put this jar there and double click on it and we should be greeted by Lombok installation window provided by Lombok like this.

3). Give Lombok Install Path Now click on the "Specify Location" button and locate the eclipse.exe/STS.exe path under the eclipse installation folder like this.

Configure Lombok in Eclipse and STS


Then,

Configure Lombok in Eclipse and STS


 

You have to select an eclipse.ini file.

4). Finish Lombok Installation
Now we need to finally install this by clicking the "Install/Update" button and we should finish installing Lombok in eclipse and we are ready to use it. The final screen will look like,


5). Please make sure to add below entry into the eclipse.ini or STS.ini file, if it's not already there.

 
-vmargs -javaagent:lombok.jar

or

-javaagent:/home/devuser/eclipse/lombok.jar 


6). Restart eclipse/sts


7). Clean your project

If you are using eclipse or sts, click on Project->clean.

After the successful installation of Lombok, you can use its a given example.

4. Add Dependencey in pom.xml

You have to add the following dependency in your project's pom.xml file.

 

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>



In the Spring Boot application by providing the following artifact.The Spring Boot by default takes the supported version.

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


Then create a class e.g Student.java

 

@Getter
@Setter
@ToString
class Student
{
private String firstName; 
private String lastName;
private String rollNumber
 
}

Alternatively you can use @Data instead of, using @Getter, @Setter and @ToString

 
@Data
class Student
{
private String firstName; 
private String lastName;
private String rollNumber
 
}

The code given above should be shown without errors in the IDE, correctly compiled and executed.

5. Conclusion

In this article, We have seen What is Lombok ,advantages of project lombok in java development and how to Configure Lombok in Eclipse and STS.