Blog Image

Configuring Lombok in Eclipse  

Project Lombok is a java library that automatically plugs into your editor and builds tools, spicing up your java.
The Lombok tool is used for removing the boilerplate logic present in our classes. Which helps to ease the maintainability of our application.

Lombok is a tool and it used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those object automatically. For this Lombok has provided some annotations. All the required method which we need in our any model/Entity class that we can get using Lombok annotation so we no need to write it manually. Lombok also provides the features such as lazy loading, thread safety or immutability.

Annotations provided by Lombok:

  • @val
Finally! Hassle-free final local variables.
You can use val as the type of a local variable declaration instead of actually writing the type.

  • @var
Mutably! Hassle-free local variables.
var works exactly like val, except the local variable is not marked as final.

  • @NonNull
You can use @NonNull on the parameter of a method or constructor to have lombok generate a null-check statement for you.

  • @Cleanup
Automatic resource management: Call your close() methods safely with no hassle.
You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope.

  • @Getter/@Setter
You can annotate any field with @Getter and/or @Setter, to let lombok generate the default getter/setter automatically.

  • @ToString
Any class definition may be annotated with @ToString to let lombok generate an implementation of the toString() method. By default, it'll print your class name, along with each field, in order, separated by commas.

  • @EqualsAndHashCode
Any class definition may be annotated with @EqualsAndHashCode to let lombok generate implementations of the equals(Object other) and hashCode() methods.

  • @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
Constructors made to order: Generates constructors that take no arguments, one argument per final / non-nullfield, or one argument for every field.

  • @Data
@Data is a convenient shortcut annotation that bundles the features of @ToString, @EqualsAndHashCode, @Getter / @Setter and @RequiredArgsConstructor together

  • @Value
@Value is the immutable variant of @Data; all fields are made private and final by default, and setters are not generated. The class itself is also made final by default, because immutability is not something that can be forced onto a subclass.

  • @Builder
The @Builder annotation produces complex builder APIs for your classes. @Builder lets you automatically produce the code required to have your class be instantiable.

  • @Synchronized
@Synchronized is a safer variant of the synchronized method modifier. Like synchronized, the annotation can be used on static and instance methods only. It operates similarly to the synchronized keyword, but it locks on different objects.


Dependencies for lombok in Java projects:
Maven
In order to add lombok to the classpath of a Java Project with Apache Maven the following dependency has to be added:

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


Gradle
In order to add lombok to the classpath of a Java Project with Gradle the following dependency has to be added:

dependencies {
    compileOnly('org.projectlombok:lombok')
}


Once we will add this dependency then we are enable Lombok project, and all annotation we can write in your application. But itís not enough to enable Lombok project in our eclipse. So for that we need to configure Lombok in our eclipse IDE.
Follow below steps:
  • Go to m2 repository , where Lombok jar stored

  • Then go to command prompt and specify path to this directory and then run this Lombok jar with command

  • Run command

  • After run above command one popup will open like below

  • Then click on specify location and then give your eclipse.exe installed path like below


Write your Entity/Model class as below:


So if you mark above dto we manually no need to write setter, getter, toString and constructor manually

These features we can achieve using annotation provided by Lombok tool.


tags: lombok tool

About author

User Image
Basant

Well, This is Basant Having Experience in Java/J2ee development platform , currently working as a technology consultant in one of MNC and am passionate about Java technology completely lost in code . Exploring, Learning and Sharing.

0

-Comments

Be the first person to write a comment for this Blog
Load More

No More Comments

Leave a Comment

Your comment has been posted and will appear soon.