4
votes

I am trying to use a Grails Project as a Plugin to basically have my domain classes in the Plugin and then use them in multiple Grails projects.

I've done this:

grails create-app web

grails create-app plugin

create a settings.gradle in the root directory of both projects with include 'plugin', 'web'

then I added spring security to the plugin and used s2-quickstart to create a user and a role domain class and added some default users to the Bootstrap.groovy.

Starting the plugin project alone doesn't show any issues.

Now I added the plugin as a dependency to the web project: compile (':plugin') This way I can access the domain classes from the plugin inside the web project, it compiles fine. I added the spring config to the application.groovy and am now trying to use the domain classes from the plugin inside the web project.

Trying this however my project does not correctly start and it tells me this:

java.lang.IllegalStateException: Either class [htcommon.HtRole] is not a domain class or GORM has not been initialized correctly or has already been shutdown. If you are unit testing your entities using the mocking APIs

as soon as my code tries to do new HtRole(...).save()

It seems the domain classes from the plugin are not recognized as GORM classes somehow.

2
You should probably use create-plugin instead of create-app. Also, did import HtRole correctly using the correct namespace?Alexander Kerchum
You should create your plugin with grails create-plugin plugin not with create-app. You really should read the documentation about how to create plugins before writing anything more. You've got a lot of things wrong here.Joshua Moore
I tried grails create-app plugin --profile=plugin as well as grails create-plugin All of those created different errors. :s What documentation am I supposed to read? I started with the point about multi project stuff a bit down from here: grails.github.io/grails-doc/latest/guide/… It clearly says to create a plugin via create-app @AlexanderKerchum: HtRole is correctly imported, it compiles fineCola_Colin
Also, compile (':web') should be compile (':plugin') I think. I'd have to see more of your code to give you more information about that.Alexander Kerchum
Yep, that was a mistake I made in explaining what I did, I have a compile dependency to the plugin projectCola_Colin

2 Answers

4
votes

The issue with the domain not being recognized as a GORM class was due to the constructors provided in them. These constructors were generated from s2-quickstart, but should be removed (it's a bug in spring-security-core). I removed the constructors and the one place you were using them I used map style default constructors. Then I fixed the call you had to get the current user.

The repaired source is in this repo on GitHub (patch-1 branch is working, master is the OP's original broken code)

2
votes

I received the same error message when running a plugin containing GORM domains using grails run-app in Grails 3.1.6. I fixed the problem by providing explicit configuration for initialising Hibernate as follows:

build.gradle:

dependencies {
    ...
    runtime "org.grails.plugins:hibernate4"
    runtime "org.hibernate:hibernate-ehcache"
}

grails-app/conf/application.yml:

---
environments:
    development:
        hibernate:
            cache:
                queries: false
                use_second_level_cache: true
                use_query_cache: false
                region.factory_class: 'org.hibernate.cache.ehcache.EhCacheRegionFactory'

         dataSource:
             pooled: true
             jmxExport: true
             driverClassName: org.h2.Driver
             username: sa
             password:
             dbCreate: create-drop
             url: jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE