4

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.

Cola_Colin
  • 415
  • 5
  • 16
  • You should probably use `create-plugin` instead of `create-app`. Also, did import HtRole correctly using the correct namespace? – Alexander Kerchum Mar 09 '16 at 18:54
  • 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 Mar 09 '16 at 18:55
  • 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: https://grails.github.io/grails-doc/latest/guide/single.html#creatingAndInstallingPlugins It clearly says to create a plugin via create-app @AlexanderKerchum: HtRole is correctly imported, it compiles fine – Cola_Colin Mar 09 '16 at 18:56
  • 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 Mar 09 '16 at 18:57
  • Yep, that was a mistake I made in explaining what I did, I have a compile dependency to the plugin project – Cola_Colin Mar 09 '16 at 19:01
  • Can you make a project that mimcs this (or simply this project if there isn't any proprietary code yet) and upload it to github so I can see what is going on. There is a lot of configuration that makes it difficult to explain everything in SO. – Alexander Kerchum Mar 09 '16 at 19:09
  • Here is a new example, with a new issue. I used create-plugin this time, maybe that did fix the GORM issue. I dunno. By now I think there are maybe 5 issues or so that play basically hide and seek with me. At least one of them always shows up and prevents me from getting anything to completely work. https://github.com/ColaColin/grailsmultiprojectissues – Cola_Colin Mar 09 '16 at 19:57
  • https://github.com/KerchumA222/grailsmultiprojectissues is where I'll be making changes to show you. – Alexander Kerchum Mar 09 '16 at 20:33

2 Answers2

4

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

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
osborp
  • 362
  • 1
  • 3
  • 9
  • Stupid, stupid, stupid. Of course, the error hinted that GORM was in fact not configured properly. I should have wondered the moment I saw the data source config missing from my plugin. This fixed my issues with my ``3.1.11`` ``web-plugin`` – Michael Jess Sep 26 '16 at 05:50