Java 将 lombok 与 gradle 和 spring-boot 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40475910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using lombok with gradle and spring-boot
提问by Lucas
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. I am even able to compile a code using lombok and making a cal to a method generated by lombok.
我能够包含注释,并且我在编辑器中包含了 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. I don't get whats going on and how to fix it. Any idea?
但是,如果我手动包含 setter 和 getter,这可以正常工作。我不明白发生了什么以及如何解决它。任何的想法?
回答by Lucas
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!
谢谢!
回答by naXa
With the latest Lombok 1.18 it's simple. io.franzbecker.gradle-lombokplugin is not required. Example dependencies from my project:
使用最新的 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'
}
Other suggestions:
其他建议:
testCompile("junit:junit")is not required because thespring-boot-starter-test“Starter” contains JUnit.- Use
implementationorapiconfiguration instead ofcompile(since Gradle 3.4). How do I choose the right one? Do not use
compileconfiguration fordevtools. A tip from Developer Toolsguide:Flagging the dependency as optional in Maven or using a custom
developmentOnlyconfiguration in Gradle (as shown above) is a best practice that prevents devtools from being transitively applied to other modules that use your project.If you use IntelliJ IDEA, make sure you have Lombok plugininstalled and Annotation Processingenabled. To make an initial project setup easier for newcomers you can also specify the Lombok plugin as required.
testCompile("junit:junit")不是必需的,因为spring-boot-starter-test“Starter”包含 JUnit。- 使用
implementation或api配置代替compile(自Gradle 3.4 起)。我该如何选择合适的? 不要将
compile配置用于devtools. 来自开发者工具指南的提示:在 Maven 中将依赖项标记为可选或
developmentOnly在 Gradle 中使用自定义配置(如上所示)是防止 devtools 被传递地应用于使用您的项目的其他模块的最佳实践。如果您使用 IntelliJ IDEA,请确保已安装Lombok 插件并启用注释处理。为了让新手更容易进行初始项目设置,您还可以根据需要指定 Lombok 插件。

