How to exclude property from Lombok builder?

Solution 1:

Yes, you can place @Builder on a constructor or static (factory) method, containing just the fields you want.

Disclosure: I am a Lombok developer.

Solution 2:

Alternatively, I found out that marking a field as final, static or static final instructs @Builder to ignore this field.

@Builder
public class MyClass {
   private String myField;

   private final String excludeThisField = "bar";
}

Lombok 1.16.10

Solution 3:

Create the builder in code and add a private setter for your property.

@Builder
XYZClientWrapper{
    String name;
    String domain;
    XYZClient client;

    public static class XYZClientWrapperBuilder {
        private XYZClientWrapperBuilder client(XYZClient client) { return this; }
    }
}

Solution 4:

Here is my preferred solution. With that, you can create your field client at the end and have it depending on other fields that previously set by the builder.

XYZClientWrapper{
    String name;
    String domain;
    XYZClient client;
    
    @Builder
    public XYZClientWrapper(String name, String domain) {
        this.name = name;
        this.domain = domain;
        this.client = calculateClient();
    }
}