7

I am trying to use Lombok in my project. My question is that I have to add Lombok dependency in POM.xml as below

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

BUT

a) WHY DO I have to add the code below under the build tag? What is the need for the below exclusion?

<build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
          <excludes>
            <exclude>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
            </exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>

b) Why do I need to install the LOMBOK plugin as part of IntelliJ idea settings?

Can anyone explain in simple and layman's language so that my basics are cleared?

Helena
  • 444
  • 2
  • 15
  • 2
    You shouldn't need the exclude, make the dependency scoped provided and it is already optional. So I would expect it not to be in the artifact anyway. – M. Deinum Dec 08 '22 at 07:31

1 Answers1

6

a) Lombok is an annotation processor and is only needed at compile time. That's why it is excluded.

This configuration explicitly removes Lombox from the target artifact.

b) IntelliJ does not use Maven to compile your code. In order to process the Lombok annotations, the IntelliJ plugin must be activated.

IntelliJ uses an internal mechanism to compile your code.

c) Lombok generates code from the annotations. For example

@Getter
public class Employee() {
  private String name;
}

will generate String getName() at COMPILE time.

Therefore Lombok is not needed at runtime.

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • 2
    You seem to have answered but it doesn't explain it fully so that i could understand. I have now more questions due to your answers. a) If some plugin is only needed at compile time, then what it has to do with exclusion under tag? b) If intelliJ doesn't use maven then what it uses? Mine is a maven project so what is IntelliJ using if NOT maven? c) If Lombok is only needed at compile time, then how does things work at runtime? – Helena Dec 07 '22 at 15:22
  • I can answer on (c) - as Lombok generates code(eg, getName method) at compile time, during runtime, getName is interpreted without problem by JRE. After all, Lombok is a kind of a preprocessor before the runtime. – Park JongBum Feb 06 '23 at 01:17