I use Lombok in my Spring project and because I use the IDE Visual Studio Code with the plugin Extension Pack for Java, the necessary generated sources are created automatically. This works inside of the IDE, but it fails outside of it.
When running "./mvnw clean package" I get the error: cannot find symbol. I strongly believe that this problem is due to a missing configuration in the pom.xml file, that triggers the creation of the generated sources before packaging.
How can I trigger the creation of the generated sources for Lombok before packaging?
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.myproject</groupId>
<artifactId>base</artifactId>
<version>0.0.1</version>
<name>base</name>
<description>My Project Base</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>23</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<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>
</project>
Edit 1:
I get errors everywhere Lombok is used, i.e. this error:
[ERROR] /home/myuser/Documents/Programming/myproject/backend/base/src/main/java/com/myproject/base/service/logic/SettingService.java:[11,42] cannot find symbol
symbol: class Setting_
location: package com.myproject.base.data.model.entity
In this instance the class SettingService tries and fails to import this:
import com.glasses.base.data.model.entity.Setting_;
for making use of it like this with SETTING_KEY:
Sort sort = Sort.by(Sort.Direction.ASC, Setting_.SETTING_KEY)
.and(Sort.by(Sort.Direction.ASC, Setting_.SETTING_VALUE));
The Setting class looks like this:
import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.Table; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "setting")
public class Setting {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(min = 1, max = 50)
private String settingKey;
@NotBlank
@Size(min = 1, max = 50)
private String settingValue;
public Setting(String settingKey, String settingValue) {
this.settingKey = settingKey;
this.settingValue = settingValue;
}
}
The problem here is that this file is missing "target/generated-sources/annotations/com/glasses/base/data/model/entity/Setting_.java" which would look like this:
import jakarta.persistence.metamodel.SingularAttribute;
import jakarta.persistence.metamodel.StaticMetamodel;
import javax.annotation.processing.Generated;
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Setting.class)
public abstract class Setting_ {
public static volatile SingularAttribute<Setting, String> settingValue;
public static volatile SingularAttribute<Setting, Long> id;
public static volatile SingularAttribute<Setting, String> settingKey;
public static final String SETTING_VALUE = "settingValue";
public static final String ID = "id";
public static final String SETTING_KEY = "settingKey";
}
Currently this file is only generated by the IDE, but not by Maven. This is was needs to change.
Setting_class?hibernate-jpamodelgendoes.