问题
I have a Micronaut project configured to use GORM and Groovy (1). This project contains lots of domain classes that are working perfectly, persisting data in a MySQL database as expected.
Now I wish to make this domain classes common to another Micronaut project (2).
I tried building a JAR file containing only the domain package and including it in the project 2 thru build.gradle. The classes are compiled and made accessible in code, and I'm able to call GORM methods like findBy, createCriteria, etc. Is also good to mention that all project 2's domain classes are annotated with @Entity.
But, when I run the project (using IntelliJ) and hit some of this code I get:
Either class [com.project2.domain.Foo] is not a
domain class or GORM has not been initialized correctly or has already
been shutdown. Ensure GORM is loaded and configured correctly before
calling any methods on a GORM entity.
I know that GORM is well configured and initialized because I created a "local" domain class inside project 2 and things worked fine.
Before asking this question I saw this one and maybe it can bring some inspiration: Importing domain classes from GORM-standalone module into Grails
EDIT 1: Current build.gradle dependencies:
dependencies {
compile "io.micronaut.configuration:micronaut-kafka"
compile "io.micronaut.configuration:micronaut-hibernate-validator"
compile("io.micronaut.configuration:micronaut-hibernate-gorm") {
exclude group: 'org.grails', module: 'grails-datastore-gorm-hibernate5'
}
compile "org.grails:grails-datastore-gorm-hibernate5:6.1.9.RELEASE"
compile "io.micronaut:micronaut-http-client"
compile "io.micronaut:micronaut-http-server-netty"
compile "io.micronaut:micronaut-runtime-groovy"
compile "io.micronaut:micronaut-validation"
compileOnly "io.micronaut:micronaut-inject-groovy"
runtime "ch.qos.logback:logback-classic:1.2.3"
runtime "org.apache.tomcat:tomcat-jdbc"
runtime "mysql:mysql-connector-java:6.0.6"
testCompile "io.micronaut:micronaut-inject-groovy"
testCompile("org.spockframework:spock-core") {
exclude group: "org.codehaus.groovy", module: "groovy-all"
}
compile files('src/main/resources/libs/my-domain-lib.jar')
}
Thanks in advance!
回答1:
I'm using a similar setup.
I have a core-lib with GORM-standalone, and couple of (Vert.x) verticles and Grails apps using those via gradle's compile project() or compile dep:from-artifactory:0.1-SNAPSHOT directives.
In order to make it possible I needed to:
1) Make sure that each domain class is annotated with grails.gorm.annotation.Entity
2) Tweak Grails' Application.groovy like that:
class Application extends GrailsAutoConfiguration {
@Override
protected boolean limitScanningToApplication() {
false
}
@Override
Collection<String> packageNames() {
super.packageNames() + 'your.domainclass.package'
}
}
3) For no-Grails projects I needed to find and initialize the domain classes myself, and for each DB it can be different (mine is for Mongo-GORM)
ClassPathScanningCandidateComponentProvider compProvider = new ClassPathScanningCandidateComponentProvider( false )
compProvider.addIncludeFilter new AnnotationTypeFilter( Entity )
def domainClasses = compProvider.findCandidateComponents( 'io.my.domain' ).collect{ BeanDefinition bd -> Class.forName bd.beanClassName }
new MongoDatastore( config, *domainClasses )
来源:https://stackoverflow.com/questions/53621770/how-to-import-domain-classes-from-jar-into-a-micronaut-project