Getting Started with Project Lombok

Download Lombok

Download the jar file of Lombok from here .

Using lombok in Java projects

For Eclipse

  • Since downloaded lombok.jar is an executable jar, it is started by double click etc.
  • The Lombok installer starts up, Specify location…click on.
  • eclipse.exe Is selected.
  • Install / Update Click.

For the Gradle project

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')}

For the Maven project

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>

Doing it manually

The lombok library can also be downloaded directly from here: https://projectlombok.org/download.html and be added to the classpath.

lombok manual classpath

Why Lombok is Required?

In Java vs. other language debate, the first knock which you get from other language supporters is that Java requires lot of boiler plate code and you just cannot get over it, and you are defenseless. The same issue is also reported in multiple platforms and developer communities.

Let’s see a sample of code which has boiler plate code.

import java.time.LocalDate;public class Person {	String firstName;	String lastName;	LocalDate dateOfBirth;	public Person(String firstName, String lastName, LocalDate dateOfBirth) {		super();		this.firstName = firstName;		this.lastName = lastName;		this.dateOfBirth = dateOfBirth;	}	public String getFirstName() {		return firstName;	}	public void setFirstName(String firstName) {		this.firstName = firstName;	}	public String getLastName() {		return lastName;	}	public void setLastName(String lastName) {		this.lastName = lastName;	}	public LocalDate getDateOfBirth() {		return dateOfBirth;	}	public void setDateOfBirth(LocalDate dateOfBirth) {		this.dateOfBirth = dateOfBirth;	}	@Override	public int hashCode() {		final int prime = 31;		int result = 1;		result = prime * result + ((dateOfBirth == null) ? 0 : dateOfBirth.hashCode());		result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());		result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());		return result;	}	@Override	public boolean equals(Object obj) {		if (this == obj)			return true;		if (obj == null)			return false;		if (getClass() != obj.getClass())			return false;		Person other = (Person) obj;		if (dateOfBirth == null) {			if (other.dateOfBirth != null)				return false;		} else if (!dateOfBirth.equals(other.dateOfBirth))			return false;		if (firstName == null) {			if (other.firstName != null)				return false;		} else if (!firstName.equals(other.firstName))			return false;		if (lastName == null) {			if (other.lastName != null)				return false;		} else if (!lastName.equals(other.lastName))			return false;		return true;	}	@Override	public String toString() {		return "Person [firstName=" + firstName + ", lastName=" + lastName + "dateOfBirth=" + dateOfBirth + "]";	}}

A class should have getter-setters for the instance variables, equals & hashCode method implementation, all field constructors and an implementation of toString method. This class so far has no business logic and even without it is 80+ lines of code. This is insane.

Project Lombok Solution:

Project Lombok is a java library that automatically plugs into your editor and build tools and helps reduce the boiler plate code. Same code we can write with the help of Lomobok Annotation:

@AllArgsConstructor @Getter @Setter@EqualsAndHashCode @ToStringpublic class PersonLombok {	String firstName;	String lastName;	LocalDate dateOfBirth;}

Now this code is neat and clean.