【问题标题】:Using lombok with gradle and spring-boot将 lombok 与 gradle 和 spring-boot 一起使用
【发布时间】:2017-03-21 10:19:09
【问题描述】:

我正在尝试使用 lombok 构建一个项目,这就是我所拥有的依赖项。

dependencies {
   compile("org.springframework.boot:spring-boot-starter-thymeleaf")
   compile("org.springframework.social:spring-social-facebook")
   compile("org.springframework.social:spring-social-twitter")
   testCompile("org.springframework.boot:spring-boot-starter-test")
   testCompile("junit:junit")
   compile("org.springframework.boot:spring-boot-devtools")
   compile("org.springframework.boot:spring-boot-starter-data-jpa")
   compile("mysql:mysql-connector-java")
   compileOnly("org.projectlombok:lombok:1.16.10")
}

我能够包含注释,并且我在编辑器中包含了 lombok。我什至可以使用 lombok 编译代码并对 lombok 生成的方法进行校准。

这是我的实体:

@Data
@Entity
@Table(name = "TEA_USER", uniqueConstraints = {
    @UniqueConstraint(columnNames = { "USR_EMAIL" }),
    @UniqueConstraint(columnNames = { "USR_NAME" })
})
public class User {


   @NotNull
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name="USR_ID")
   private long id;

   @NotNull
   @Column(name="USR_FNAME")
   private String firstName;

   @NotNull
   @Column(name="USR_LNAME")
   private String lastName;


   @NotNull
   @Min(5)
   @Max(30)
   @Column(name="USR_NAME")
   private String username;

   @Column(name="USR_EMAIL")
   private String email;

   @Min(8)
   @NotNull
   @Column(name="USR_PASSWORD")
   private String password;
}

这是一个编译良好的函数:

@PostMapping("/registration/register")
public String doRegister (@ModelAttribute @Valid User user, BindingResult result){
    user.getEmail();
    System.out.println(user.getFirstName());
    if (result.hasErrors()) {
         return "register/customRegister";
    }
    this.userRepository.save(user);
    return "register/customRegistered";
}

但是当我运行 bootRun 并尝试访问功能时,这是我得到的异常:

org.springframework.beans.NotReadablePropertyException: Invalid property 'firstName' of bean class [com.lucasfrossard.entities.User]: Bean property 'firstName' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

但是,如果我手动包含 setter 和 getter,这可以正常工作。我不明白发生了什么以及如何解决它。有什么想法吗?

【问题讨论】:

    标签: java spring-boot gradle lombok


    【解决方案1】:

    如果您使用 Eclipse 或其分支之一(我使用的是 Spring Tool Suite 4.5.1.RELEASE,- 相同的步骤适用于另一个其他版本),您需要做的就是:

    • build.gradle:

      dependencies {
        compileOnly 'org.projectlombok:lombok'  
        annotationProcessor 'org.projectlombok:lombok'
      }
      
    • 然后,右键单击您的项目 > Gradle > 刷新 Gradle 项目。 lombok-"version".jar出现在您项目的项目和外部依赖项

    • 右键单击该lombok-"version".jar > 运行方式 > Java 应用程序(类似于双击实际 jar 或在命令行上运行 java -jar lombok-"version".jar。)

    • 将出现一个 GUI,按照所有说明进行操作,其中一件事就是将 lombok.jar 复制到 IDE 的根文件夹。

    更新:

    1 年多后,使用4.11.1.RELEASE,我仍在回顾我为安装 Lombok 所写的内容。

    【讨论】:

      【解决方案2】:

      使用最新的 Lombok 1.18 很简单。 io.franzbecker.gradle-lombok 插件不是必需的。我的项目中的示例依赖项:

      dependencies {
          implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
          implementation "org.springframework.social:spring-social-facebook"
          implementation "org.springframework.social:spring-social-twitter"
          implementation "org.springframework.boot:spring-boot-starter-data-jpa"
      
          testImplementation "org.springframework.boot:spring-boot-starter-test"
      
          runtimeClasspath "org.springframework.boot:spring-boot-devtools"
          runtime "mysql:mysql-connector-java"
      
          // https://projectlombok.org
          compileOnly 'org.projectlombok:lombok:1.18.4'
          annotationProcessor 'org.projectlombok:lombok:1.18.4'
      }
      

      其他建议:

      1. testCompile("junit:junit") 不是必需的,因为 spring-boot-starter-test “Starter” contains JUnit
      2. 使用implementationapi 配置而不是compile(因为Gradle 3.4)。 How do I choose the right one?
      3. 不要将compile 配置用于devtools。来自Developer Tools 指南的提示:

        在 Maven 中将依赖项标记为可选或在 Gradle 中使用自定义 developmentOnly 配置(如上所示)是防止开发工具被传递到使用您项目的其他模块的最佳实践。

      4. 如果您使用 IntelliJ IDEA,请确保已安装 Lombok plugin 并启用 Annotation Processing。为了让新手更容易进行初始项目设置,您还可以将 Lombok 插件指​​定为 required

      【讨论】:

      • 谢谢。 annotationProcessor 'org.projectlombok:lombok' 为我解决了这个问题
      【解决方案3】:

      经过一段时间的努力,我发现这个插件可以解决问题:

      https://github.com/franzbecker/gradle-lombok

      我的 gradle 文件看起来像这样:

      buildscript {
          repositories {
              maven { url "https://repo.spring.io/libs-milestone" }
              maven { url "https://plugins.gradle.org/m2/" }
          }
          dependencies {
              classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
              classpath 'org.springframework:springloaded:1.2.6.RELEASE'
          }
      }
      
      plugins {
          id 'io.franzbecker.gradle-lombok' version '1.8'
          id 'java'
      }
      
      
      
      apply plugin: 'java'
      apply plugin: 'eclipse'
      apply plugin: 'idea'
      apply plugin: 'spring-boot'
      
      
      
      jar {
          baseName = 'gs-accessing-facebook'
          version =  '0.1.0'
      }
      
      repositories {
          mavenCentral()
          maven { url "https://repo.spring.io/libs-milestone" }
      }
      
      sourceCompatibility = 1.8
      targetCompatibility = 1.8
      
      dependencies {
          // compile("org.projectlombok:lombok:1.16.10")
          compile("org.springframework.boot:spring-boot-starter-thymeleaf")
          compile("org.springframework.social:spring-social-facebook")
          compile("org.springframework.social:spring-social-twitter")
          testCompile("org.springframework.boot:spring-boot-starter-test")
          testCompile("junit:junit")
          compile("org.springframework.boot:spring-boot-devtools")
          compile("org.springframework.boot:spring-boot-starter-data-jpa")
          compile("mysql:mysql-connector-java")
      }
      
      idea {
          module {
              inheritOutputDirs = false
              outputDir = file("$buildDir/classes/main/")
          }
      }
      
      bootRun {
          addResources = true
          jvmArgs "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005"
      }
      

      我之前遇到过这个插件,但我做错了,它没有工作。我很高兴它现在起作用了。

      谢谢!

      【讨论】:

      • 我们也有这个设置,不需要额外的插件。您使用的是哪个 IDE?我猜你应该为你的IDE安装插件? projectlombok.org/download.html
      • 这个插件对我帮助很大,谢谢!你能解释一下为什么会这样吗?在 maven 项目中,使用 lombok 添加 就足够了,但是在 gradle 中,我们需要一些奇怪的插件来代替。
      • 可能是插件自动激活java编译器的注解处理。据说它与代码的实际运行无关,lombok 只做编译类型的事情。但是,我不确定在最新版本的 gradle/lombok 中是否真的需要使用该插件
      • 已确认,此处不需要插件,仅包含 compileOnly 'org.projectlombok:lombok:1.18.2' 作为依赖项。在这个项目中使用 gradle 4.5.1。
      猜你喜欢
      • 2015-05-15
      • 1970-01-01
      • 2015-12-20
      • 2020-09-21
      • 1970-01-01
      • 2015-11-03
      • 2018-08-25
      • 2020-03-25
      • 2021-01-02
      相关资源
      最近更新 更多