Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Scope for Lombok (Compile vs. Provided)

Tags:

java

maven

lombok

I recently found out that the lombok.jar ends up in our final artifact, which shouldn't be necessary. In my understanding lombok is compile-time only.

        <dependency>             <groupId>org.projectlombok</groupId>             <artifactId>lombok</artifactId>             <version>1.14.4</version>         </dependency> 

But when I set it to scope provided, I get strange behaviour in unit tests. They crash with ClassNotFoundExceptions then when trying to resolve

java.lang.NoClassDefFoundError: com/svv/esp/serviceimpl/dataimport/common/validation/LongValidator 

Which maven scope is in general used for lombok?

I'm using Oracle JDK build 1.8.0_25-b17 on MacOSX 10.9

like image 264
mkraemerx Avatar asked Apr 01 '15 08:04

mkraemerx


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.

What does maven scope provided mean?

Maven dependency scope provided is used during build and test the project. They are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by servlet container or application server.

What is the use of provided scope in maven dependency?

Provided. We use this scope to mark dependencies that should be provided at runtime by JDK or a container. A good use case for this scope would be a web application deployed in some container, where the container already provides some libraries itself.

What does compile mean in maven dependency?

Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects. provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime.


2 Answers

Lombok should be used at the provided scope (see the official docs).

The reason (as has been stated in the comments) is that lombok is a compile-time-only tool. That is, it is not needed at runtime at all. By making the scope provided, you make the lombok libraries available to the compiler but it is not a dependency of your compiled jar. As such, your final jar will not depend on Lombok and it does not need to be included in any deployment, which reduces the dependencies and size of your deployables.

like image 109
agentgonzo Avatar answered Sep 22 '22 02:09

agentgonzo


Usually compile. provided is for jars that are usually shipped with the application server that will host the application. If you don't want the jar in the final application, it is maybe best to use the maven plugin rather than the jar directly: http://awhitford.github.io/lombok.maven/lombok-maven-plugin/index.html

like image 25
EmirCalabuch Avatar answered Sep 22 '22 02:09

EmirCalabuch