Michal
Michal

Reputation: 819

SpringBoot gradle multimodule project Unresolved reference error

I'm trying to create a multimodule springboot application on gradle, but I get an Unresolved reference error message during build. Gradle config for individual modules is below.

Unresolved reference error on import org.example.finalmultimodule.data.repositories.IPersonRepository during build:

enter image description here

Error: enter image description here

Directory tree:

enter image description here

BuildTools > Gradle: enter image description here

Main 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"
        }
    }
}

Core 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"))
}

Data 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"))
}

Business 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")
}

Upvotes: 2

Views: 3472

Answers (1)

avolkmann
avolkmann

Reputation: 3105

I had a very similar situation where my classes from other modules would not be resolved even though everything looked fine in IntelliJ.

I managed to solve it by explicitly adding

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

to my spring boot application.

Also, set library projects up like this:

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

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

Upvotes: 4

Related Questions