How to get rid of duplicate class errors in Intellij for a Mavenized project using Lombok
Asked Answered
S

6

24

I have a Maven managed Lombok project and I use Intellij. After building, I always get lots of errors in Intellij about duplicate classes because of the generated sources in target/generated-sources/delombok. Is there something I can do to git rid of these errors? Right now I just delete the target folder, but this is really irritating to have to do.

I have the standard configuration in Maven and the Lombok source code in is in src/main/lombok:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

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

  <profiles>
    <profile>
        <id>lombok-needs-tools-jar</id>
        <activation>
            <file>
                <exists>${java.home}/../lib/tools.jar</exists>
            </file>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok-maven-plugin</artifactId>
                    <version>1.16.8.0</version>
                    <dependencies>
                        <dependency>
                            <groupId>sun.jdk</groupId>
                            <artifactId>tools</artifactId>
                            <version>1.8</version>
                            <scope>system</scope>
                            <systemPath>${java.home}/../lib/tools.jar</systemPath>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
Speedometer answered 14/6, 2016 at 2:4 Comment(3)
Is there any reason for using delombok? For just using Lombok, you only need the (provided) dependency on lombok.Copestone
I want to generate Javadoc that includes the Lombokified methods. For that, I need to generate sources so that I can run javadoc against those.Speedometer
I am not a mvn guru, but I'm pretty sure you can configure to delombok to a different directory (configure lombok.outputDirectory) and instruct javadoc to use that directory.Copestone
F
0

This solution suggested by Andrey Dernov here worked for me with Maven.

Enable Settings (Preferences on macOS) > Build, Execution, Deployment > Build Tools > Maven > Runner > Enable "Delegate IDE build/run actions to maven" option

Folberth answered 9/9, 2022 at 7:57 Comment(1)
This solution didn't work for me IntelliJ IDEA 2022.2.1 on Windows. I still get duplicated sources.Trimmer
D
23

According to delombok goal documentation: the default output directory is:

${project.build.directory}/generated-sources/delombok

I have found an JetBrains Team member comment stating that:

IDEA automatically excludes the build 'target' folder, providing that there are no generated sources under it, otherwise it excludes all sub-folders but the generated.

If you have some generated code or build artifacts that you want being excluded, you may put it under the 'target' folder.

This means that /generated-sources directory is by default not excluded and if you intend on excluding some files you should place them under parent /target directory and NOT under /generated-sources.

To achieve this you should configure the plugin and provide non-default <outputDirectory>:

<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.16.18.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sourceDirectory>src/main/java</sourceDirectory>
        <outputDirectory>${project.build.directory}/delombok</outputDirectory>
        <addOutputDirectory>false</addOutputDirectory>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>

This will stop IDEA from yielding Duplicate class found in (...) warnings.

Damaris answered 28/7, 2017 at 21:19 Comment(2)
I will try to test this out sometime in the next month. I am going to be on vacation for a while, work is crazy and I actually refactored the project I was working to not use delombok a while back because these errors were confusing to people who were working with my codebase.Speedometer
This solution didn't work for me IntelliJ IDEA 2022.2.1 on Windows. I still get duplicated sources.Trimmer
M
2

I tried Krzusiek's solution but IntelliJ would still mark my source files from src/main/java as duplicated.

After many failed attempts to tweak the lombok-maven-plugin I eventually decided to get rid of the delombok folder after compilation. That folder only server as an intermediary stage for compilation (in my case I needed it to use lombok with AspectJ compiler via the aspectj-maven-plugin) and there's no real need to keep it once your source files have been compiled.

Manually removing the delombok folder after each compilation is a real bummer, so I just configured an additional execution of the maven-clean-plugin which would specifically target the delombok folder.

Your POM should contain:

<build>

    <sourceDirectory>${project.build.directory}/generated-sources/delombok</sourceDirectory>

    <plugins>
        <!-- will delombok source files into /target/generated-sources/delombok -->
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.16.16.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>

        <!-- other plugins bound to compile phase should go here -->

        <plugin>
            <artifactId>maven-clean-plugin</artifactId>
            <executions>
                <execution>
                    <id>delombok-removal</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                    <configuration>
                        <excludeDefaultDirectories>true</excludeDefaultDirectories>
                        <filesets>
                            <fileset>
                                <directory>${project.build.sourceDirectory}/com</directory> <!-- assuming your root package is something like com.mycompany -->
                            </fileset>
                        </filesets>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Mammillary answered 24/12, 2019 at 14:11 Comment(0)
D
0

The idea is to remove generated-source from sources. I could do it from Project Structure -> Select from the list of packages and in right hand side find the folders that are being used to derive sources for that package. If you find generated-source in that list remove it.

Dorolisa answered 10/3, 2020 at 5:5 Comment(1)
This solution looked like it worked for me as it not longer showed the classes as duplicated, until I ran tests and they were still duplicated.Trimmer
A
0

Firstly, you have to apply to Krzysiek's answer - move generated_sources directory under target so it will be /target/generated_sources. The next step is to unmark given directory as generated sources root. Then you have to right click on given project in Intellij, select Maven and then Reload project. After that do the same but this time go Maven -> Generate Sources and Update Folders. If you have multi module project and one module depends on each other i.e. module A depends on B then you have to start with B first.

Albemarle answered 6/1, 2022 at 8:28 Comment(1)
This solution didn't work for me IntelliJ IDEA 2022.2.1 on Windows. Even if I unmark as generated sources it gets marked again automatically.Trimmer
F
0

This solution suggested by Andrey Dernov here worked for me with Maven.

Enable Settings (Preferences on macOS) > Build, Execution, Deployment > Build Tools > Maven > Runner > Enable "Delegate IDE build/run actions to maven" option

Folberth answered 9/9, 2022 at 7:57 Comment(1)
This solution didn't work for me IntelliJ IDEA 2022.2.1 on Windows. I still get duplicated sources.Trimmer
S
-1

you just need to unmark the folder with generated sources in Intellij and will be fine.

Schilt answered 27/4, 2020 at 13:25 Comment(1)
Consider including how to do it to give a useful answer.Trimmer

© 2022 - 2024 — McMap. All rights reserved.