Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent lombok from being packaged into Spring Boot jar?

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

like image 672
Kirill Avatar asked Dec 18 '18 21:12

Kirill


People also ask

Why is Lombok dependency 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.

How can I add external jar in spring boot?

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.

Does spring boot create fat jar?

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.

Can I use a spring boot jar as a dependency?

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.


2 Answers

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.

like image 168
Ian Lim Avatar answered Nov 01 '22 09:11

Ian Lim


Looks like it's a transitive dependency of one of your dependency. If you confirm it, you know what to do.

like image 40
Alexander.Furer Avatar answered Nov 01 '22 08:11

Alexander.Furer