Using Lombok
March 22, 2022
Background
Lombok is a java code generation tool, it can remove a lot of boilerplate code.
Installing Lombok
To configure intellji, there are two steps, enable the relevant IDE plugins and configurations and add the lombok maven dependency.
Enable lombok in IDE
Intellji versions above 2020.3 come with lombok pre-installed so it should work as is. You can verify (or install for older versions) the configuration by going to the Plugins
section and searching for lombok
. Make sure the lombok plugin is installed and verify both Enable annotation processing
and Obtain processors from project classpath
are checked.
Add lombok to classpath via maven
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.6</version><scope>provided</scope></dependency>
Tips
@Data
You can use this to generate boilerplate code (getters, setters, etc..)
@EqualsAndHashCode
Overrides equals and hashcode (for object equality and such). Can also add an exclude parameter for fields that should be excluded from equality check. Exclude has been helpful to prevent infinite cycles.
@Log4j2
Generates a logger field (using format for your respective logger).
@Builder
Generates builder implementation for object. Builder pattern looks like so.
Object.builder().field(value).build()
@Jacksonized
Configures the generated builder class to be used by Jackson’s deserialization - more info