Gradle compilation error with lombok after merge

n00bst3r :

After switching branch the compilation failed during my build. Here a snippet:

    $ ./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);

This is the how my build.gradle looks like:

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

It's weird, because before switching the branch and before merging everthing worked fine. Has anyone an idea what the reason could be for this compilation problem? Gradle version is 6.2.2`enter code here. I also tried to delete the cache under C:\Users\user.gradle\caches\6.2.2
But also this didn't help.

Here the classes that aren't recognized:

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

And the Util Class:

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;
    }
}
Jan Rieke :

Static imports of single members ("non-star form") often cause problems when importing lombok-processed classes.

The workaround is to use the star (*) form of the static import:

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

The lombok documentation for @UtilityClass also mentions this in the small print:

Due to a peculiar way javac processes static imports, trying to do a non-star static import of any of the members of a @UtilityClass won't work. Either use a star static import: import static TypeMarkedWithUtilityClass.*; or don't statically import any of the members.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=347974&siteId=1