在我的 Kotlin spring boot 项目中,我使用了 Kotlin DSL Gradle,其中包含三个子项目。其中两个是security
这取决于 database
.
在 IntelliJ 中,应用程序成功运行并按预期执行(当作为 Spring Boot 应用程序运行配置运行时)。
但是,当我尝试使用 Gradle 构建项目时 ./gradlew clean build
我明白了
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (3, 26): Unresolved reference: database
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (19, 24): Unresolved reference: UsersRepository
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (36, 48): Unresolved reference: it
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (38, 42): Unresolved reference: it
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (38, 75): Unresolved reference: it
e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (40, 63): Unresolved reference: it
> Task :security:compileKotlin FAILED
它提示的实际线路是:
import com.path.database.repositories.UsersRepository
it
错误是来自 repo 的值。 UserRepository
类(class)在模块 database
中和 JwtRealm
在security
模块。我的
build.gradle.kts
security
中的文件模块如下所示:import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
group = "com.path"
version = "1.0-SNAPSHOT"
plugins {
kotlin("jvm")
id("org.jetbrains.kotlin.plugin.spring")
id("org.jetbrains.kotlin.plugin.noarg")
id("org.jetbrains.kotlin.plugin.jpa")
id("org.springframework.boot")
id("io.spring.dependency-management")
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
implementation(project(":database"))
implementation("org.springframework.boot:spring-boot-starter-web:2.1.1.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-actuator:2.1.1.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-data-jpa:2.1.1.RELEASE")
implementation("org.springframework.boot:spring-boot-starter-jdbc:2.1.1.RELEASE")
implementation("org.apache.shiro:shiro-spring-boot-web-starter:1.4.0")
implementation("com.h2database:h2:1.4.197")
implementation("com.auth0:java-jwt:3.4.1")
implementation("io.github.microutils:kotlin-logging:1.6.22")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
}
这与我的根项目 Gradle 文件基本相同,只是它还包含对所有子项目(或模块)的引用。我还在
settings.gradle.kts
中列出了所有子项目。IntelliJ 运行或浏览此代码没有问题,没有突出显示的错误或警告。
我有一个
@Configuration
用 @ComponentScan
注释类作为每个模块的根注释也。我错过了什么?
最佳答案
经过大量挖掘,我发现这是一个 Spring Boot 问题。我通过申请解决了这个问题
bootJar.enabled = false
jar.enabled = true
到每个子项目。在 KotlinDSL 中,这是:
tasks.withType<BootJar> {
enabled = false
}
tasks.withType<Jar> {
enabled = true
}
我希望这对其他人有帮助,因为我经过大量挖掘才发现这个问题。解决方案的来源在这里:https://github.com/gradle/kotlin-dsl/issues/393
关于intellij-idea - 子项目中对包的未解决依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775262/