Maven Scope for Lombok (Compile vs. Provided)

JavaMavenLombok

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

One can work with compile and true for <optional/>.

<scope>compile</scope>
<optional>true</optional>

See Maven – Optional Dependencies and Dependency Exclusions.

Solution 3 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmkraemerxView Question on Stackoverflow
Solution 1 - JavaagentgonzoView Answer on Stackoverflow
Solution 2 - JavaJin KwonView Answer on Stackoverflow
Solution 3 - JavaEmirCalabuchView Answer on Stackoverflow