SpringBoot - STS/Eclipse marks MapStruct mapper implementation with the error 'The type X is already defined'

Viewed 127

STS v. 4.14.1.RELEASE

Java v. 11

Spring Boot v. 2.6.7

MapStruct v. 1.5.1

I'm testing MapStruct functionalities and I've found myself in a very frustrating yet apparently basic problem which I'm not being able to solve for some hours now.

Once added the MapStruct dependencies and plugin to the project as supposed:

<properties>
    <java.version>11</java.version>
    <org.mapstruct.version>1.5.1.Final</org.mapstruct.version>
</properties>


<dependency>
    <groupId>org.mapstruct</groupId>
    <artifactId>mapstruct</artifactId>
    <version>${org.mapstruct.version}</version>
</dependency>

        
<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-compiler-plugin</artifactId>
     <version>3.8.0</version>
     <configuration>
         <annotationProcessorPaths>
             <path>
                 <groupId>org.mapstruct</groupId>
                 <artifactId>mapstruct-processor</artifactId>
                 <version>${org.mapstruct.version}</version>
             </path>
             <path>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok</artifactId>
                 <version>${lombok.version}</version>
             </path>
             <dependency>
                 <groupId>org.projectlombok</groupId>
                 <artifactId>lombok-mapstruct-binding</artifactId>
                 <version>0.2.0</version>
             </dependency>
         </annotationProcessorPaths>
         <compilerArgs>
             <compilerArg>
                 -Amapstruct.defaultComponentModel=spring
             </compilerArg>
         </compilerArgs>
     </configuration>
 </plugin>

MapStruct is supposed to auto generate the MapperImpl in the same package where the mapper interface is implemented, under the root folder 'target/generated-sources/annotations' and it does it perfectly.

The problem is STS/Eclipse marks the generated MapperImpl class with an error 'The type XMapperImpl is already defined'.

I've search everywhere in the project - both mine and auto-generated classes - and there is not such another class with the same name - actually it is the only mapper interface in the whole project-, so I don't understand how can it be already defined.

I've also cleaned the project several times, re-built it... Anything I can think of in order to clean whatever could be interfering with this class and every time the project generates the XMapperImpl, STS marks it with the same error.

At this point I am wondering whether there is something I might be overlooking or perhaps some bug in the STS/Eclipse?

Update: I'm pretty sure it has to be some kind of cache, because even if I delete the source file of the mapper, when updating the Maven project, the mapper implementation keeps on appearing in the auto-generated files. I still don't understand where it is being cached though.

1 Answers

Just in case someone finds it useful the solution I've ended up applying in this case, I'll leave it here.

Since neither Eclipse nor Maven 'clean' functionalities do clean the 'garbage' files which cause these errors, these are the steps I've taken to solve it:

Search the paths where the [garbage] files causing the problem could possibly be.

Given my set-up (linux machine, STS/Eclipse, Maven...) they were in the paths:

    $HOME/project-path/target/classes/path-to-mappers/ -> files.class, files.java
    $HOME/project-path/.apt-generated/path-to-mappers/ -> files.class, files.java (These files are hidden; won't be found in a 'regular' files search)

In case of different set-ups, just adapt these paths/folders to perform the search.

And delete them manually - All of them are derived/cached files, so there's no risk to delete something which won't be regenerated with a simple Maven's Update Project.

And that's all. Run Maven Update Project and everything will work perfectly again and errors will be gone.

Related