Project Lombok is a Java library that helps reduce
Lombok can easily be added to the project as one of the dependencies.
If the project is a Gradle project, we can add the following two lines to the dependencies section of the build.gradle file:
compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
If the project is a Maven project, we can add the following lines to the dependencies section of the pom.xml file:
<dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.22</version><scope>provided</scope></dependency></dependencies>
@Data annotationThe @Data annotation is equivalent to the combination of the following annotations:
@Getter@Setter@RequiredArgsConstructor@ToString@EqualsAndHashCodeWe can replace annotating a class with the annotations that are listed above and a single @Data annotation. The @Data annotation does the following work:
toString() method implementation.equals() and hashCode() implementations, involving the fields of class.@NonNull, in order to ensure that the field is never null.<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>Data annotation.Person class with age and name as the fields of the class. This class is annotated with the @Data annotation.Person class. The value for the name field is passed, because it’s a final field and the @Data annotation includes the functionality of the @RequiredArgsConstructor annotation.age field of the person object, using the setter.age value of the person object, using the getter for the age field.name value of the person object, using the getter for the name field.toString method.