49

I have a JHipster project in which I have added dependency for Lombok in build.gradle:

compile group: 'org.projectlombok', name: 'lombok', version: lombok_version

And I have the Lombok plugin stalled for IntelliJ. I've turned on annotation processing in IntelliJ, I can build without errors from the IntelliJ IDE, but when I try to build from the command line I get build errors. It seems like Gradle is not processing the annotations and can't find the getter/setter and log declarations. The project also runs without any kind of errors.

Command Line:

./gradlew build

Errors :

/Users/.../source/v4.0/src/main/java/com/.../service/MyService.java:145: error: cannot find symbol
        log.info("Security Context: " + SecurityUtils.getCurrentUserLogin());
        ^
  symbol:   variable log
  location: class MyService

Error:

/Users/.../source/v4.0/src/main/java/com/.../service/MyService.java:105: error: cannot find symbol
        myClass.setDescription(description);
                        ^
  symbol:   method setDescription(String)
  location: variable myClass of type MyClass

Service Class:

import lombok.extern.slf4j.Slf4j; 
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MyService {      
    public void someMethod(){
        log.debug("Security Context: " + SecurityUtils.getCurrentUserLogin());
        MyClass myCLass = new MyClass();
        myClass.setDescription(description);
    }
}

Entity Class:

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="t_juror_file_update")
@Getter
@Setter
@NoArgsConstructor
public class MyClass {

    private String description;

}

I've been trying to figure this out for hours, but totally stuck. Any help would be appreciated.

Jose Gulisano
  • 1,281
  • 3
  • 11
  • 12
  • Can you post your SecurityUtils class? – Ethan Feb 06 '16 at 23:02
  • Did you also include a dependency on SLF4J? – Roel Spilker Feb 08 '16 at 08:42
  • 1
    Thanks Ethan and Roel for your responses. I added more details to show I get compilation errors on getters/setters and log declarations. I don't think it is specific to the SecureUtils class. I have a compile group: 'org.springframework.boot', name: 'spring-boot-starter-logging' is providing SLF4J. – Jose Gulisano Feb 08 '16 at 15:29

5 Answers5

101

I had the same problem and worked for me when added to build.gradle:

dependencies{

compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'

}

Resource: https://projectlombok.org/setup/gradle

user3170676
  • 1,021
  • 2
  • 7
  • 4
  • 5
    works well with gradle 5.6.2, `annotationProcessor 'org.projectlombok:lombok:1.18.8'` is mandatory – Yanjiong Wang Sep 15 '19 at 09:45
  • 11
    In case you are using lombok in tests you need to add: `testCompileOnly 'org.projectlombok:lombok:1.18.8'` and `testAnnotationProcessor 'org.projectlombok:lombok:1.18.8'` – Ilya Ovesnov Jul 09 '20 at 01:37
  • Works like a charm! I only needed to add the `annotationProcessor 'org.projectlombok:lombok:1.18.24'` part to my existing `implementation 'org.projectlombok:lombok:1.18.24'` though. It's important to note that the version of the version of the annotationProcesser needs to match that of lombok though, or you'll get all sorts of other crazy errors. =^,~'= – Kira Resari Sep 27 '22 at 07:48
  • It actually worked. I checked with gradle 7.6. Thanks – S.P Singh Dec 30 '22 at 12:06
29

You will need to specify lombok as an annotation processor. To do this, You will need to add following to build.gradle in a Jhipster project.

apply plugin: 'net.ltgt.apt'

dependencies {    
    provided "org.projectlombok:lombok:$lombokVersion"
    apt "org.projectlombok:lombok:$lombokVersion"

    /** ... */
}

Jhipster uses net.ltgt.gradle:gradle-apt-plugin for annotation processing.

For IntelliJ setup, Enable annotation Processing should be checked.

More Info: Project Lombok - android instructions

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
10

I use MacBook and Gradle 7.1.1. In the build.gradle file, I have added the following without a plugin then it can build successfully.

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    
    testCompileOnly 'org.projectlombok:lombok:1.18.20'
    testAnnotationProcessor 'org.projectlombok:lombok:1.18.20'
}
KHACHORNCHIT
  • 2,222
  • 23
  • 19
8

For new users, consider using gradle plugin:

plugins {
  id "io.freefair.lombok" version "5.3.3.3"
}

Then you don't need compileOnly or annotationProcessor

Reference: https://plugins.gradle.org/plugin/io.freefair.lombok

Steven Yue
  • 966
  • 1
  • 8
  • 16
1

The same issue i was facing where as ./gradlew clean build were working fine but when i was trying to do gradle clean build the getter and setter methods which is suppose to be generated by lombok where not found at the time of gradle build . As it throw build failed exception for compilation

Earlier i was having grapper wrapper version 3.4.1 and gradle version 5.6.2. So i am able to fix this issue by down grading my gradle version to 4.10.0 where as my gradle wrapper version remain the same i.e 3.4.1

This is the gradle version

Gradle 4.10

Build time: 2018-08-27 18:35:06 UTC Revision: ee3751ed9f2034effc1f0072c2b2ee74b5dce67d

Kotlin DSL: 1.0-rc-3 Kotlin: 1.2.60 Groovy: 2.4.15 Ant: Apache Ant(TM) version 1.9.11 compiled on March 23 2018 JVM: 1.8.0_191 (Oracle Corporation 25.191-b12) OS: Mac OS X 10.14.6 x86_64

This is the gradle wrapper version

Gradle 3.4.1

Build time: 2017-03-03 19:45:41 UTC Revision: 9eb76efdd3d034dc506c719dac2955efb5ff9a93

Groovy: 2.4.7 Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 JVM: 1.8.0_191 (Oracle Corporation 25.191-b12) OS: Mac OS X 10.14.6 x86_64

This works for me

Rahul
  • 11
  • 2