Search code examples
javamicronaut

Loading @Singleton Beans from external jar


I'm trying to load into my micronaut CLI application (cli.jar) some classes from an external jar, annotated with @singleton in hope that they will be injected in the application. That doesn't happen, even though the classes actually packaged (shadowed) in the cli.jar. I tried to see with java -verbose, whether the classes in the class loader and they are not.

Can it work with micronaut?


Solution

  • Here is the solution according to Graeme's hint. Here is a build.gradle with the working setup:

    plugins {
        id 'java'
        id "io.spring.dependency-management" version "1.0.6.RELEASE"
    }
    
    group 'xyz.transformercli'
    version '1.0-SNAPSHOT'
    
    sourceCompatibility = 11
    
    repositories {
        mavenCentral()
    }
    
    dependencyManagement {
        imports {
            mavenBom 'io.micronaut:micronaut-bom:1.0.3'
        }
    }
    
    dependencies {
        annotationProcessor "io.micronaut:micronaut-inject-java"
        //my jar with base classes
        compile files('libs/transformer-cli-0.1.jar')
        compile "io.micronaut:micronaut-inject"
    }