Spring Boot 2 - using Lombok plugin with Maven project (in pom.xml file)
In this short article, we would like to show how to add Lombok plugin to Spring Boot 2 application that is configured with Maven project.
What is Lombok?
It is a plugin that lets to automatically generate constructors, getters, setters, equals method and much more, saving programmer time making source code more clear.
Â
Quick solution:
- add Lombok library using
provided
scope
to<dependencies>
:
e.g.<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> <scope>provided</scope> </dependency>
- add Java versions toÂ
<configuration>
 inÂmaven-compiler-plugin
:
e.g.<source>11</source> <target>11</target>
- add Lombok path inside
<annotationProcessorPaths>
element:
e.g.<path> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.24</version> </path>
Note: to find newest Lombok version check this link.
Â
Practical example
In this section you can find example pom.xml
file that contains Lombok configuration. The solution presented in the below allows to generate source code by Lombok in build time.
Hint: it can be used with Maven modules too.
pom.xml
file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
<relativePath/>
</parent>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version> <!-- --- required -->
<maven.compiler.source>${java.version}</maven.compiler.source> <!-- --- required -->
<maven.compiler.target>${java.version}</maven.compiler.target> <!-- --- required -->
</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> <!-- ------------------------- required -->
<groupId>org.projectlombok</groupId> <!-- ------------------------- required -->
<artifactId>lombok</artifactId> <!-- ------------------------- required -->
<version>1.18.24</version> <!-- ------------------------- required -->
<scope>provided</scope> <!-- ------------------------- required -->
</dependency> <!-- ------------------------- required -->
</dependencies>
<build>
<plugins>
<plugin> <!-- --------- required -->
<groupId>org.apache.maven.plugins</groupId> <!-- --------- required -->
<artifactId>maven-compiler-plugin</artifactId> <!-- --------- required -->
<version>3.7.0</version> <!-- --------- required -->
<configuration> <!-- --------- required -->
<source>${maven.compiler.source}</source> <!-- --------- required -->
<target>${maven.compiler.target}</target> <!-- --------- required -->
<annotationProcessorPaths> <!-- --------- required -->
<path> <!-- --------- required -->
<groupId>org.projectlombok</groupId> <!-- --------- required -->
<artifactId>lombok</artifactId> <!-- --------- required -->
<version>1.18.24</version> <!-- --------- required -->
</path> <!-- --------- required -->
</annotationProcessorPaths> <!-- --------- required -->
</configuration> <!-- --------- required -->
</plugin> <!-- --------- required -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Â
Testing Lombok
1. Using IntelliJ IDEA
If Lombok is not installed it is necessary to install Lombok Plugin in IntelliJ IDEA.
Note: we can enable Lombok by clicking on popup in right bottom corner after IntelliJ detected it.
Â
2. Using command line:
Run the command:
mvn install
Example output (ending):
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.770 s
[INFO] Finished at: 2022-05-17T07:01:01+02:00
[INFO] ------------------------------------------------------------------------
Â