Project Lombok uses a Java Annotation Processor and Java annotations to add boilerplate code to existing classes. The major IDEs also support Lombok in varying forms, making it viable for both command-line and IDE-based work. In this introduction, we’ll begin by creating a project from scratch using a Maven Archetype and then migrate it to use Project Lombok and Slf4j. Then we’ll move on to seeing how it works in IntelliJ and then we’ll migrate the project to use Gradle.
Create a Basic Project
We need a project, since this is about a tool, the any project will do. To avoid much of the work, let’s use a Maven archetype to create a trival project with a single production class and a single test class using JUnit.
First we’ll create a place to put the project, then create the primordial project. For this example, I’ll be working in my account: /Users/schuchert/src. Where I mention directories, remember to replace my directory with yours.
Create the top-level directory for a number of projects
Use a maven archetype to create a basic project:
Verify the basic project works
Now we have basic project that uses Java and JUnit, so it’s time to add Lombok to the project dependences and get things to work at the command line.
Adding Lombok
The project created by the Maven archetype has a simple POM. It has a single dependency on JUnit. We’ll make 3 changes: update the version of JUnit, add a dependency for Lombok, and add a dependency for Slf4j.
Update the pom.xml file with the changed dependencies:
Gently Start: Logging
The project has one production class, App.java. Open that file and update as shown (note, since I’m still working at the command line, here’s a command to edit that file:
Update App.java
Make two updates, add an annotation and update the line that writes to the console:
Verify that the project still builds by re-running the tests:
Note, while the test created by the maven archetype does not directly test what we changed, the code as written will not compile if the annotation processor is not in place. If you are a healthy skeptic like me, you might want to verify this assertion. Verify by removing the dependency on lombok and see if the tests still pass (note use of “clean” is necessary since we modified the pom.xml file but not the Java code):
Restore the dependency and try again last time:
Note that we did not need to use “clean” this time because the last attempt to build failed.
Is anything going on?
What this demonstrates may be trivial/subtle. To make sure you didn’t miss it, consider the production code again:
Notice the line:
How can this code compile? There is not a local variable named log.
There does not appear to be a field called log, yet the code compiles.
What the Lombok annotation processor does is modify the source code to:
Introduce a static field called Log (that’s an Slf4j logger due to the the @Slf4j annotation)
Initialize that field to an Slf4j logger
There are a number of annotations and there are even extensions to Lomboc for even more. However, sticking with just this one, how about working with an IDE?
Getting this to work in IntelliJ Idea
Open the pom.xml in Idea 13.
Run the tests
Starting with a Maven project, Idea will open the project and execute
tests fine. We get the same results using the command line and in the
IDE. While Idea supports this out of the box, there are some irritating
code inspection errors. To fix this, you can install the Lomboc plugin
and those also disappear.
Gradle
What if you are using Gradle instead of Maven? This is a quick set of steps to accomplish the same thing using gradle.
Create a build.gradle file. In the the gettingstarted directory:
Here’s the generated build.gradle (a few blank lines removed):
Run the tests and verify that they pass:
Note: Depending on the version of Java you are using, you might notice some
warnings. In my system I’m using JDK 1.7 but the generated gradle file uses
1.5 as the expected version. To fix this, update to the appropriate version
of Java you are using, which is 1.7 in my case:
Rebuild
One more build should show those warnings disappear. Note the use of –rerun-tasks is necessary since the last build was a success:
Gradle and Idea 13
Idea support for gradle improved quite a bit in Idea 13. However, the annotation processing is not so cleanly handled by Idea + Gradle. If you import the build.gradle project, you’ll need to turn on annotation processing to make the code compile successfully. If you do not IntelliJ will respond with a compilation error:
Conclusion
We’ve created a simple application and introduced Lombok. The jar file for Lombok needs to be in the classpath during compilation when working at the command line. This happens naturally when using Maven or Gradle by depending on Lombok. In an IDE, there are different ways to accomplish that.
Comments