18
votes

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.

7
did you add it as a dependency? you need to configure it as plugin to be executed before the compile plugin: awhitford.github.io/lombok.maven/lombok-maven-plugin/usage.html - wemu
what kind of IDE are you using? - pezetem
How are you building your project? Are you using an IDE? Have you tried building it using maven directly? - Michele Da Ros
@wemu Yes, I have tried with a plugin as well : <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.16.12.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> </plugin> Also I have put annotated classes in src/main/lombok instead of src/main/java - O.Zaiats
@MicheleDaRos IDE does not matter. I am running maven commands - O.Zaiats

7 Answers

22
votes

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.

8
votes

In case of anyone using JDK 11

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>${lombok.version}</version>
                    </path>
                </annotationProcessorPaths>
                <compilerArgs>
                    <arg>-sourcepath</arg>
                    <arg>${project.basedir}/src/main/java${path.separator}${project.basedir}/target/generated-sources/annotations${path.separator}/</arg>
                </compilerArgs>
            </configuration>
        </plugin>
    </plugins>
</build>
2
votes

I was using Java 8 and @Getter(onMethod = @__({@NoSerialization})) and @Getter(onMethod = @__({@Translation(messageKey = "translation.key")})) onX annotations. And I get duplicate element '<any?>' in annotation @<any?>. in error output. Looks like guys from Lombok have such issue with Java 8 for a long time link to issue on github. Lombok does not handle annotations with parameters like messageKey in annotation above. it works only with annotations without parameters and annotations with only value parameter (when you don't write the name of parameter).

1
votes

I'm not sure what the difference is between lombok and lombok-maven-plugin, but my projects are configured with this dependency:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-maven-plugin</artifactId>
        <version>1.16.12.0</version>
    </dependency>

I haven't experimented with root and submodule poms yet, as my projects all tend to be rather isolated from each other. Not sure if that could be causing an issue for you.

If you are using Eclipse, have you run the lombok.jar file and pointed it to your eclipse.exe file? it needs to modify the .exe in order for Eclipse to know that those getters and setters are coming, so that Eclipse doesn't complain during development.

Edit: I'm using maven-compiler-plugin:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
1
votes

I had this same issue and nothing worked, i.e. maven plugin version, annotationProcessorPaths, provided scope, etc.

In the end I narrowed it down to having a static import on an @UtilityClass method from a class within the same project, i.e. not brought in from a dependency. This caused annotation processing to fail, even for unrelated classes, and made it look like lombok code was just not being compiled properly. Getting rid of the static import made it all work.

There's an open issue on Github for this, though apparently it's too hard to fix.

0
votes

Maven Groovy and Java + Lombok

The solution on this stack overflow answer worked for me. I missed adding the javaAgentClass earlier

-1
votes

use:

<scope>provided</scope>

in pom.xml like that:

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