Search code examples
groovymicronaut

can we import the specified classes jar file from another mincroanut project to an micronaut project


I have 2 micronaut (groovy ) projects , called project A and project B

Project B has controllers and services ( package com.service , com.controller ) but I only created jar from package com.service the code in com.service package has @Singleton annotation and @Scheduled

and I has enabled annotation processing as link (https://docs.micronaut.io/latest/guide/index.html#ideaSetup) to both projects please see my gradle code below to generate JAR file ( the output file is project-b-libs-0.x.jar)

task createLibraryJar(type: Jar) {
    baseName( getArchivesBaseName() + "-libs")
    from sourceSets.main.output
    includeEmptyDirs = false
    include '**/service/**/*.class'
}

Then I added proejct-b-libs-0.x.jar to Project A The gradle's dependencies are below

dependencies {
annotationProcessor "io.micronaut:micronaut-inject-java"
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.groovy:micronaut-runtime-groovy")
implementation("javax.annotation:javax.annotation-api")
implementation("io.micronaut:micronaut-http-client")
runtimeOnly("ch.qos.logback:logback-classic")
compileOnly files('libs/project-b-libs-0.4.jar')
compile "io.micronaut:micronaut-inject"
}

Finally after I tried call @Inject Object from the class in JAR file, it showed error on run time

Caused by: io.micronaut.context.exceptions.BeanContextException: Error loading bean [com.service.TestService]: com/service/StripePaymentService

Project A has com.service.TestService to call com.service.StripePaymentService which is in JAR file

Sorry for my English and Thank you to trying to understand me


Solution

  • compile files('libs/project-b-libs-0.4.jar')
    

    I just use compile , not compileOnly , and can not use jar file from gralde script above when I extract jar file , some mincronaut's stuff is missing

    so I try another gradle task's script as below ( change from include to exclude )

    task createLibraryJar(type: Jar) {
    
        baseName( getArchivesBaseName() + "-libs")
        from sourceSets.main.output
        includeEmptyDirs = false
        exclude '**/controller/**/*.class'
    
    }
    

    and it works because the micronaut's stuff still in JAR file, I only exclude unnecessary classes from my jar file