Partner – Codium – NPI (cat = IDE)
announcement - icon

Explore the secure, reliable, and high-performance Test Execution Cloud built for scale. Right in your IDE:

>> CodiumAI. Meaningful Code Tests for Busy Devs

Basically, write code that works the way you meant it to.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Lombok is a library that facilitates many tedious tasks and reduces Java source code verbosity.

Of course, we usually want to be able to use the library in an IDE, which requires additional setup.

In this tutorial, we’ll talk about configuring Lombok in two of the most popular Java IDEs — IntelliJ IDEA and Eclipse.

Further reading:

Using Lombok's @Builder Annotation

Learn how the @Builder annotation in Project Lombok can help you reduce boilerplate code when implementing the builder pattern to create instances of your Java classes.

Introduction to Project Lombok

A comprehensive and very practical introduction to many useful usecases of Project Lombok on standard Java code.

2. Lombok in IntelliJ IDEA

As of IntelliJ version 2020.3, we don’t need to configure the IDE to use Lombok anymore. The IDE comes bundled with the plugin. Also, the annotation processing will be enabled automatically.

In earlier versions of IntelliJ, we need to perform the below steps to use Lombok. Also, if we use the latest version and the IDE doesn’t recognize the Lombok annotation, we need to verify that the below configuration was not disabled manually.

2.1. Enabling Annotation Processing

Lombok uses annotation processing through APT. So, when the compiler calls it, the library generates new source files based on annotations in the originals.

Annotation processing isn’t enabled by default, though.

Therefore, the first thing to do is to enable annotation processing in our project.

We need to go to the Preferences | Build, Execution, Deployment | Compiler | Annotation Processors and make sure of the following:

  • Enable annotation processing box is checked
  • Obtain processors from project classpath option is selected
lombok1

2.2. Installing the IDE Plugin

While Lombok generates code only during compilation, the IDE highlights errors in raw source code:

lobom2

There is a dedicated plugin that makes IntelliJ aware of the source code to be generated. After installing it, the errors go away and regular features like Find Usages and Navigate To start working.

We need to go to the Preferences | Plugins, open the Marketplace tab, type “lombok” and choose Lombok Plugin by Michail Plushnikov:

lombok3

Next, click the Install button on the plugin page:

lombok4

After the installation, click the Restart IDE button:

lombok5

3. Lombok in Eclipse

If we’re using Eclipse IDE, we need to get the Lombok jar first. The latest version is located on Maven Central.

For our example, we’re using lombok-1.18.4.jar.

Next, we can run the jar via java -jar command, and an installer UI will open. This tries to automatically detect all available Eclipse installations, but it’s also possible to specify the location manually.

Once we’ve selected the installations, we press the Install/Update button:

lombok6

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 the About dialog:

lombok7

4. Adding Lombok to the Compile Classpath

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:

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

The most recent version is located on Maven Central.

Everything should be fine now. The source code should be shown without errors in the IDE, correctly compiled and executed:

public class UserIntegrationTest {

    @Test
    public void givenAnnotatedUser_thenHasGettersAndSetters() {
        User user = new User();
        user.setFirstName("Test");
        assertEquals(user.gerFirstName(), "Test");
    }

    @Getter @Setter
    class User {
        private String firstName;
    }
}

5. Conclusion

Lombok does a great job of reducing Java verbosity and covering boilerplate stuff under the hood. In this article, we checked how to configure the tool for the two most popular Java IDEs.

The source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!