简体   繁体   English

将 lombok 与 gradle 和 spring-boot 结合使用

[英]Using lombok with gradle and spring-boot

I am trying to build a project with lombok and this is what I have as dependencie.我正在尝试使用 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")
}

I am able to include the anotations, and I have included lombok in the editor.我能够包含注释,并且我在编辑器中包含了 lombok。 I am even able to compile a code using lombok and making a cal to a method generated by lombok.我什至能够使用 lombok 编译代码并对 lombok 生成的方法进行校准。

This is my entity:这是我的实体:

@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;
}

And this is a function that compiles fine:这是一个编译良好的函数:

@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";
}

But when I run bootRun and I try to access the funcionality this is the Exception I get:但是当我运行 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?

But, if I manually include the setter and getters, this works fine.但是,如果我手动包含 setter 和 getter,这可以正常工作。 I don't get whats going on and how to fix it.我不明白发生了什么以及如何解决它。 Any idea?有什么想法吗?

With the latest Lombok 1.18 it's simple.使用最新的 Lombok 1.18,这很简单。 io.franzbecker.gradle-lombok plugin is not required. io.franzbecker.gradle-lombok插件不是必需的。 Example dependencies from my project:我的项目中的示例依赖项:

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'
}

Other suggestions:其他建议:

  1. testCompile("junit:junit") is not required because the spring-boot-starter-test “Starter” contains JUnit . testCompile("junit:junit")不是必需的,因为spring-boot-starter-test “Starter” 包含 JUnit
  2. Use implementation or api configuration instead of compile (since Gradle 3.4 ).使用implementationapi配置而不是compile (自Gradle 3.4 起)。 How do I choose the right one? 我该如何选择合适的?
  3. Do not use compile configuration for devtools .不要对devtools使用compile配置。 A tip from Developer Tools guide:来自开发者工具指南的提示:

    Flagging the dependency as optional in Maven or using a custom developmentOnly configuration in Gradle (as shown above) is a best practice that prevents devtools from being transitively applied to other modules that use your project.在 Maven 中将依赖项标记为可选或在 Gradle 中使用自定义developmentOnly配置(如上所示)是一种最佳实践,可以防止 devtools 被传递地应用于使用您项目的其他模块。

  4. If you use IntelliJ IDEA, make sure you have Lombok plugin installed and Annotation Processing enabled.如果您使用 IntelliJ IDEA,请确保已安装Lombok 插件并启用注释处理 To make an initial project setup easier for newcomers you can also specify the Lombok plugin as required .为了让新手更容易进行初始项目设置,您还可以根据需要指定 Lombok 插件。

After struggling with this for a while I found this plugin would do the trick:在为此苦苦挣扎了一段时间后,我发现这个插件可以解决问题:

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

My gradle file just looks like this:我的 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"
}

I had come across this plugin before but I did something wrong and it didn't work.我以前遇到过这个插件,但我做错了,它没有用。 I am glad it worked now.我很高兴它现在起作用了。

Thanks!谢谢!

If you are using Eclipse or one of its offshoots(I am using Spring Tool Suite 4.5.1.RELEASE , - the same steps apply for another other release), all that you need to do is:如果您使用的是Eclipse或其一个分支(我使用的是 Spring Tool Suite 4.5.1.RELEASE , - 同样的步骤适用于另一个其他版本),您需要做的就是:

  • In build.gradle , you ONLY need this configuration:build.gradle ,你需要这样的配置:

     dependencies { compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' }
  • Then, right-click on your project > Gradle > Refresh Gradle Project.然后,右键单击您的项目 > Gradle > 刷新 Gradle 项目。 The lombok-"version".jar will appear inside your project's Project and External Dependencies lombok-"version".jar出现在您项目的项目和外部依赖项中

  • Right-click on that lombok-"version".jar > Run As > Java Application (similar to double-clicking on the actual jar or running java -jar lombok-"version".jar on the command line.)右键单击lombok-"version".jar > Run As > Java Application(类似于双击实际 jar 或在命令行上运行java -jar lombok-"version".jar 。)

  • A GUI will appear, follow all the instructions and one of the thing it does is, it copies lombok.jar to your IDE's root folder.将出现一个 GUI,按照所有说明进行操作,其中一件事就是将lombok.jar复制到 IDE 的根文件夹中。

  • The only other thing you will need to do, is to add that lombok.jar to your project build/class path.您唯一需要做的另一件事是将该lombok.jar添加到您的项目构建/类路径中。



That's it, cheers!就是这样,干杯!

PS For more information, you can visit this page , although it's not necessary. PS 有关更多信息,您可以访问此页面,尽管它不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM