【发布时间】:2021-10-19 11:59:10
【问题描述】:
运行命令mvn clean install时出现以下错误:
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/target/generated-sources/kapt/compile/com/xxx/xxx/xxx/xxx/DataMapperImpl.java:[10,40] cannot find symbol
[ERROR] symbol: class DataMapperDecorator
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/target/generated-sources/kapt/compile/com/xxx/xxx/xxx/xxx/DataMapperImpl.java:[10,74] cannot find symbol
[ERROR] symbol: class DataMapper
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/xxx/generated-sources/kapt/compile/com/xxx/xxx/xxx/api/DataMapperImpl.java:[12,19] cannot find symbol
[ERROR] symbol: class DataMapper
[ERROR] location: class com.xxx.xxx.xxx.xxx.DataMapperImpl
mapstruct 生成DataMapperImpl.java 类后似乎找不到DataMapper 和DataMapperDecoretor 类。
与mapstruct相关的代码在xxx.kt文件中:
//Other stuff
...
@Mapper
@DecoratedWith(DataMapperDecorator::class)
interface DataMapper {
@Mappings(
Mapping(source = "data.value", target = "dataValue"),
Mapping(source = "data.current.value", target = "currentValue"),
)
fun toDto(data: Data) : RefDataDto
}
abstract class DataMapperDecorator : DataMapper {
@Autowired
private val delegate: DataMapper? = null
override fun toDto(data: Data): dataDto {
val dataDto = delegate!!.toDto(data)
dataDto.primaryValue = data.primaryValue?.let { CurrencyUtil.toMajor(it) }
return dataDto
}
}
关于我在根文件中的pom 文件:
...
<properties>
...
<org.mapstruct.version>1.4.2.Final</org.mapstruct.version>
</properties>
...
这是我正在使用 mapstruct 的模块的pom:
...
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/kotlin</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
...
我用点隐藏了文件的某些部分,并且我没有使用 Lombok 项目(我看到了与它相关的相同问题,我们正在尝试将这些项目一起使用)。
更新 1:
我注意到该错误与生成的类DataMapperImpl.java 中包含该生成类应使用的类的包不可见有关。确实我看到了这个错误:
[ERROR] /Users/xxx/xxx/xxx/xxx.xxx/xxx/generated-sources/kapt/compile/com/xxx/xxx/xxx/xxx/RefDataMapperImpl.java:[3,47] package com.my.package.application.domain does not exist
当然这个包是存在的!
更新 2:
我正在继续调查这个问题。我试图简化删除 DataMapperDecorator 并将DataMapper、Data 和DataDto 类放在同一个文件中。对于所有三个类,仍然存在相同的错误cannot find symbol: class。我不确定这是否与 DataMapperImpl (生成的类)中没有这些类的导入有关。仅针对标准 java 库的导入,例如:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.UUID;
import javax.annotation.processing.Generated;
编辑 1:
从日志中我还可以看到以下警告:
[WARNING] 'tools.jar' was not found, kapt may work unreliably
更新 3:
target->classes 下没有 mapstruct(使用 IntelliJ IDEA)我可以看到我的项目的类。另一方面,当我介绍 mapstruct 时,我看到的是 mapstruct 类是在 target->generated-sources->kapt->compile 下生成的,但在 target->classes 下我看不到其他类。是否会比我项目的其他类更早生成 mapstruct 类,导致编译器找不到 Data、DataDto 类?
【问题讨论】: