How can I get Maven to compile from code modified (delombok'd) instead of from src/main/java

10,521

Solution 1

It looks you didn't read the documentation cause the plugin needs to be configured correctly like this which is the generate-sources life-cycle phase and afterwards the source files which have been generated will automatically be picked up by the maven-compiler-plugin.

<build>
  <plugins>
    <plugin>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok-maven-plugin</artifactId>
      <version>0.11.6.0</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>delombok</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Solution 2

This is not going to be useful for everyone. But IF you use lombok to write your own annotation processor, then you need to have a different configuration.

To create a compiler without lombok you need to set proc to none:

      <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                                    <proc>none</proc>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>

And with lombok you must explicitly set the annotation processor :

      <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven-compiler-plugin.version}</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
                <annotationProcessors>
                    <annotationProcessor>lombok.core.AnnotationProcessor</annotationProcessor>
                </annotationProcessors>
            </configuration>
        </plugin>
    </plugins>
Share:
10,521
Jamey
Author by

Jamey

I am currently a developer at InCrowd, I love learning new technologies and solving problems! :D

Updated on November 23, 2022

Comments

  • Jamey
    Jamey over 1 year

    Using the delombok maven plugin places all my files in 'target/generated-sources/delombok'. When I try and run the maven compiler it complains about duplicate classes so I set addOutputDirectory to false as this question recommends. The problem now is that the delombok'ed files are ignored and so the compiler complains about missing methods.

    How can I tell the maven compiler plugin to ignore the default 'src/main/java' directory and instead to use the 'target/generated-sources/delombok' directory to compile from?

    Running mvn compile -X produces the following output when the compiler runs:

    [DEBUG]   (f) compileSourceRoots = [C:\Users\Jamey.Holden\workspace\Apollo\Website\src\main\java, C:\Users\Jamey.Holden\workspace\Apollo\Website\target\generated-sources\java]
    [DEBUG]   (f) compilerId = javac
    [DEBUG]   (f) debug = true
    [DEBUG]   (f) encoding = UTF-8
    [DEBUG]   (f) failOnError = true
    [DEBUG]   (f) forceJavacCompilerUse = false
    [DEBUG]   (f) fork = false
    [DEBUG]   (f) generatedSourcesDirectory = C:\Users\Jamey.Holden\workspace\Apollo\Website\target\generated-sources\annotations
    [DEBUG]   (f) mojoExecution = org.apache.maven.plugins:maven-compiler-plugin:3.0:compile {execution: default-compile}
    [DEBUG]   (f) optimize = false
    [DEBUG]   (f) outputDirectory = C:\Users\Jamey.Holden\workspace\Apollo\Website\target\classes
    [DEBUG]   (f) proc = none
    [DEBUG]   (f) projectArtifact = ic.apollo:website:war:0.1
    [DEBUG]   (f) showDeprecation = false
    [DEBUG]   (f) showWarnings = false
    [DEBUG]   (f) skipMultiThreadWarning = false
    [DEBUG]   (f) source = 1.6
    [DEBUG]   (f) staleMillis = 0
    [DEBUG]   (f) target = 1.6
    [DEBUG]   (f) verbose = false
    [DEBUG]   (f) mavenSession = org.apache.maven.execution.MavenSession@393e6226
    [DEBUG]   (f) session = org.apache.maven.execution.MavenSession@393e6226
    

    Then further down where command line options are printed I can see that the -sourcepath argument is: -sourcepath C:\Users\Jamey.Holden\workspace\Apollo\Website\src\main\java;C:\Users\Jamey.Holden\workspace\Apollo\Website\target\generated-sources\java; Neither of these are the delombok directory, so it's fair enough that it can't find all the getters and setters when it tries to compile.

    UPDATE I think I'm getting to the bottom of the problem. I was setting proc=none to prevent annotation processing, because I am using queryDSL to generate meta-entities and when this was not set to avoid annotation processing the compiler had a duplicate entities found error. Removing proc=none and the querydsl annotation processor solved the problem. Now I've just go to get m2e to work again.

  • Jamey
    Jamey over 11 years
    I'm accepting this because it led to me solving my problem which was that I had set proc=none, thank you!
  • zygimantus
    zygimantus over 8 years
    Why when I used this /<annotationProcessor>lombok.core.AnnotationProcessor</annot‌​ationProcessor> the generated source is not visible in my project?
  • Jezor
    Jezor about 7 years
    For people from the future who will encounter the error with encoding, I had to add a property (properties tag) <lombok.encoding>UTF-8</lombok.encoding> to pom.xml.
  • Jelle De Loecker
    Jelle De Loecker over 3 years
    I've added this, but it still compiles the original files in src/main/lombok and ignored the generated ones.