Languages
[Edit]
EN

Spring Boot 2 - using Lombok plugin with Maven project (in pom.xml file)

10 points
Created by:
Creg
9450

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:

  1. 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>
  2. add Java versions to <configuration> in maven-compiler-plugin:
    e.g.
    <source>11</source>
    <target>11</target>
  3. 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] ------------------------------------------------------------------------

 

References

  1. Project Lombok - Homepage
  2. Project Lombok - Maven Repository

Alternative titles

  1. Spring Boot 2 - use Lombok plugin with Maven project (in pom.xml file)
  2. Spring Boot 2 - add Lombok plugin to Maven project (in pom.xml file)
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

â€ïžđŸ’» 🙂

Join