Maven compiler plugin does not see Lombok's annotations

Viewed 618

in my application I have 3 modules like this:

permissions (parent pom)
|---permission-api (just api, without main spring class)
|---permission-service (spring boot app)

In parent pom in main module I have pom which contains:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
    <module>permissions-api</module>
    <module>permissions-service</module>
</modules>

and some dependencies. Now I want to build my whole project with maven clean install. Permission-api module is build (there is no problem with lombok annotations). In pom only reference to lombok is:

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

Anyway when I try to build only permission-serivce or entire project, I've got compilation errors like this:

cannot find symbol
[ERROR]   symbol:   method getPermissionsList()
[ERROR]   location: variable employee of type permissions.model.entity.Employee

I know that I should add maven-compiler-plugin to pom.xml (I tried to add this to parent pom too) and I did but it does not help:

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


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>

Can you tell me what am I doing wrong?

3 Answers

Meanwhile I found a solution. Everything started to works when I add:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-envers</artifactId>
    <version>${hibernate.version}</version>
</dependency>

Probably that missing dependency was causing first compilation error and maven compiler incorectly showed next lombok "missing annotations" as compilation errors too.

Dependency, as in projectlombok.org, should be in scope 'provided', meaning that it's required only for compilation, but not needed in runtime:

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

This should be enough.

When you use optional true, you are preventing dependency being transferred to its children transitively. Remove optional and you should be fine.

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

Related