1

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.

5
  • please show the full error Commented Nov 6, 2024 at 16:21
  • @PerdiEstaquel Just added more detail to the post. Commented Nov 6, 2024 at 16:41
  • You know that lombock is not responsible for generating Setting_ class? hibernate-jpamodelgen does. Commented Nov 6, 2024 at 16:47
  • @talexmovedtoCodidact Learning something new today. Thank you for that info. It seems there are issues using Lombok together with entity classes: thorben-janssen.com/… Commented Nov 6, 2024 at 17:20
  • @Socrates I had this problem. Don't remember details, but do remember that I solve it by changing an order of annotation processors. Commented Nov 6, 2024 at 17:48

1 Answer 1

2

You will need to add maven-processor-plugin to the pom.xml

To ensure the generated classes are created before compilation. This plugin runs annotation processors like those required for Lombok and Hibernate JPA model generation during the generate-sources phase.

https://mvnrepository.com/artifact/org.bsc.maven/maven-processor-plugin[maven-processor-plugin]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.