Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure Lombok with maven-compiler-plugin?

I have a root module and submodule in maven in the project. I am trying to use Lombok. I have added

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.12</version>
    <scope>provided</scope>
</dependency>

to root pom.xml. In submodule I have a class with Lombok annotations. When I am trying to build the project I get a lot of

cannot find symbol

where I am trying to call getters and setters.

I have tried to use lombok-maven-plugin with same version (1.16.12) in root pom and in the sub pom as well with delombok and moving my annotated class to src/main/lombok, I have looked through almost all questions in SO, try all the variants, but not succeed.

I am using Maven 3, Java 8, maven-compiler-plugin with 3.6.1 version.

How should I configure the project to be able to use lombok? Or maybe I am doing smth wrong.

like image 654
O.Zaiats Avatar asked Feb 15 '17 18:02

O.Zaiats


People also ask

What is Lombok maven dependency?

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 do I add Lombok to my project?

Go to File > Settings > Plugins. Click on Browse repositories... Search for Lombok Plugin. Click on Install plugin.

How do you use Lombok dependency?

1. Introduction to lombok. Lombok is used to reduce boilerplate code for model/data objects, e.g., it can generate getters and setters for those object automatically by using Lombok annotations. The easiest way is to use the @Data annotation.

Is Lombok needed at runtime?

Speaking about additional dependencies, Lombok is a compile-time library. So, it's not needed in the runtime and it does not affect the bundle size.


1 Answers

This is not a direct answer to the question which seems to be solved but acts as reference for future searchers:

If you're using Dagger (or something else) to process your annotations like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>
        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

You have to add lombok as path like

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
      <configuration>
        <annotationProcessorPaths>
          <path>
            <groupId>com.google.dagger</groupId>
            <artifactId>dagger-compiler</artifactId>
            <version>2.15</version>
          </path>

          <!-- SOLUTION --> 
          <path>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
          </path>


        </annotationProcessorPaths>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
    ....
  </plugins>
</build>

You still have to list lombok as provided dependency tho.

like image 164
Ore Avatar answered Oct 04 '22 02:10

Ore