javaspring-bootdockermavenlombok

Docker lombock mvn package cannot find symbol, constructor cannot be applied to given types


root
  src
  pom.xml
  Dockerfile
  docker-compose.yaml

Dockerfile

FROM openjdk:22-ea-19-jdk-bookworm

# Install tools
RUN apt update && apt install -y maven vim

# Set working directory
WORKDIR /app

# Copy all project files at once
COPY . .

# Build the project (download dependencies and compile)
RUN mvn clean package -DskipTests

# Expose port
EXPOSE 8082

# Run the jar
CMD ["java", "-jar", "target/myapp.jar"]

When I run

docker build -t myappimage . 

It fails at mvn clean package with the compilation errors like:

45.79 [INFO] Recompiling the module because of changed dependency.
45.83 [INFO] Compiling 55 source files with javac [debug parameters release 21] 
to target/classes
52.75 [INFO] -------------------------------------------------------------
52.76 [ERROR] COMPILATION ERROR : 
52.76 [INFO] -------------------------------------------------------------
52.76 [ERROR] /app/src/main/java/com/myapp/DataInitializer.java:
[37,38] constructor DogBreed in class com.myapp.dog.entity.DogBreed cannot 
be applied to given types;
52.76   required: no arguments
52.76   found:    <nulltype>,com.myapp.dog.DogBreedEnum
52.76   reason: actual and formal argument lists differ in length
52.76 [ERROR] /app/src/main/java/com/myapp/DataInitializer.java:
[38,33] incompatible types: inference variable T has incompatible bounds
52.76     equality constraints: com.myapp.dog.entity.DogBreed
52.76     lower bounds: java.lang.Object
52.76 [ERROR] /app/src/main/java/com/myapp/DataInitializer.java:
[49,25] constructor Person in class com.myapp.Person 
cannot be applied to given types;
52.76   required: no arguments
52.76   found:    <nulltype>,java.lang.String,int,int
52.76   reason: actual and formal argument lists differ in length

I use lombock for creating constructors for my entities, could that be the reason?

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor

Also after the constructor errors come these

52.85 [ERROR] /app/src/main/java/com/myapp/dog/service/DogService.java:[122,36]
 cannot find symbol
52.85 [ERROR]   symbol:   method getIsGood()
52.85 [ERROR]   location: variable dog of type com.myapp.dog.entity.Dog

Meanwhile if I simply do mvn package without using Docker, it produces a jar just fine. And I can run it with java -jar myapp.jar fine as well.


Solution

  • Inside Docker, Maven runs in a clean, headless environment. If Lombok isn't properly configured as an annotation processor, Maven won't generate constructors or getters.

    In your situation, it looks like Lombok annotations are not being processed during the Docker build.

    Please, make sure that your pom.xml has a correct Lombok configuration (see docs), especially the required annotation processor paths:

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.38</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>1.18.38</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>