Eclipse Maven build succeeds, but project still reports compilation errors

Viewed 149

I'm using Eclipse 2021-3 (4.19.0) on Mac OS Big Sur. I have imported a Maven project (https://github.com/gavinklfong/stream-api-exercises) and have verified the right JDK is selected (v 11)

enter image description here

When I right-click on my project, select "Run" -> "Maven" and specify a goal of install, everythign reports successful in the console output. However, I see compilation errors next to certain lines

enter image description here

One for example, complains "The method getCategory is undefined for the type Product." Below is the Product class

package space.gavinklfong.demo.streamapi.models;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.With;

@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
public class Product {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    private String category;
    
    @With
    private Double price;
    
    @ManyToMany(mappedBy = "products")
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    private Set<Order> orders;
    
}

How do I configure my Maven project in Eclipse to use the same compiler/build path that is being used when I execute a Run -> Maven build command from Eclipse?

3 Answers

Lombok is an Annotation preprocessor. It is not enough to add it as an dependency (well for an compilation through maven it is).

After you added Lombok to your dependencies, it is listed below Maven Dependencies in your hierarchy. Double click the lombok.jar and an funny installer opens (given that you've added Java to your $PATH). Install Lombok for eclipse, it takes usually less than a second (no apparent confirmation pops up). Just close the installer and it should work right away.

In some cases you might want to clear the cache.

Assuming that you've set up Eclipse already, I think you'd also need to setup Maven.
When looking at the relevant pom.xml, it's unclear which version it is; this should be:

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

See the change log; when the version is too old, JDK11 won't be compatible.

Annotation processing also needs to be enabled:

screenshot

When the IDE knows about the generated sources, it should know about the getters & setters. The "Enable processing in editor" checkbox likely isn't enabled. Run Project > Clean, once enabled.

Related