Gradle build fails on Lombok annotated classes

2020-08-09 07:38发布

问题:

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.

回答1:

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



回答2:

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



回答3:

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