Lombok注释类的Gradle构建失败

Jos*_*ano 11 intellij-idea gradle lombok jhipster

我有一个JHipster项目,我在build.gradle中为Lombok添加了依赖项:

compile group: 'org.projectlombok', name: 'lombok', version: lombok_version
Run Code Online (Sandbox Code Playgroud)

我为IntelliJ停止了Lombok插件.我在IntelliJ中打开了注释处理,我可以在没有IntelliJ IDE错误的情况下构建,但是当我尝试从命令行构建时,我会遇到构建错误.看起来Gradle没有处理注释,也找不到getter/setter和log声明.该项目也运行没有任何错误.

命令行:

./gradlew build
Run Code Online (Sandbox Code Playgroud)

错误:

/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
Run Code Online (Sandbox Code Playgroud)

错误:

/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
Run Code Online (Sandbox Code Playgroud)

服务类别:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

实体类:

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;

}
Run Code Online (Sandbox Code Playgroud)

我一直试图弄清楚这几个小时,但完全卡住了.任何帮助,将不胜感激.

The*_*ect 17

您需要指定lombok为注释处理器.为此,您需要build.gradleJhipster项目中添加以下内容.

apply plugin: 'net.ltgt.apt'

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

    /** ... */
}
Run Code Online (Sandbox Code Playgroud)

Jhipster使用net.ltgt.gradle:gradle-apt-plugin用于注释处理.

对于IntelliJ设置,Enable annotation Processing应该检查.

更多信息:Project Lombok - android说明


use*_*676 12

我遇到了同样的问题,并在添加到build.gradle时为我工作:

dependencies{

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

}
Run Code Online (Sandbox Code Playgroud)

资源:https : //projectlombok.org/setup/gradle

  • 如果您在测试中使用 lombok,则需要添加: `testCompileOnly 'org.projectlombok:lombok:1.18.8'` 和 `testAnnotationProcessor 'org.projectlombok:lombok:1.18.8'` (4认同)
  • 与 gradle 5.6.2 配合良好,`annotationProcessor 'org.projectlombok:lombok:1.18.8'` 是强制的 (3认同)

Kha*_*aen 11

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'
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*Yue 8

对于新用户,请考虑使用 gradle 插件:

plugins {
  id "io.freefair.lombok" version "5.3.3.3"
}
Run Code Online (Sandbox Code Playgroud)

那么你不需要compileOnly或者annotationProcessor

参考: https: //plugins.gradle.org/plugin/io.freefair.lombok