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

StackOverflow https://stackoverflow.com/questions/13479099

Pregunta

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.

¿Fue útil?

Solución

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>

Otros consejos

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>

Do something similar to

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.0</version>
      <configuration>
        <includes>
            <include>target/generated-sources/delombok/*.java</include>
        </includes>
        <excludes>
            <exclude>src/main/java</exclude>
        </excludes>         
      </configuration>
    </plugin>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top