2
votes

I'm trying to create a sample project where domain classes are in an external normal groovy project and then used in a grails app (see https://github.com/ivanarrizabalaga/grails-domain-griffon):

  • book-svr
  • book-common

I'm also following the grails guide in order to get this (see http://grails.org/doc/latest/guide/hibernate.html) but the imported classes are not being recognized as domain classes.

The relevant parts:

External domain class:

package com.nortia.book
import grails.persistence.Entity

@Entity
class Book implements Serializable{

    private static final long serialVersionUID = 1L;

    String title
    String author

    static constraints = {
        title blank:false
        author blank:false
    }
}

build.gradle:

....
dependencies {
    // We use the latest groovy 2.x version for building this library
    compile 'org.codehaus.groovy:groovy:2.1.7'

    compile "org.grails:grails-datastore-gorm-hibernate4:3.0.0.RELEASE"
    compile "org.grails:grails-spring:2.3.7"
....

In the grails app,

hibernate.cfg.xml:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    '-//Hibernate/Hibernate Configuration DTD 3.0//EN'
    'http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd'>

<hibernate-configuration>
    <session-factory>
        <mapping package='com.nortia.book' />       
        <mapping class='com.nortia.book.Book' />
    </session-factory>
</hibernate-configuration>

BookController.groovy (I've tried an scaffolded and coded controller, and both failed):

...
class BookController{
    static scaffold=Book
}
...

console.log (error):

ERROR ScaffoldingGrailsPlugin  - Cannot generate controller logic for scaffolded class class com.nortia.book.Book. It is not a domain class!

Finally, suspicious log messages while initializing:

DEBUG cfg.Configuration  - session-factory config [null] named package [com.nortia.book] for mapping
INFO  cfg.Configuration  - Mapping package com.nortia.book
WARN  cfg.AnnotationBinder  - Package not found or wo package-info.java: com.nortia.book
DEBUG cfg.Configuration  - session-factory config [null] named class [com.nortia.book.Book] for mapping
INFO  cfg.Configuration  - Configured SessionFactory: null

So I'm wondering:

  • What's the missing piece?
  • According to the docs, looks like external 'domains' must be java classes but that's not a good option for my purpose.
  • I haven't try yet with a grails binary plugin instead of a groovy project, is it the way to go? (I need to use the domains in a griffon project that is why I opted this way first).
1

1 Answers

4
votes

Finally solve it creating a 'sort of' a binary plugin manually.

Let's take a look step by step:

book-domain

Tree structure

src
  main
    groovy
      demo
        Book.groovy
    resources
      META-INF
        grails-plugin.xml
build.gradle

Book.groovy

package demo

import grails.persistence.Entity

@Entity
class Book{

    String title

    static constraints = {
        title blank: false
    }
}

grails-plugin.xml

<plugin name='book-domain' version='1.0' grailsVersion='2.3 &gt; *'>
  <author>Ivan Arrizabalaga</author>
  <title>External domains</title>
  <description>An external domain plugin</description>
  <documentation>http://grails.org/plugin/book-domain</documentation>
  <type>demo.BookDomainGrailsPlugin</type>
  <packaging>binary</packaging>
  <resources>
    <resource>demo.Book</resource>
  </resources>
</plugin>

build.gradle

/*
 * This build file was auto generated by running the Gradle 'init' task
 * by 'arrizabalaga' at '5/26/14 12:34 PM' with Gradle 1.11
 *
 * This generated file contains a sample Groovy project to get you started.
 * For more details take a look at the Groovy Quickstart chapter in the Gradle
 * user guide available at http://gradle.org/docs/1.11/userguide/tutorial_groovy_projects.html
 */

// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
apply plugin: 'maven'

group = 'demo'
version = '1.0'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use 'maven central' for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    mavenCentral()
    mavenLocal()
}

// In this section you declare the dependencies for your production and test code
dependencies {
    // We use the latest groovy 2.x version for building this library
    compile 'org.codehaus.groovy:groovy-all:2.1.9'
    //compile "org.grails:grails-datastore-gorm-hibernate4:3.1.0.RELEASE"
    compile "org.grails:grails-datastore-gorm-hibernate:3.1.0.RELEASE"
    compile "commons-lang:commons-lang:2.6"

    // We use the awesome Spock testing and specification framework
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
    testCompile 'junit:junit:4.11'
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    classifier = 'javadoc'
    from javadoc.destinationDir
}

artifacts {
    archives sourcesJar
    archives javadocJar
}

using it

Now the project can be built (gradle install to publish it into your local maven repo) and used (declaring the proper dependency) in any given project.

If the project that uses the jar is a Grails app it turns automatically the @Entity classes into real Domains.

Hope it helps.