kotlingradlemicronautmulti-module

Micronaut multi module project - Error instantiating bean of type [XXX]


I am creating my first micronaut gradle multimode application

Project structure

Root project 'finance'
+--- Project ':application'
\--- Project ':main'

root build.gradle.kts

plugins {
    id("org.jetbrains.kotlin.jvm") version "1.8.22"
    id("org.jetbrains.kotlin.plugin.allopen") version "1.8.22"

}

version = "0.1"
group = "com.jupemo.finance"

repositories {
    mavenCentral()
}

subprojects {

    apply(plugin = "org.jetbrains.kotlin.jvm")
    apply(plugin = "org.jetbrains.kotlin.plugin.allopen")


    repositories {
        mavenCentral()
    }


    java {
        sourceCompatibility = JavaVersion.toVersion("17")
    }

    dependencies {
        runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
        runtimeOnly("org.yaml:snakeyaml")

        compileOnly("org.projectlombok:lombok:1.18.30")
        annotationProcessor("org.projectlombok:lombok:1.18.30")
    }


    tasks {
        compileKotlin {
            compilerOptions {
                jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
            }
        }
        compileTestKotlin {
            compilerOptions {
                jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
            }
        }
    }

}

main build.gradle.kts

plugins {
    id("com.google.devtools.ksp") version "1.8.22-1.0.11"
    id("com.github.johnrengelman.shadow") version "8.1.1"
    id("io.micronaut.application") version "4.0.4"
    id("io.micronaut.aot") version "4.0.4"
}




val kotlinVersion=project.properties.get("kotlinVersion")

dependencies {
    ksp("io.micronaut.serde:micronaut-serde-processor")
    implementation("io.micronaut.serde:micronaut-serde-jackson")
    implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
    runtimeOnly("ch.qos.logback:logback-classic")

    implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
    ksp("io.micronaut:micronaut-http-validation") // move to rest
    compileOnly("io.micronaut:micronaut-http-client") // move to rest
    testImplementation("io.micronaut:micronaut-http-client") // move to rest
    implementation("io.micronaut:micronaut-management")

    implementation(project(":application"))

}

application {
    mainClass.set("com.jupemo.finance.ApplicationKt")
}


graalvmNative.toolchainDetection.set(false)
micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("com.jupemo.*")
    }
    aot {
    // Please review carefully the optimizations enabled below
    // Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
        optimizeServiceLoading.set(false)
        convertYamlToJava.set(false)
        precomputeOperations.set(true)
        cacheEnvironment.set(true)
        optimizeClassLoading.set(true)
        deduceEnvironment.set(true)
        optimizeNetty.set(true)
    }
}

application build.gradle.kts

plugins {
    id("io.micronaut.library") version "4.0.4"
}
./gradlew build

run successful

./gradlew run

the application started successfully

curl http://localhost:8080/hello

Running the curl I got the following error

Message: com.jupemo.finance.controller.FinanceController
Path Taken: new FinanceController(UseCase useCase)
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [com.jupemo.finance.controller.FinanceController]

Message: com.jupemo.finance.controller.FinanceController
Path Taken: new FinanceController(UseCase useCase)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2322)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2277)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2289)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3056)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:81)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistratio
...
Caused by: java.lang.InstantiationError: com.jupemo.finance.controller.FinanceController
        at com.jupemo.finance.controller.$FinanceController$Definition.instantiate(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2307)
        ... 80 common frames omitted

I am not able to instantiate the UseCase bean

repository -> https://github.com/jupemo/finance

I am trying yo run the application successfully to better understand micronaut build in gradle


Solution

  • The problem was that the ksp plugin was not well-defined.

    Without the ksp plugin, it was not possible to do the annotations processing and for that reason, the program was not reaching the bean

    repository ksp plugin https://github.com/google/ksp

    Working gradle

    root gradle:

    plugins {
        id("org.jetbrains.kotlin.jvm")
        id("org.jetbrains.kotlin.plugin.allopen")
        id("com.google.devtools.ksp")
    }
    
    repositories {
        mavenCentral()
    }
    
    subprojects {
        repositories {
            mavenCentral()
        }
    
        apply(plugin = "org.jetbrains.kotlin.jvm")
        apply(plugin = "org.jetbrains.kotlin.plugin.allopen")
        apply(plugin = "com.google.devtools.ksp")
    
        java {
            sourceCompatibility = JavaVersion.toVersion("17")
        }
    
        tasks {
            compileKotlin {
                compilerOptions {
                    jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
                }
            }
            compileTestKotlin {
                compilerOptions {
                    jvmTarget.set(org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17)
                }
            }
        }
    }
    

    main gradle

        plugins {
        id("com.github.johnrengelman.shadow")
        id("io.micronaut.application")
        id("io.micronaut.aot")
    }
    
    version = "0.1"
    group = "com.jupemo.finance"
    
    val kotlinVersion=project.properties.get("kotlinVersion")
    
    dependencies {
        ksp("io.micronaut:micronaut-http-validation")
        ksp("io.micronaut.serde:micronaut-serde-processor")
        implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
        implementation("io.micronaut.serde:micronaut-serde-jackson")
        implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
        compileOnly("io.micronaut:micronaut-http-client")
        runtimeOnly("ch.qos.logback:logback-classic")
        runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
        testImplementation("io.micronaut:micronaut-http-client")
        runtimeOnly("org.yaml:snakeyaml")
        annotationProcessor("io.micronaut:micronaut-inject-java")
    
        implementation(project(":application"))
    }
    
    
    application {
        mainClass.set("com.jupemo.finance.ApplicationKt")
    }
    
    graalvmNative.toolchainDetection.set(false)
    micronaut {
        runtime("netty")
        testRuntime("junit5")
        processing {
            incremental(true)
            annotations("com.jupemo.finance.*")
        }
        aot {
        // Please review carefully the optimizations enabled below
        // Check https://micronaut-projects.github.io/micronaut-aot/latest/guide/ for more details
            optimizeServiceLoading.set(false)
            convertYamlToJava.set(false)
            precomputeOperations.set(true)
            cacheEnvironment.set(true)
            optimizeClassLoading.set(true)
            deduceEnvironment.set(true)
            optimizeNetty.set(true)
        }
    }
    

    application gradle:

    plugins {
        id("io.micronaut.library")
    }
    
    version = "0.1"
    group = "com.jupemo.finance"