Using Project Lombok annotations in your POJOs will generates boilerplate code automatically at compile time. Delombok with maven plugin configuration provides a way to check how the compiled code looks like, because the actual code available in .class
files, which is not directly visible for the developer. Delomboking helps in learning phase to understand how your compiled classes looks like and also helps to get back your actual POJOs if would like to move out of it later.
1. Maven configuration for delombok
1.1. Following is the complete maven configuration in pom.xml. You need to configure lombok-maven-plugin
and make sure you already installed lombok setup for your IDE. To Setup in Eclipse or in Spring Tool Suite refer to our Lombok Maven example setup with Eclipse.
<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>com.javabydeveloper</groupId> <artifactId>delombok-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>delombok-maven-example</name> <url>http://maven.apache.org</url> <properties> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <lombokSource>${project.basedir}/src/main/java/com/javabydeveloper/lombok</lombokSource> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> </dependencies> <profiles> <profile> <id>dlombok</id> <build> <plugins> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.18.12.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> <configuration> <sourceDirectory>${lombokSource}</sourceDirectory> <verbose>true</verbose> <formatPreferences> <generateDelombokComment>skip</generateDelombokComment> <javaLangAsFQN>skip</javaLangAsFQN> <suppressWarnings>skip</suppressWarnings> </formatPreferences> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
1.2. lombokSource
configuration property to provide the location where your lombok related classes. It always good to keep under separate package all Lombok related classes, it’s easier to check and maintain delomboked code.
1.3. lombok-maven-plugin
is for delomboking your source code in generate-sources
phase and we would need to use delombok goal name to generate delomboking code for your lomboked classes.
1.4. We need to configure and run lombok-maven-plugin
in a separate profile otherwise while generating sources main build will fail .
2. Delombok maven example Project Structure
Following is the project structure used in example application for delomboking code with maven.
3. Generate Delombok code
3.1. To generate delombok code with above configuration, run the delombok
goal with profile dlombok
. Following is the maven command.
$mvn clean lombok:delombok -Pdlombok
3.2. Console Results :
[INFO] Scanning for projects... [INFO] [INFO] ---------------< com.javabydeveloper:delombok-tutorial >---------------- [INFO] Building delombok-maven-example 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ delombok-tutorial --- [INFO] Deleting \Workspaces\Lombok\delombok-maven-example\target [INFO] [INFO] --- lombok-maven-plugin:1.18.12.0:delombok (default-cli) @ delombok-tutorial --- File: \Workspaces\Lombok\delombok-maven-example\src\main\java\com\javabydeveloper\lombok\data\User.java [delomboked] File: \Workspaces\Lombok\delombok-maven-example\src\main\java\com\javabydeveloper\lombok\nonnull\LombokNonNullDemo1.java [delomboked] File: \Workspaces\Lombok\delombok-maven-example\src\main\java\com\javabydeveloper\lombok\settergetter\User.java [delomboked] File: \Workspaces\Lombok\delombok-maven-example\src\main\java\com\javabydeveloper\lombok\User.java [delomboked] [INFO] Delombok complete. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.810 s [INFO] Finished at: 2020-05-16T13:01:10+08:00 [INFO] ------------------------------------------------------------------------
Build will be success and you can see delomboked code in “generated-sources” directory under “target” folder.
4. Example DeLomboked code
4.1. Lomboked User.java
To know about @Data annotation you can refer our tutorial Lombok Data annotation Examples.
@Data public class User { private Long id; private String username; }
4.2. DeLomboked User.java
public class User { private Long id; private String username; public User() { } public Long getId() { return this.id; } public String getUsername() { return this.username; } public void setId(final Long id) { this.id = id; } public void setUsername(final String username) { this.username = username; } @Override public boolean equals(final Object o) { if (o == this) return true; if (!(o instanceof User)) return false; final User other = (User) o; if (!other.canEqual((Object) this)) return false; final Object this$id = this.getId(); final Object other$id = other.getId(); if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false; final Object this$username = this.getUsername(); final Object other$username = other.getUsername(); if (this$username == null ? other$username != null : !this$username.equals(other$username)) return false; return true; } protected boolean canEqual(final Object other) { return other instanceof User; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final Object $id = this.getId(); result = result * PRIME + ($id == null ? 43 : $id.hashCode()); final Object $username = this.getUsername(); result = result * PRIME + ($username == null ? 43 : $username.hashCode()); return result; } @Override public String toString() { return "User(id=" + this.getId() + ", username=" + this.getUsername() + ")"; } }
5. Conclusion
In this article we have covered how to delombok your lombok code using maven configuration with an delombok maven example.
You can checkout source code from our github repository.
You might be interested in our other following Lombok Tutorials :
- Lombok @Getters / @Setters
- Lombok @ToString
- Lombok @EqualsAndHashCode
- Lombok @AllArgsConstructor
- Lombok @NoArgsConstructor
- Lombok @RequiredArgsConstructor
- Lombok @NonNull
- Lombok Spring Boot example