Project Lombok provides annotations which reduce boilerplate code in Java applications (see Listing 1). The Netbeans IDE supports it almost out-of-the-box. You just have to enable the option „Project Properties > Build > Compiling > Enable Annotation Processing“. However the current version 1.16.8 is not working using Netbeans 8.1 and Maven.
1 2 3 4 5 6 7 8 9 10 11 12 |
import lombok.Builder; import lombok.Data; @Data @Builder public class Entity { private int id; private String name; } |
Using Lombok Version 1.14 …
If you’re using Lombok 1.14, Netbeans and Maven, everything works find. Just add the following dependency to your pom.xml:
1 2 3 4 5 |
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.8</version> </dependency> |
… using Version 1.16…
If you’re not using Maven, everything is still fine! Just add version 1.16 to your project dependencies using „Project Properties > Libraries“. Netbeans uses its default build.xml and your have to manage your project dependencies on your own. Have fun!
One way to avoid this is simply using version 1.14. It provides the very usefull @Getter, @Setter and @Data annotations and a lot of other stuff. One of our customers however uses the @Builder annotation heavily throughout the code. In a plain-old Netbeans project with Ant. So…
- @Builder is stable since Lombok 1.16
- It deminishes A LOT of boilerplate code
- We need to stick to version 1.16
Now a problem occures when I wanted to build the project with Maven (or in Jamaican: „a situation occurs“). Integrating Maven makes of course also a lot of sense if you don’t want to update your Spring dependencies by hand 🙂
However Maven fails with an error message like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
--- maven-compiler-plugin:2.3.2:compile (default-compile) @ lomboktest --- Compiling 2 source files to C:\Users\dawi\Documents\NetBeansProjects\lomboktest\target\classes ------------------------------------------------------------- COMPILATION ERROR : ------------------------------------------------------------- com/illucit/lomboktest/Main.java:[8,25] error: cannot find symbol com/illucit/lomboktest/Main.java:[9,28] error: cannot find symbol 2 errors ------------------------------------------------------------- ------------------------------------------------------------------------ BUILD FAILURE ------------------------------------------------------------------------ |
Delombok your source code
Delombok takes the annotated classes and replaces them with the actually generated code. This can be useful for Javadoc generation or the integration into the Google Widget Toolkit.
The solution is now to integrate the delombok Maven plugin into the Maven lifecycle:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<build> <plugins> <plugin> <groupId>org.projectlombok</groupId> <artifactId>lombok-maven-plugin</artifactId> <version>1.16.8.0</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>delombok</goal> </goals> </execution> </executions> </plugin> </plugins> </build> |
The delomboked code is then placed in target/generated-sources/delombok.
So when you’re modifying the entities, you have to build your project twice. The first run is delomboking the sources by running maven with lombok:delombok. Then you can use the generated methods:
1 2 3 4 5 6 |
public class Main { public static void main(String[] args) { Entity e = Entity.builder().name("Name").build(); System.out.println(e.getName()); } } |
If you just want to compile an existing project, building once is enough as the plugin runs in the generate-sources phase.
Summary
We got Lombok 1.16 working with Netbeans 8.1 and Maven by simply adding one Maven plugin. Check out the sources of the sample project on Github!
Hi
The „enable annotation processing“ is only available for the non-Maven projects in my workspace. I am running Netbeans 8.1 on a Macbook pro. Is the option to be found somewhere else for Maven projects?
Hey,
No, the option does not exist for Maven projects. But for Lombok you don’t need to check such a flag for a Maven project. As long as Lombok is a dependency of your project, the annotations will be processed automatically. Which Version of Lombok are you using?
Best
Daniel