问题
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 prior to compilation. In my configuration, my sourceDirectory (Java with Lombok annotations, src/main/java) is processed to create Java (without Lombok annotations) in target/generated-sources/delombok.
However, every file in sourceDirectory now has a corresponding (identically named) file in target/generated-sources/delombok, resulting in compilation failures due to duplicate classes.
How can I tell the Java compiler to ignore the sources in sourceDirectory?
Note that the default Lombok Maven configuration would have the developer put Java (with Lombok annotations) in the src/main/lombok folder instead of src/main/java. However, I do not wish to do this because it confuses IDEs and my code compiles just fine (if I remove the Maven plugin).
Also note that simply redefining sourceDirectory will also upset IDEs (they no longer know where to find the Java source code!).
回答1:
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>
回答2:
Create a new Maven profile, and in the new profile, just redefine the sources:
<sourceDirectory>target/generated-sources/delombok</sourceDirectory>
Alternatively
- Build helper: there's a build helper which seems to provide this functionality, but I have not used this before.
- 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.
回答3:
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:
- Override the default
<build><sourceDirectory>fromsrc/main/javato be${project.build.directory}/generated-sources/delombok. - Override the default delombok
sourceDirectoryfromsrc/main/lombokto besrc/main/java, and disableaddOutputDirectory.
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.)
回答4:
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
来源:https://stackoverflow.com/questions/11329965/how-to-ignore-the-java-source-directory-during-maven-compilation