-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
delombok sources in src/main/java rather than src/main/lombok #7
Comments
The "duplicate class" issue is precisely the reason why lombok code is segregated into its own source tree. However, I think you should be able to work around your issue by essentially telling Maven that your source is not in src/main/java; you can do this by adjusting the
in your pom. Alternatively, the maven-compiler-plugin configuration may be hacked to avoid compiling src/main/java AND target/generated-source/delombok. I think if you configure the value with a -sourcepath directive, that would do the trick. (Perhaps I can try this later to prove it. Feel free to beat me to it.) |
I tried your suggestion and it works: <sourceDirectory>target/generated-sources/delombok</sourceDirectory> The above will cause src/main/java to be omitted from compilation. There is no "duplicate class" error. Of course I need to remember to set the sourceDirectory parameter of lombok-maven-plugin to "${project.basedir}/src/main/java". I couldn't make the maven-compiler-plugin to exclude src/main/java from -sourcepath directive. The excludes option seems to affect only files and directories inside src/main/java (or inside other source compile paths). Maybe you have meant some other configuration, I couldn't find it. The workaround you suggested is ok. I just tested it on the simple example project. I plan to test your workaround on a bigger project and I will report if I have any problems applying it. However, the issue is still valid. If you have a project without lombok-maven-plugin and at some point you decide to include lombok-maven-plugin, you shouldn't be forced to go through all the maven modules you have and replace the directory name from "java" to "lombok". The "duplicate class" issue is hardly the reason for creating such a convetion. The convention stems from the fact that delombok is acting as source code generator, while it actually doesn't "generate" the source code. Delombok "transforms" the source code. Let me give you a simple example: I have a project that uses cxf-xjc-plugin that generates some java source files and places them in the directory target/generated-sources/xsdtojava. The java files use lombok. The project uses another external tool that processes java source files and the external tool cannot handle lombok. So at this point I decide to add lombok-maven-plugin to delombok source files from directories: src/main/java and target/generated-sources/xsdtojava. If I want to use your workaround, I can't set sourceDirectory to both directories. Maybe there is some way of specifying two directories for delombok goal. I couldn't find any at http://awhitford.github.com/lombok.maven/maven-lombok-plugin/delombok-mojo.html Actually it is not important, if there is a possibility to specify two source directories, because it would be just another workaround. I am not familiar with the maven build process, so I don't know how to describe what would be the good solution or if it is even feasible, but it seems to me that delombok goal should be executed after source code generation and before compilation. I know delombok is itself source code generator, but it differs from other generators in that it does not "add" sources, but rather "replaces" them. This is what my issue is about. Is implementing delombok as the source code generator the only way to go, given current possibilities in the maven project model and build process? What is your opinion on the issue? |
I understand the frustration, but options are limited. Fundamentally, delombok is acting like a source code generator; as much as you may want to think of it as a transformer, it is really a generator -- it is just that the input is Java-like (sometimes valid Java, sometimes not), and the output is Java. Certainly, if you want to fine-tune maven plugins to run at various phases, or even multiple times, you can do that. That is really a function of Maven: http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag |
I'd also like to see this problem addressed in future releases - maybe the trick documented here could be the default? (Or the default way to suggest that people install the plugin) |
Argh - using the target/generated-sources/delombok hack results in Netbeans thinking there is no Java source code for the project. Any hints on how to get around that? |
I've started a discussion on Stack Overflow about this to see if anyone with more Maven experience has anything to say: http://stackoverflow.com/questions/11329965 |
I've also submitted an RFE with Maven to support the proposed "profile" solution |
Setting addOutputDirectory to false in the configuration seems to of fixed this issue See http://stackoverflow.com/a/11930955/342518 for more information |
I recently responded to Issue #17 which is very similar. One needs to:
Basically, you will use <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> Use this hack at your own risk. (My guess is that this may confuse your IDE and some other developers.) |
This may work fine with
in lombok-maven-plugin:1.16.14.0:testDelombok (test-delombok) phase it can't find packages, symbols, so all unknown classes are replaced with |
You may have better luck using If you still have issues, can you provide a small sample application that demonstrates this issue so that it can be further investigated? |
Thanks @awhitford for your reply, but unfortunately it didn't work. Here is a small maven project to demonstrate that: As you will run
and the delomboked test file: final java.lang.Object myObj = new MyObj();
final com.small.project.MyObjFromT myObjT = new MyObjFromT();
|
Thank you @thelaurg for the unit test. I added something similar to my test project, was able to reproduce the problem, and then tweaked |
@awhitford I was also facing the same issue of duplicate classes with maven build for lombok-maven-plugin. Then I came across this discussion thread and used the below configuration in my parent pom.xml file
And I am able to do maven build successfully with maven 3.5.2 and jdk 1.8. Execution default of goal org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok failed: A required class was missing while executing org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok: com/sun/tools/javac/tree/JCTree$JCCompilationUnitrealm = plugin>org.projectlombok:lombok-maven-plugin:1.16.20.0 (org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok:default:generate-sources) org.apache.maven.plugin.PluginExecutionException: Execution default of goal org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok failed: A required class was missing while executing org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok: com/sun/tools/javac/tree/JCTree$JCCompilationUnitrealm = plugin>org.projectlombok:lombok-maven-plugin:1.16.20.0
Caused by: org.apache.maven.plugin.PluginContainerException: A required class was missing while executing org.projectlombok:lombok-maven-plugin:1.16.20.0:delombok: com/sun/tools/javac/tree/JCTree$JCCompilationUnitrealm = plugin>org.projectlombok:lombok-maven-plugin:1.16.20.0
Caused by: java.lang.NoClassDefFoundError: com/sun/tools/javac/tree/JCTree$JCCompilationUnit I am making an educated guess that this might be a jdk incompatibilty issue as the plugin requirement as mentioned in lombok-maven-plugin doc is jdk 1.6 but I am using jdk 1.8. Please note that I cannot use jdk 1.6 as it is a spring boot project and needs jdk 1.8 only. Suggest any workaround or treat this as a friendly observation of issue and provide solution for it to work with jdk versions higher than 1.6. |
@c0demark I think I will need more information about your scenario. Your problem is stemming from Eclipse, not the command-line? Note that Lombok requires Oracle JDK or Open JDK -- if you have another JDK, that may not work (and there is nothing that I can do about that). If you would like me to investigate further, please put together a sample project that exhibits the issue with reproduction steps. |
@awhitford I am getting error on java 11 although I set |
On a kind of related topic, I wanted to permanently delombok a large, multi-module project that had lombok and java code all in I hacked together this
Maybe others find this useful :) If anything's unclear I can tidy up the source code a bit. In the parent pom, I set: <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>copy-src-to-lombok</id>
<configuration>
<target>
<echo>trying to copy from [${basedir}/src/main]</echo>
<echo>target: [${basedir}/src/main/lombok]</echo>
<mkdir dir="${basedir}/src/main/lombok"/>
<mkdir dir="${basedir}/src/main/java"/>
<copy todir="${basedir}/src/main/lombok">
<fileset dir="${basedir}/src/main/java" includes="**/*"/>
</copy>
</target>
</configuration>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
<execution>
<id>replace-lombok-with-delombok</id>
<configuration>
<target>
<echo>removing lombok copy dir [${basedir}/src/main/lombok]</echo>
<delete dir="${basedir}/src/main/lombok"/>
<copy todir="${basedir}/src/main/java" failonerror="false">
<fileset dir="${basedir}/target/generated-sources/delombok"
includes="**/*java"/>
</copy>
<delete dir="${basedir}/target/generated-sources/delombok"/>
</target>
</configuration>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.20.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
<configuration>
<formatPreferences>
<suppressWarnings>skip</suppressWarnings>
<danceAroundIdeChecks>skip</danceAroundIdeChecks>
<indent>2</indent>
<generated>skip</generated>
</formatPreferences>
<outputDirectory>${basedir}/target/generated-sources/delombok</outputDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement> |
not friendly to idea .... |
I finally use this method solve this problem gently! hope can help the other guys. |
I have all the java sources in src/main/java directory. I want to be able to delombok them without changing the directory to src/main/lombok. I tried to use sourceDirectory parameter by setting it to "src/main/java", but I get a lot of "duplicate class" errors. I think the errors appear, because maven is trying to build from src/main/java and target/generated-sources/delombok at the same time.
Would it be possible to add a configuration parameter that would delombok the source code in src/main/java directory and continue the compilation only with the generated sources (ie. omitting original src/main/java)?
The text was updated successfully, but these errors were encountered: