Search code examples
javapluginsdependencieslombok

Why do I have to add Lombok plugin, why adding dependency is not enough


Im curious why do I have to add Lombok plugin. Why adding dependency to pom.xml is not sufficient like for example in Mockito?


Solution

  • You have to understand how Lombok actually works.

    There are two parts here:

    • the annotations, such as @Data, @ToString, ...
    • that "plugin" that actually generates the corresponding code

    You need a dependency to lombok so that the compiler/IDE can resolve the annotations themselves. ( annotations need to be imported, just like any other "element" that you are using in your source code ). But that only makes sure that your compiler / IDE knows that these are valid annotations.

    The real trick is that later on, when code is compiled into class files, that "active" part of lombok is available to actually generate code.

    This is similar to java bean validation. You need to import one JAR at compile time, so that all the different validation annotations are known, and can be used within your source code. But in order to objects being actively validated, you later need some other component that does that (the difference here is that validation happens at runtime, whereas Lombok is a compile time only thing).

    And please note: that second way of "annotation usage/handling" is the more common path. In your example about JUnit, and @RunWith: that annotation tells JUnit at runtime to use that mockito runner class (instead of its own runner implementation). And that mockito runner then knows what to do at runtime with all the other annotations. Same story: annotations present in source code need to be known, for the active part, some component is doing stuff at runtime.

    Sure, you can also use annotations to influence the build task, but as said, that is less common, and way more complicated.