How to ignore the Java Source directory during Maven Compilation?

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:

  1. Override the default <build><sourceDirectory> from src/main/java to be ${project.build.directory}/generated-sources/delombok.
  2. Override the default 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

  1. Build helper: there's a build helper which seems to provide this functionality, but I have not used this before.
  2. Hack: there's a hack described here, but the author recommends against it, and besides: if you're going to take the time to modify your POM and create this hack, you may as well do things The Right Way and implement a profile.

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

  • All sources in src/main/java
  • Generated sources go in 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>

Tags:

Java

Maven

Lombok