Intellij idea 子项目中对包的未解析依赖关系
在我的Kotlin spring boot项目中,我使用的是Kotlin DSL Gradle,有三个子项目。其中两个是Intellij idea 子项目中对包的未解析依赖关系,intellij-idea,kotlin,gradle-kotlin-dsl,Intellij Idea,Kotlin,Gradle Kotlin Dsl,在我的Kotlin spring boot项目中,我使用的是Kotlin DSL Gradle,有三个子项目。其中两个是安全性,它依赖于数据库 在IntelliJ中,应用程序成功运行并按预期执行(当作为Spring Boot应用程序运行配置运行时) 然而,当我尝试用Gradle/gradlew clean build构建项目时,我得到了 e: /security/src/main/kotlin/com/path/security/jwt/JwtRealm.kt: (3, 26): Unresol
安全性
,它依赖于数据库
在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
错误是回购协议中的值。UserRepository
类位于模块database
中,JwtRealm
位于security
模块中
security
模块中我的build.gradle.kts
文件如下所示:
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")
}
import org.jetbrains.kotlin.gradle.tasks.kotlincomfile
group=“com.path”
version=“1.0-SNAPSHOT”
插件{
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管理”)
}
存储库{
mavenCentral()
}
依赖关系{
实施(kotlin(“stdlib-jdk8”))
实施(kotlin(“反映”))
实施(项目(“:数据库”))
实现(“org.springframework.boot:springbootstarterweb:2.1.1.RELEASE”)
实现(“org.springframework.boot:springbootstarteractor:2.1.1.RELEASE”)
实现(“org.springframework.boot:springbootstarter数据jpa:2.1.1.RELEASE”)
实现(“org.springframework.boot:springbootstarterjdbc:2.1.1.RELEASE”)
实现(“org.apache.shiro:shiro-spring-boot-webstarter:1.4.0”)
实施(“com.h2数据库:h2:1.4.197”)
实现(“com.auth0:javajwt:3.4.1”)
实现(“io.github.microutils:kotlin日志记录:1.6.22”)
}
tasks.withType{
kotlinOptions.jvmTarget=“1.8”
Kotlinoctions.freeCompilerArgs=listOf(“-Xjsr305=strict”)
}
它基本上与我的根项目Gradle文件相同,只是还包含对所有子项目(或模块)的引用。我还列出了settings.gradle.kts
IntelliJ在运行或浏览此代码时没有问题,没有突出显示的错误或警告
我有一个带有@Configuration
注释的类作为每个模块的根,还带有@ComponentScan
注释
我遗漏了什么?经过大量挖掘,我发现这是一个弹簧靴问题。我通过申请来解决这个问题
bootJar.enabled = false
jar.enabled = true
到每个子项目。在KotlinDSL中,这是:
tasks.withType<BootJar> {
enabled = false
}
tasks.withType<Jar> {
enabled = true
}
tasks.withType{
已启用=错误
}
tasks.withType{
启用=真
}
我希望这对其他人有所帮助,因为我是在大量挖掘之后才发现这个问题的。解决方案的来源如下:IntelliJ默认使用自己的构建系统。您可以让它改为使用Gradle:设置->构建、执行、部署->构建工具->Gradle->运行程序->将IDE构建/运行操作委托给Gradle
。可以在IntelliJ中独立于Gradle设置依赖项(尽管这不是一个好主意)。这就是为什么它和其中一个一起工作,而不是另一个。如果您在IntelliJ中打开Gradle窗口并展开“安全”项目的节点,您应该会看到它的依赖项。你能看到里面的“数据库”吗?它突出显示为红色吗?这将显示Gradle是否可以将其视为依赖项。是的,Gradle选项卡显示依赖项都很满意<代码>安全性
确实依赖于数据库
,而没有红色的哑炮。从选项卡运行构建自然也会失败。运行/gradlew dependencies
是否显示了您期望的依赖关系?另外,您有任何自定义源代码集或类似的东西吗?是的,依赖项完全符合预期。稍后我会用它们更新。不,在我的设置中没有复杂或不寻常的东西,没有自定义的源代码集。你甚至无法想象我花了多少时间试图找到这个解决方案。。。谢谢。我已经花了大约12个小时来敲我的头,正因为这个,我准备放弃科特林。非常感谢。