lombok
gradle
spring-boot
java
build-tools

Using lombok with gradle and spring-boot

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Lombok can remove repetitive Java code in Spring Boot projects, but only if build configuration and IDE settings are aligned. Most integration failures come from annotation processor setup, not from Lombok annotations themselves. A reliable setup includes correct Gradle scopes, IDE annotation processing, and one consistent way to verify generated code.

Configure Lombok Dependencies in Gradle

In a Gradle build, Lombok should usually be compileOnly and annotationProcessor. This keeps Lombok out of runtime artifacts while still enabling compile-time code generation.

groovy
1plugins {
2    id 'java'
3    id 'org.springframework.boot' version '3.3.2'
4    id 'io.spring.dependency-management' version '1.1.6'
5}
6
7group = 'com.example'
8version = '0.0.1-SNAPSHOT'
9
10java {
11    toolchain {
12        languageVersion = JavaLanguageVersion.of(21)
13    }
14}
15
16dependencies {
17    implementation 'org.springframework.boot:spring-boot-starter-web'
18
19    compileOnly 'org.projectlombok:lombok:1.18.34'
20    annotationProcessor 'org.projectlombok:lombok:1.18.34'
21
22    testImplementation 'org.springframework.boot:spring-boot-starter-test'
23    testCompileOnly 'org.projectlombok:lombok:1.18.34'
24    testAnnotationProcessor 'org.projectlombok:lombok:1.18.34'
25}

After changes, run a clean compile to force fresh annotation generation.

bash
./gradlew clean compileJava

Use Lombok in Spring Components Carefully

A common and safe pattern in Spring Boot services is constructor injection with @RequiredArgsConstructor.

java
1package com.example.demo.service;
2
3import lombok.RequiredArgsConstructor;
4import org.springframework.stereotype.Service;
5
6@Service
7@RequiredArgsConstructor
8public class InvoiceService {
9    private final TaxCalculator taxCalculator;
10
11    public long totalWithTax(long amount) {
12        return amount + taxCalculator.calculate(amount);
13    }
14}

For response models or value carriers, immutable annotations such as @Value are often easier to maintain than mutable classes.

java
1package com.example.demo.api;
2
3import lombok.Builder;
4import lombok.Value;
5
6@Value
7@Builder
8public class InvoiceResponse {
9    String id;
10    long total;
11}

This keeps boilerplate low while preserving clear domain intent.

IDE Annotation Processing Must Match Build

A frequent problem is a successful Gradle build with IDE red errors. That usually means IDE annotation processing is disabled.

Checklist:

  1. Enable annotation processing in IDE settings.
  2. Reimport or reload Gradle project.
  3. Rebuild project from IDE.
  4. Verify one Lombok class resolves generated members.

Do not skip this sync step in team onboarding documentation.

Logging and Utility Annotations

Lombok logging annotations can reduce repetitive logger setup in Spring jobs and controllers.

java
1package com.example.demo.jobs;
2
3import lombok.extern.slf4j.Slf4j;
4import org.springframework.scheduling.annotation.Scheduled;
5import org.springframework.stereotype.Component;
6
7@Component
8@Slf4j
9public class DailyJob {
10
11    @Scheduled(fixedDelay = 60000)
12    public void execute() {
13        log.info("daily job started");
14        log.info("daily job finished");
15    }
16}

Use this for convenience, but keep logs structured and meaningful. Lombok should reduce syntax, not replace logging discipline.

@Data on Entities Requires Caution

@Data generates equals, hashCode, and toString, which can be risky on JPA entities with lazy relations and identity lifecycle semantics.

Prefer explicit combinations such as:

  • '@Getter'
  • '@Setter'
  • '@ToString(exclude = ...)'
  • custom equals and hashCode where needed

For many persistence models, controlled explicit behavior is safer than broad automatic generation.

CI and Team Stability Practices

To keep Lombok stable across environments:

  • pin Lombok version in build file
  • pin Java toolchain version
  • run clean compile in CI
  • add one smoke test that instantiates a Lombok-annotated Spring bean

Example smoke test:

java
1package com.example.demo;
2
3import com.example.demo.service.InvoiceService;
4import org.junit.jupiter.api.Test;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.boot.test.context.SpringBootTest;
7
8@SpringBootTest
9class ContextTest {
10    @Autowired
11    InvoiceService invoiceService;
12
13    @Test
14    void contextLoads() {
15        assert invoiceService != null;
16    }
17}

This catches annotation processing drift early.

Common Pitfalls

A common pitfall is declaring Lombok only as a normal implementation dependency and forgetting annotation processor scope.

Another pitfall is trusting IDE errors when Gradle build is healthy, or vice versa, without checking annotation processing parity.

A third pitfall is overusing broad annotations such as @Data in entities where generated equality and string output can cause subtle bugs.

Teams also treat Lombok as a design shortcut instead of a code generation helper, which leads to unclear domain models.

Summary

  • Lombok with Spring Boot and Gradle depends on proper annotation processor configuration.
  • Use compileOnly and annotationProcessor scopes for clean runtime artifacts.
  • Keep IDE annotation processing synchronized with Gradle behavior.
  • Use Lombok selectively, especially in persistence-heavy classes.
  • Add CI guardrails so generated code behavior stays stable across machines.

Course illustration
Course illustration

All Rights Reserved.