简体   繁体   English

SpringBoot - STS/Eclipse 将 MapStruct 映射器实现标记为错误“类型 X 已定义”

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

STS v. 4.14.1.RELEASE STS 诉 4.14.1.RELEASE

Java v. 11 Java 诉 11

Spring Boot v. 2.6.7 Spring 引导 v. 2.6.7

MapStruct v. 1.5.1 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.我正在测试 MapStruct 的功能,我发现自己遇到了一个非常令人沮丧但显然是基本的问题,我现在几个小时都无法解决。

Once added the MapStruct dependencies and plugin to the project as supposed:一旦按照设想将 MapStruct 依赖项和插件添加到项目中:

<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. MapStruct 应该在实现映射器接口的同一个 package 中自动生成 MapperImpl,在根文件夹 'target/generated-sources/annotations' 下,它完美地完成了。

The problem is STS/Eclipse marks the generated MapperImpl class with an error ' The type XMapperImpl is already defined '.问题是 STS/Eclipse 将生成的 MapperImpl class 标记为错误“ XMapperImpl 类型已定义”。

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.我在项目中到处搜索——包括我的类和自动生成的类——没有另一个同名的 class——实际上它是整个项目中唯一的映射器接口——所以我不明白怎么能它已经被定义了。

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.我还清理了几次项目,重新构建了它......我能想到的任何事情都是为了清理可能干扰这个 class 的任何东西,并且每次项目生成 XMapperImpl 时,STS 都会用相同的错误标记它。

At this point I am wondering whether there is something I might be overlooking or perhaps some bug in the STS/Eclipse?在这一点上,我想知道在 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.更新:我很确定它必须是某种缓存,因为即使我删除了映射器的源文件,在更新 Maven 项目时,映射器实现仍然出现在自动生成的文件中。 I still don't understand where it is being cached though.我仍然不明白它被缓存在哪里。

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:由于 Eclipse 和 Maven 的“清理”功能都不能清理导致这些错误的“垃圾”文件,因此我采取了以下步骤来解决它:

Search the paths where the [garbage] files causing the problem could possibly be.搜索可能导致问题的 [garbage] 文件所在的路径。

Given my set-up (linux machine, STS/Eclipse, Maven...) they were in the paths:鉴于我的设置(Linux 机器、STS/Eclipse、Maven...),它们位于以下路径中:

    $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.并手动删除它们——它们都是派生/缓存的文件,因此删除不会通过简单的 Maven 更新项目重新生成的东西是没有风险的。

And that's all.就这样。 Run Maven Update Project and everything will work perfectly again and errors will be gone.运行 Maven 更新项目,一切将再次完美运行,错误将消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM