SpringBoot Gradle多模块项目未解决的参考错误

时间:2020-09-23 11:14:41

标签: spring-boot kotlin gradle multi-module

我正在尝试在gradle上创建多模块springboot应用程序,但在构建过程中收到未解决的参考错误消息。各个模块的Gradle配置如下。

在构建期间导入org.example.finalmultimodule.data.repositories.IPersonRepository时出现未解决的参考错误:

enter image description here

错误: enter image description here

目录树:

enter image description here

BuildTools> Gradle: enter image description here

主要build.gradle:

plugins {
    id("org.springframework.boot") version "2.3.3.RELEASE"
    id("io.spring.dependency-management") version "1.0.10.RELEASE"
    kotlin("plugin.spring") version "1.4.10"
    kotlin("jvm") version "1.4.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

// Projekty
project(":data") {
    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
}

project(":business") {
    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
}

project(":api") {
    apply(plugin = "org.springframework.boot")
    apply(plugin = "io.spring.dependency-management")
}

dependencies {
    // Moduly
    implementation(project(":core"))
    implementation(project(":data"))
    implementation(project(":business"))
    implementation(project(":api"))

    implementation(kotlin("stdlib"))
}

allprojects {
    tasks.withType<Test> {
        useJUnitPlatform()
    }

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }
}

核心build.gradle:

plugins {
    kotlin("jvm")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    // Anotace pro @JsonProperty
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.11.2")

    implementation(kotlin("stdlib"))
}

数据build.gradle:

plugins {
    kotlin("jvm")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    // Moduly
    implementation(project(":core"))

    // Anotace @Component etc...
    implementation("org.springframework:spring-context")

    // Corutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

    // Databaze
    implementation("org.springframework.boot:spring-boot-starter-data-r2dbc")
    implementation("io.r2dbc:r2dbc-mssql")

    implementation(kotlin("stdlib"))
}

企业build.gradle:

plugins {
    kotlin("jvm")
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":core"))
    implementation(project(":data"))

    // Kediatr variace na Mediatr
    implementation("com.trendyol:kediatr-spring-starter:1.0.14")

    implementation(kotlin("stdlib"))
}

Api(springboot)build.gradle:

plugins {
    kotlin("jvm")
    kotlin("plugin.spring")
}

group = "org.example"
version = "0.0.1-SNAPSHOT"


repositories {
    mavenCentral()
}

dependencies {
    // Moduly
    implementation(project(":core"))
    implementation(project(":business"))

    // Kediatr variace na Mediatr
    implementation("com.trendyol:kediatr-spring-starter:1.0.14")
    
    implementation("org.springframework.boot:spring-boot-starter-rsocket")
    implementation("org.springframework.boot:spring-boot-starter-validation")
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

    testImplementation("org.springframework.boot:spring-boot-starter-test") {
        exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
    }
    testImplementation("io.projectreactor:reactor-test")
}

1 个答案:

答案 0 :(得分:0)

我有一个非常相似的情况,即使在IntelliJ中一切正常,其他模块的类也无法解析。

我设法通过显式添加来解决

@ComponentScan(basePackages = ["my.package"])

到我的spring boot应用程序。

还可以这样设置库项目:

tasks.findByName("bootJar")?.apply {
    enabled = false
}

tasks.findByName("jar")?.apply {
    enabled = true
}