java 合并后lombok的Gradle编译错误

new9mtju  于 2023-08-01  发布在  Java
关注(0)|答案(1)|浏览(156)

在切换分支后,编译在我的构建过程中失败。这里是一个片段:

$ ./gradlew clean build
Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details

    > Task :compileJava
    C:\Users\xxx\Documents\Repositories\Homepage\Home\src\main\java\com\xxx\project\bootstrap\DatabaseLoader.java:19: error: cannot find symbol
    import static com.xxx.homepage.bootstrap.DataSetupUtil.getUserObject;
    ^
      symbol:   static getUserObject
      location: class DataSetupUtil
    C:\Users\xxx\Documents\Repositories\Homepage\Home\src\main\java\com\xxx\project\bootstrap\DataSetupUtil.java:17: error: cannot find symbol
            user.setEmail(email);
                ^
      symbol:   method setEmail(@lombok.NonNull String)
      location: variable user of type User
    C:\Users\xxx\Documents\Repositories\Homepage\Home\src\main\java\com\xxx\project\bootstrap\DataSetupUtil.java:18: error: cannot find symbol
            user.setUsername(username);
                ^
      symbol:   method setUsername(@lombok.NonNull String)
      location: variable user of type User
    C:\Users\xxx\Documents\Repositories\Homepage\Home\src\main\java\com\xxx\project\bootstrap\DataSetupUtil.java:19: error: cannot find symbol
            user.setName(name);
                ^
      symbol:   method setName(@lombok.NonNull String)
      location: variable user of type User
    C:\Users\xxx\Documents\Repositories\Homepage\Home\src\main\java\com\xxx\project\bootstrap\DataSetupUtil.java:20: error: cannot find symbol
            user.setFirstname(firstname);

字符串
这就是我的build.gradle看起来像:

plugins {
    id 'org.springframework.boot' version '2.2.5.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.xxx'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    repositories {
        maven {
            url "${nexusUrl}/content/groups/public/"
            credentials {
                username "$nexusUsername"
                password "$nexusPassword"
            }
        }
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'

    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
    compile group: 'com.google.guava', name: 'guava', version: '28.2-jre'

    compile group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5', version: '3.0.4.RELEASE'
    runtimeOnly 'com.h2database:h2'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.security:spring-security-test'
}


这很奇怪,因为在切换分支和合并之前,一切都很正常。有没有人知道这个编译问题的原因是什么?Gradle版本是6.2.2`。我还尝试删除C:\Users\user.gradle\caches\6.2.2下该高速缓存
但这也无济于事。
下面是未被识别的类:

import lombok.*;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

@Entity
@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class User implements UserDetails {

    @Id
    @GeneratedValue
    private Long id;

    @Version
    private Integer version;

    @NonNull
    @Size(min = 8, max = 20)
    @Column(nullable = false, unique = true, name="USERNAME")
    private String username;

    @NonNull
    @Size(min = 8, max = 30)
    @Column(nullable = false, unique = true, name="EMAIL")
    private String email;

    @NonNull
    @Column(length = 100, name = "PASSWORD")
    private String password;

    @NonNull
    @Column(nullable = false, name="ENABLED")
    private boolean enabled;

    @Size(min = 3, max = 20)
    @Column(name="NAME")
    private String name;

    @Size(min = 3, max = 20)
    @Column(name="FIRST_NAME")
    private String firstname;

    @Column(name="DATE_OF_BIRTH")
    private Date dateOfBirth;

    @Size(min = 3, max = 20)
    @Column(name="CITY")
    private String city;

    @Size(min = 3, max = 30)
    @Column(name = "COUNTRY")
    private String country;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "users_roles",
            joinColumns = @JoinColumn(name = "user_id",referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id",referencedColumnName = "id")
    )
    private Set<Role> roles = new HashSet<>();

    public void addRole(Role role) {
        roles.add(role);
    }

    public void addRoles(Set<Role> roles) {
        roles.forEach(this::addRole);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //map roles to GrantedAuthorities:
        return roles.stream().map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
}


Util类:

import com.rajic.homepage.domain.Role;
import com.rajic.homepage.domain.User;
import lombok.NonNull;
import lombok.experimental.UtilityClass;

import java.util.Date;

@UtilityClass
public class DataSetupUtil {

    public User getUserObject(@NonNull String email, @NonNull String username,
                              @NonNull String password, @NonNull String name, @NonNull String firstname,
                              @NonNull String city, @NonNull String country, @NonNull Date dateOfBirth, Role...roles) {
        User user = new User();
        user.setEmail(email);
        user.setUsername(username);
        user.setName(name);
        user.setFirstname(firstname);
        user.setPassword(password);
        user.setCity(city);
        user.setCountry(country);
        user.setDateOfBirth(dateOfBirth);
        user.setEnabled(true);
        for (int i = 0; i < roles.length; i++) {
            user.addRole(roles[i]);
        }
        return user;
    }
}

nxowjjhe

nxowjjhe1#

当导入lombok处理的类时,单个成员(“非星型形式”)的静态导入通常会导致问题。
解决方法是使用星星(*)形式的静态导入:

import static com.xxx.homepage.bootstrap.DataSetupUtil.*;

字符串
@UtilityClass的lombok文档也在小字中提到了这一点:
由于javac处理静态导入的特殊方式,尝试对@UtilityClass的任何成员执行非星形静态导入都是不起作用的。或者使用星星静态导入:import static TypeMarkedWithUtilityClass.*;或不静态导入任何成员。

相关问题