๐ช๐ต๐ฎ๐ ๐ถ๐ ๐ถ๐?
Lombok is a Java library which auto generates boilerplate code through the use of annotations.
It can generate code for getters/setters, ๐๐๐๐๐๐()
, ๐๐๐๐๐ฒ๐๐๐()
, ๐๐๐๐๐๐๐๐()
and even constructors/builders.
๐ช๐ต๐ ๐๐๐ฒ ๐ถ๐?
It eliminates boilerplate code which leads to…
- Improve readability since there is less clutter and also makes pull requests easier to review.
- Increased productivity and frees up your time to focus on core business logic.
An example
No Lombok:
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
// hashCode implementation
}
@Override
public boolean equals(Object o) {
// equals implementation
}
@Override
public String toString() {
return "name: " + name;
}
}
With Lombok:
@Data
public class User {
private String name;
}
- All the boilerplate code is replaced with just a
@Data
annotation. - If you need to add new attributes, you don’t need to change the
equals()
,hashCode()
andtoString()
methods as Lombok will handle them for you!
How to get started?
Note:
The setup below is for maven projects.
At the time of writing, the latest of Lombok is 1.18.38
.
Add the Lombok dependency in your pom.xml
:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
After adding the Lombok dependency, you might get a prompt from your IDE to enable annotation processing (If you don’t get the prompt, you will have to follow your IDE specific instructions to enable). Enable annotation processing.
Add the following plugin in your pom.xml
so that maven build can use the Lombok generated code.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.14.0</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>