简体   繁体   中英

Using lombok with gradle and spring-boot

I am trying to build a project with lombok and this is what I have as dependencie.

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. I am even able to compile a code using lombok and making a cal to a method generated by 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:

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. I don't get whats going on and how to fix it. Any idea?

With the latest Lombok 1.18 it's simple. io.franzbecker.gradle-lombok plugin is not required. 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 .
  2. Use implementation or api configuration instead of compile (since Gradle 3.4 ). How do I choose the right one?
  3. Do not use compile configuration for devtools . 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.

  4. If you use IntelliJ IDEA, make sure you have Lombok plugin installed and Annotation Processing enabled. To make an initial project setup easier for newcomers you can also specify the Lombok plugin as required .

After struggling with this for a while I found this plugin would do the trick:

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

My gradle file just looks like this:

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:

  • In build.gradle , you ONLY need this configuration:

     dependencies { compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' }
  • Then, right-click on your project > Gradle > Refresh Gradle Project. The lombok-"version".jar will appear inside your project's Project and External Dependencies

  • 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.)

  • 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.

  • The only other thing you will need to do, is to add that lombok.jar to your project build/class path.



That's it, cheers!

PS For more information, you can visit this page , although it's not necessary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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