Having to use Lombok in modules for projects that don’t know that library, I try to stay under cover… I mean that I develop the modules with Lombok but clients of those modules must not see Lombok’s annotations: so I set up Maven to delombok the source code I have written.

I’ve set up a “Maven Project” in Eclipse: the full project, called delombokfullness, is forkable at GitHub:

https://github.com/caladyon/BethelwhiteSamples/tree/e3e8e7eacb584d504334af8632c5840344f729fb/delombokfullness

Technologies used:

  • Lombok / lombok-maven-plugin 1.16 (any recent version should work too)
  • Eclipse (with Lombok jar installed)
  • Java 8 – but it should work with other versions, lombok requires Java 6

The versions of Lombok and its plugin should be kept aligned.

Why delombok?

I’ve already said why I think Lombok is useful and important… so why delombok? Why removing the Lombok’s annotations and building vanilla souirce code?

The first reason that come to my mind is being able to use the delomboked source code in a lombok-unaware environment: so every one could use the generated API and debug your source code.

Secondly, the delomboked code could be used to generate a full Javadoc (documenting the generated method as well) or to do a test coverage analysis.

Step 1 – Create and customize the project

I’ve created a new Maven project with the Eclipse wizard. I’ve used the “Create a simple project (skip archetype selection)” option.

Then I modified the pom.xml file, adding few properties…

        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

…and adding a single dependency.

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
    <scope>provided</scope>
</dependency>

This is enough to start programming with Lombok: you can see that I’ve written two classes that work well: LombokAnnotatedClass and LombokAnnotatedField.

But take care of one thing: I’ve put these classes under an extra source folder that I’ve added: “src/main/lombok”. This source folder will come in handy later on.

Step 2 – Set up lombok-maven-plugin

Now it’s time to bring the plugin in the pom.xml:

    <!-- http://awhitford.github.io/lombok.maven/lombok-maven-plugin/usage.html -->
    <plugin>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok-maven-plugin</artifactId>
        <version>1.16.20.0</version>
        <configuration>
            <compilerVersion>1.8</compilerVersion>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>delombok</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

This configuration activates the “delombok” action during the source file generation: it takes all the files under “src/main/lombok” (the extra source folder I’ve created before), and generates vanilla java file removing lombok annotations. (You could find the generated files under “target/generated-sources/delombok”).

You can try it out with the following command:

mvn compile

The command output shows among other things:

[INFO] --- lombok-maven-plugin:1.16.20.0:delombok (default) @ delombokfullness ---
[INFO] Delombok complete.

For Eclipse users, you should add the following section to your pom.xml (in the pluginManagement part), in order to avoid an error raised by your IDE:

    <plugin>
        <groupId>org.eclipse.m2e</groupId>
        <artifactId>lifecycle-mapping</artifactId>
        <version>1.0.0</version>
        <configuration>
            <lifecycleMappingMetadata>
                <pluginExecutions>
                    <pluginExecution>
                        <pluginExecutionFilter>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-maven-plugin</artifactId>
                            <versionRange>[1,)</versionRange>
                            <goals>
                                <goal>delombok</goal>
                            </goals>
                        </pluginExecutionFilter>
                        <action>
                            <ignore />
                        </action>
                    </pluginExecution>
                </pluginExecutions>
            </lifecycleMappingMetadata>
        </configuration>
    </plugin>