If you visit official Lombok Maven guide, you will see that it's scope should be provided
.
When I create a new project from scratch using start.spring.io and add Lombok, it gets only <optional>true</optional>
in resulting pom.
Then I package Spring boot app like this and out of curiosity decide to see what gets packaged inside of jar:
mvn clean package -DskipTests=true && unzip target/demo-0.0.1-SNAPSHOT.jar -d exploded
Lombok is packaged, does not matter if I set scope to provided
or only have optional=true
.
How to prevent Lombok from being included to Spring Boot jar?
My last thought was to try another goal which comes with Spring Boot plugin:
mvn spring-boot:repackage
But unfortunately it produces the following error:
repackage failed: Source file must be provided
To set up lombok with any build tool, you have to specify that the lombok dependency is required to compile your source code, but does not need to be present when running/testing/jarring/otherwise deploying your code. Generally this is called a 'provided' dependency.
There are two simple ways to do that: the simplest one is to add the property on the command line. Also, you will not launch the Spring Boot application in the usual format (java -jar application). On the other hand, you will launch the PropertiesLauncher class but adding in the classpath your Spring Boot application.
Behind the scenes, spring-boot packages all the project dependencies inside the final artifact along side project classes (hence the “fat” jar). An embedded Tomcat server is also built-in.
You cannot use a spring-boot-plugin-manipulated-jar as a "normal" dependency, as its structure has been changed to be "startable" as a standalone JAR for Spring.
Lombok will only be available in a seperate folder lib-provided instead of the usual lib folder if you are packaging war file.
In order to exclude lombok completely from the final jar/war file, you have to do this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.1.RELEASE</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
Please refer to Exclude a dependancy section of Spring Boot Maven Plugin https://docs.spring.io/spring-boot/docs/2.3.x/maven-plugin/reference/html/#repackage-example-exclude-dependency for more details.
Looks like it's a transitive dependency of one of your dependency. If you confirm it, you know what to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With