spring-io / start.spring.io

https://start.spring.io
Apache License 2.0
2.26k stars 908 forks source link

Generated Maven project does not process Lombok annotations when compiling with Java 23 #1654

Open lwiddershoven opened 3 days ago

lwiddershoven commented 3 days ago

Example project (reproduction): https://github.com/lwiddershoven/spring-boot-start-lombok-issue

Problem

Lombok-generated methods are not found during maven compilation.

Reproduction

A project is created in start.spring.io, using maven and Java 23, with as only dependency Lombok.

Then a normal Java class SomeData is added with the Lombok @Data annotation. One attribute, id is added to this data class. This is then referenced/used in SomeDataUser. mvn clean verify will complain:

[ERROR]   /.../lombok/src/main/java/com/example/bugs/lombok/SomeDataUser.java:[7,14] cannot find symbol
[ERROR]   symbol:   method setId(int)
[ERROR]   location: variable someData of type com.example.bugs.lombok.SomeData
@Data
public class SomeData {
  private int id;
}
public class SomeDataUser {
  void test() {
     var someData = new SomeData();
     someData.setId(1);
  }
}

Expectation

I would expect that a freshly generated project with Lombok as a dependency would process annotations as part of the maven build.

Fix

One can add

<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>

to the pom file. This enables annotation processing during the build.

wilkinsona commented 3 days ago

Thanks for the sample.

The problem's occurring because Java 23 disabled automatic discovery of annotation processors from the classpath. As an alternative to declaring annotation processor paths (which requires duplicating the Lombok version), you can set a property instead:

<maven.compiler.proc>full</maven.compiler.proc>

We should probably set this property automatically for projects using Java 23, Maven, and Lombok.

lwiddershoven commented 3 days ago

This is confirmed to work also for me.

It does require an up-to-date spring-boot as the maven compiler plugin (provided by Spring Boots' parent) has to be 3.13.0, maybe 3.12.0 but higher than 3.11.0.

wilkinsona commented 3 days ago

Thanks for the confirmation. Spring Boot 3.3.x and 3.4.x should both be OK at they use 3.13.0 of the compiler plugin by default. Spring Boot 3.2.x and earlier are no longer supported by start.spring.io.