祝大家有美好的一天。我在尝试将 MapStruct 功能添加到项目时遇到了问题。而且,错误就像滚雪球一样,一个接一个。我会很高兴得到任何帮助。为了:
对于初学者,我不明白为什么 Intellij IDEA 不能正确理解
kapt
. 事实上,错误总是出现Unresolverd reference: kapt
。同时,IDE 建议导入某个类import org.jetbrains.kotlin.kapt3.base.Kapt.kapt
,因此该构造kapt
可以在块中使用dependencies
,但该类的实例应该作为参数KaptOptions
,我似乎根本不需要。如果删除导入,IDE 会发誓unresolved reference: kapt
,但项目的构建是成功的。我不会说这很关键,但它会切入眼睛并分散注意力。一般来说,我不希望有任何“红色”文件,此外,IDE 不断建议导入“红色”kapt
,这也很不方便。该项目有一个实体系统。尝试执行
gradle clean build
时,出现错误: 实体类e: <path_to_class>\System.java:3: error: System is already defined in this compilation unit import java.lang.System;
中没有导入(原则上,其他任何地方都没有)。例如,如果您将实体重命名为- 错误就会消失,但我不想诉诸这种方法。java.lang.System
System
Systemm
package com.sprojects.core.server.model.entity
import java.util.Date
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.OneToMany
import javax.persistence.PreUpdate
import javax.persistence.Table
import javax.persistence.Temporal
import javax.persistence.TemporalType
@Entity
@Table(name = "systems")
data class System(
// ... Some fields here ... //
) {
// ... Some code here //
}
- 不生成 MapStruct 映射器实现。显然,这个问题与问题 2 密切相关,但无论如何它并不能解决它。试图得到一个映射器:
val mapper = Mappers.getMapper(MyConverter::class.java)
。错误:java.lang.ClassNotFoundException: Cannot find implementation for com.sprojects.core.server.model.dto.impl.MyConverter
。build.gradle.kts 的内容*:
plugins {
// kotlin
id("kotlin")
kotlin("jvm")
kotlin("kapt")
kotlin("plugin.spring")
kotlin("plugin.jpa")
// Spring
id("org.springframework.boot")
id("io.spring.dependency-management")
// Other
id("idea")
id("com.palantir.docker")
id("org.jlleitschuh.gradle.ktlint")
id("net.ltgt.apt") version "0.21"
id("net.ltgt.apt-idea") version "0.21"
}
group = "${rootProject.group}"
version = "${rootProject.version}"
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.postgresql:postgresql")
implementation("io.jsonwebtoken:jjwt:${rootProject.extra["jwtVersion"]}")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:${rootProject.extra["springBootVersion"]}")
implementation("org.springframework.security:spring-security-jwt:${rootProject.extra["springSecurityJwtVersion"]}")
implementation("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.testcontainers:testcontainers:${rootProject.extra["testContainersVersion"]}")
testImplementation("org.testcontainers:postgresql:${rootProject.extra["testContainersVersion"]}")
testImplementation("org.springframework.security:spring-security-test")
implementation(
group = "io.springfox",
name = "springfox-swagger2",
version = "${rootProject.extra["swagger2Version"]}"
)
implementation(
group = "io.springfox",
name = "springfox-swagger-ui",
version = "${rootProject.extra["swagger2Version"]}"
)
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation(
group = "org.mapstruct",
name = "mapstruct",
version = "${rootProject.extra["mapStructVersion"]}"
)
// При такой попытке не работает сборка проекта
kapt("org.mapstruct:mapstruct-processor:${rootProject.extra["mapStructVersion"]}")
// При такой попытке работает сборка, но не создаются реализации мапперов
/*annotationProcessor(
group = "org.mapstruct",
name = "mapstruct-processor",
version = "${rootProject.extra["mapStructVersion"]}"
)*/
}
如果您需要提供更多数据,我会很高兴提供任何帮助 - 我会提供。谢谢你。
更新。问题 1 不相关。IDE 提示帮助:Apply Context
.