How to solve lombok not working in IntelliJ IDEA?
1. Purpose
In this post, I will demonstrate how to solve the following error when using lombok in IntelliJ IDEA:
When I’m trying to use lombok @Data
in my java project, I got following error:
Just as the following picture shows:
But I have already defined the lombok @Data as follows:
package com.zzz.pojo.query;
import lombok.Data;import jakarta.validation.constraints.NotNull;import io.swagger.v3.oas.annotations.media.Schema;
/** * Weather query request. * */@Datapublic class WeatherQuery {
/** * latitude */ @NotNull(groups = { QueryGroup.class }, message = "latitude can not be empty") @Schema(description = "latitude") private Double latitude;
/** * longitude */ @NotNull(groups = { QueryGroup.class }, message = "longitude can not be empty") @Schema(description = "longitude") private Double longitude;}
And I have already configured pom.xml
to include lombok
:
<?xml version="1.0" encoding="UTF-8"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.4</version> </parent> <groupId>com.zzz</groupId> <artifactId>JavaProject</artifactId> <version>1.0.0</version> <name>JavaProject</name> <description>JavaProject</description> <properties> <jdk.version>17</jdk.version> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate.validator</groupId> <artifactId>hibernate-validator</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.14</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build></project>
You can see the parsed lombok dependency:
It should work! But it did not!
I should not see the red alert as following :
The error message I got when I put my mouse over the red characters :
Cannot resolve method 'getLatitude' in 'WeatherQuery'
No candidates found for method call weatherQuery. getLatitude().
But When I try to mvn compile
the project, I do it successfully:
/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java -Dmaven.multiModuleProjectDirectory=...[INFO] Scanning for projects...[INFO][INFO] ------------------------< com.zzz:JavaProject >-------------------------[INFO] Building JavaProject 1.0.0[INFO] from pom.xml[INFO] --------------------------------[ jar ]---------------------------------[INFO][INFO] --- maven-resources-plugin:3.3.1:resources (default-resources) @ JavaProject ---[INFO] Copying 1 resource from src/main/resources to target/classes[INFO] Copying 2 resources from src/main/resources to target/classes[INFO][INFO] --- maven-compiler-plugin:3.13.0:compile (default-compile) @ JavaProject ---[INFO] Recompiling the module because of changed source code.[INFO] Compiling 15 source files with javac [debug parameters release 17] to target/classes[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 0.892 s[INFO] Finished at: 2025-03-31T16:08:13+08:00[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
So,The problem exists in IntelliJ IDEA, not in the code.
2. The Solution
2.1 What is lombok?
Lombok is a Java library. It helps reduce boilerplate code, such as getters, setters, constructors, and toString methods, by using annotations. This makes the code more concise and easier to read and maintain.
2.2 What is annotation?
In Java, annotations are a form of metadata that can be added to Java code (classes, methods, variables, etc.). They provide additional information about the code, which can be used by the compiler, tools, or at runtime.
Here is a very simple example of custom annotation:
import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;
// Define a custom annotation@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD)@interface MyAnnotation { String value() default "default value";}
class MyClass { @MyAnnotation("Hello, Annotation!") public void myMethod() { System.out.println("This is my method."); }}
public class CustomAnnotationExample { public static void main(String[] args) throws NoSuchMethodException { MyClass obj = new MyClass(); java.lang.reflect.Method method = obj.getClass().getMethod("myMethod"); if (method.isAnnotationPresent(MyAnnotation.class)) { MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); System.out.println("Annotation value: " + annotation.value()); } obj.myMethod(); }}
You can see that I have defined a custom annotation named MyAnnotation
,which can be used on a java method, then
MyCalss
’s myMethod
uses this annotation, which adds some more information to that method. When I use the java
reflection api to collect that information, I can print the value.
It’s very simple, isn’t it?
2.3 How to install lombok?
To install lombok, you can use maven or gradle to import it as dependency:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> <!-- You can choose the appropriate version --> <scope>provided</scope></dependency>
Or in gradle:
dependencies { compileOnly 'org.projectlombok:lombok:1.18.26' // You can choose the appropriate version annotationProcessor 'org.projectlombok:lombok:1.18.26'}
And one more thing here, you need to install a plugin in your IDE:
title=Install lombok plugin in IntelliJ IDEAInstall the Lombok plugin. Go to File -> Settings (on Windows/Linux) or IntelliJ IDEA -> Preferences (on macOS). Then navigate to Plugins, search for "Lombok" in the Marketplace, and install it.Enable annotation processing. Go to File -> Settings (or Preferences), then Build, Execution, Deployment -> Compiler -> Annotation Processors, and check the "Enable annotation processing" box.
2.4 The Solution
So the solution is easy, let’s check our IntelliJ IDEA :
- check if I have installed the lombok plugin
- check if I have enabled the annotation processing
For the annotation processing, I got this:
You can see that I have not enable the annotation processing.
And For the lombok plugin:
I have not installed it yet, so I click install.
After both jobs done, I got this:
It works!
3. Summary
In this post, I demonstrated how to correctly use lombok in IntelliJ IDEA. That’s it, thanks for your reading.
Final Words + More Resources
My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!