Home Lombok A basic java lombok maven example with eclipse

A basic java lombok maven example with eclipse

In this guide we are going to see what is project Lombok, setting up Lombok with IDE like eclipse or STS, a simple java Lombok maven example and project Lombok main features.

1. What is Project Lombok

Project Lombok is a mature java library that plugs into your editor or IDE like eclipse, STS, IntelliJ etc, also can plugs into build tools like maven, gradle, ant etc. Lombok library helps your ide to ignore generating boiler plate code for your pojo classes. And this is done during compile time.

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.

2. How Project Lombok works with IDE

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 (getters, setters, toString(),…etc) automatically, which happens at compile time, so developer no need to write code manually for the setters, getters, overriding toString(), equals(), hashcode(), generating with arguments and default constructors etc.

java lombok workflow

3. Setup Lombok in Eclipse or STS

  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 or 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

4. 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.

  <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.10</version>
			<scope>provided</scope>
		</dependency>

5. A simple Java lombok example :

You can annotate any field with @Getter and / or @Setter, to let lombok generate the default getter or 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

public class User {
	
    private Long id;
    private String username;
    private String firstName;
    private String lastName;
    private String email;
    private LocalDate lastUpdated;
    private boolean active;
    
    User(){} //default constructor
    
    //All args constructor
	public User(Long id, String username) {
		super();
		this.id = id;
		this.username = username;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	// other setters getters
	/* toString(), hashCode(),...etc
      * 50+ more lines of code */
}

A Lombok java Pojo

@Setter @Getter
@AllArgsConstructor
@NoArgsConstructor
public class User {
	
    private Long id;
    private String username;
    private String firstName;
    private String lastName;
    private String email;
    private LocalDate lastUpdated;
    private boolean active;
    
}

6. Test the Lombok Pojo class

public class SetterGetterDemo {
	
    public static void main(String[] args) {
    	
        User user = new User();
        user.setId(1L);
        user.setFirstName("Peter");
        user.setLastName("Milanovich");
        user.setActive(true);
        user.setUsername("peterm");
        user.setLastUpdated(LocalDate.now());
       

        System.out.println("Id : "+user.getId());
        System.out.println("Username : "+user.getUsername());
        System.out.println("Fisr Name : "+user.getFirstName());
        System.out.println("Last Name : "+user.getLastName());
        System.out.println("is user active? : "+user.isActive());
        System.out.println("last updated date : "+user.getLastUpdated());
        
    }
}

6.1. Output results

Id : 1
Username : peterm
Fisr Name : Peter
Last Name : Milanovich
is user active? : true
last updated date : 2019-12-19

7. Conclusion

In this guide we have covered how to setup Lombok with your IDE and configure lombok using maven with a simple example.

References

  1. Project Lombok
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories