I am trying to use the Lombok Maven Plugin to ensure the correct creation of Javadocs when using Lombok.
Lombok Maven introduces a new code generation goal, just pri
The delombok
goal is designed to transform java code from src/main/lombok
to target/generated-source/delombok
. Then, the other java code found in src/main/java
is combined with target/generated-source/delombok
to produce the combined java classes.
It helps to think of delombok as a source code generator.
So how can you get what you really want? (Note that Maven has an addCompileSourceRoot method, but not a corresponding removeCompileSourceRoot.) Imagine the following hack:
<build><sourceDirectory>
from src/main/java
to be ${project.build.directory}/generated-sources/delombok
.sourceDirectory
from src/main/lombok
to be src/main/java
, and disable addOutputDirectory
. Basically, you will use src/main/java
, but Maven will ignore it and instead use target/generated-sources/delombok
. The Lombok plugin will transform src/main/java
into elaborated code in target/generated-sources/delombok
.
<build>
<sourceDirectory>${project.build.directory}/generated-sources/delombok</sourceDirectory>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.6.1</version>
<executions>
<execution>
<id>delombok</id>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
<configuration>
<addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that you should not need to hack other plugins, like maven-jar-plugin
or maven-javadoc-plugin
, because they should respect the sourceDirectory
.
Use this hack at your own risk. (My guess is that this may confuse your IDE and some other developers.)
Create a new Maven profile, and in the new profile, just redefine the sources:
<sourceDirectory>target/generated-sources/delombok</sourceDirectory>
Alternatively
I recently switched from using the flakey maven-exec-plugin approach to generate raw sources for the javadoc tool to using lombok-maven-plugin
My setup
src/main/java
target/generated-sources/delombok
I initially ran into this problem but it seems to be an easy fix: Don't let lombok-maven-plugin add the delombok path to the compiler source paths with addOutputDirectoy
. IE
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>0.11.2.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
</plugin>
This seems to of solved the issue for now
EDIT: Bonus, how to generate proper javadocs with this setup
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<defaultVersion>${project.version}</defaultVersion>
<sourcepath>target/generated-sources/delombok</sourcepath>
</configuration>
</plugin>
I tried to use the solutions from the other answers, but IntelliJ would still mark my source files from src/main/java
as duplicated. So I just decided to get rid of the delombok
's content after compilation. In the end that folder only server as an intermediary stage for compilation (in my case it was required to use lombok with AspectJ compiler) and there's no real need to keep it. I did it by configuring an additional execution of the clean plugin which would specifically target the delombok
folder.
Your POM should contain
<build>
<sourceDirectory>${project.build.directory}/generated-sources/delombok</sourceDirectory>
<plugins>
<!-- will delombok source files into /target/generated-sources/delombok -->
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.16.16.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>src/main/java</sourceDirectory>
</configuration>
</plugin>
<!-- other plugins bound to compile phase should go here -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<executions>
<execution>
<id>delombok-removal</id>
<phase>compile</phase>
<goals>
<goal>clean</goal>
</goals>
<configuration>
<excludeDefaultDirectories>true</excludeDefaultDirectories>
<filesets>
<fileset>
<directory>${project.build.sourceDirectory}/com</directory> <!-- assuming your root package is something like com.mycompany -->
</fileset>
</filesets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
It would appear that there is no nice way to set up Lombok Maven. The "profile" approach suggested by @opyate doesn't work because of
http://jira.codehaus.org/browse/MNG-5310
However, a really ugly workaround is to simply have another pom.xml
, identical in every way except with the sourceDirectory
redefined, using the command line arguments to use the file like in:
Maven alternate pom