首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将lombok与gradle和spring-boot结合使用

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

Stack Overflow用户
提问于 2016-11-08 06:34:32
回答 3查看 57.4K关注 0票数 27

我正在尝试建立一个与龙目岛的项目,这是我有依赖。

代码语言:javascript
复制
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编译代码,并调用lombok生成的方法。

这是我的实体:

代码语言:javascript
复制
@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;
}

这是一个编译得很好的函数:

代码语言:javascript
复制
@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并尝试访问函数时,这是我得到的异常:

代码语言:javascript
复制
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,就可以很好地工作。我不知道发生了什么,也不知道如何修复它。有什么想法吗?

EN

回答 3

Stack Overflow用户

发布于 2018-11-09 00:47:40

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

代码语言:javascript
复制
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")不是必需的,因为contains JUnit.
  2. Use implementationapi的配置是spring-boot-starter-test“Starter”,而不是compile (从Gradle 3.4开始)。How do I choose the right one?
  3. Do不对devtools使用compile配置。来自Developer Tools指南的提示:

在Maven中将依赖项标记为可选或在Gradle中使用自定义developmentOnly配置(如上所示)是一种最佳实践,它可以防止将devtool过渡到使用您的项目的其他模块。

  1. 如果您使用IntelliJ IDEA,请确保已安装Lombok plugin并启用了Annotation Processing。为了使初学者更容易进行初始项目设置,您还可以将Lombok插件指定为required.
票数 34
EN

Stack Overflow用户

发布于 2016-11-08 06:53:35

在与此斗争了一段时间后,我发现这个插件可以做到这一点:

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

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

代码语言:javascript
复制
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"
}

我以前遇到过这个插件,但我做错了什么,它不能工作。我很高兴它现在起作用了。

谢谢!

票数 15
EN

Stack Overflow用户

发布于 2020-06-22 21:48:56

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

build.gradle中的

依赖项{ compileOnly 'org.projectlombok:lombok‘annotationProcessor 'org.projectlombok:lombok’}

  • 然后,右键单击您的项目> Gradle >刷新Gradle项目。lombok-"version".jar将显示在项目的项目和外部Dependencies

  • 右键单击lombok-"version".jar > Run As > Java Application (类似于双击实际jar或在命令行上运行java -jar lombok-"version".jar )。

  • 将出现一个图形用户界面,按照所有说明进行操作,其中一项操作是将lombok.jar复制到您的集成开发环境的根文件夹中。

更新:

1Lombok之后,使用4.11.1.RELEASE,我仍然引用我为安装year+而编写的内容。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40475910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档