使用lombok与gradle和弹簧引导

问题描述:

我正在尝试与lombok建立一个项目,这是我作为依赖项。使用lombok与gradle和弹簧引导

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") 
} 

我能够包括anotations,我已经包含在龙目岛的编辑。我甚至可以使用lombok编译代码并对lombok生成的方法进行cal编译。

这是我的实体:

@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,我尝试访问的funcionality这是例外,我得到:

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和getters,这工作正常。我不知道发生了什么,以及如何解决它。任何想法?

与此挣扎了一会儿后,我发现这个插件会做的伎俩:

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

我曾经碰到过这个插件之前,但我做了错误的事情,但没有奏效。我很高兴它现在工作。

谢谢!

+0

我们也有这个设置,不需要额外的插件。你在使用哪个IDE?你应该为你安装插件IDE我猜? https://projectlombok.org/download.html – questionare

+0

这个插件帮了我很多,谢谢! 你能解释为什么会发生?在Maven项目中,添加一个与lombok就足够了,但在这里,在gradle中,我们需要一些奇怪的插件而不是那个。 –