Oops, turns out that code depends on the grails-app classes being in the
current directory. I reworked things so when it builds the domain class jar
it creates a text file listing domain classes, so GormHelper knows what
classes to add to the application.
It's working great, and I even wrote a Swing app to test it. I wrote it up
here:
http://burtbeckwith.com/blog/?p=72Burt
On Sunday 07 September 2008 2:12:03 am Burt Beckwith wrote:
> Sorry, I missed the part about running 'grails compile' in your previous
> email. It's working fine now - cool stuff.
>
> Here's a reworked version of buildApplication that discovers files:
>
> private static void buildApplication() {
> def groovyFiles = new
> PathMatchingResourcePatternResolver().getResources(
> 'classpath*:**grails-app/**/*.groovy')
> def domainClasses = []
> for (file in groovyFiles) {
> String className = GrailsResourceUtils.getClassName(file)
> domainClasses << GormHelper.classLoader.loadClass(className)
> }
>
> application = new DefaultGrailsApplication(
> domainClasses as Class[],
> new GroovyClassLoader(GormHelper.classLoader))
> application.initialise()
> }
>
> Burt
>
> On Sunday 07 September 2008 12:22:01 am Burt Beckwith wrote:
> > Interesting progress. I couldn't get it to run - are you running from the
> > commandline using Ant? I'm not seeing how the classloader will find the
> > classes if they're not under src/groovy.
> >
> > Btw - the array problem is due to Groovy thinking you're defining a
> > closure, but this will work:
> >
> > def domainClasses = [bookClass, carClass, personClass, songClass]
> > application = new DefaultGrailsApplication(domainClasses as Class[],
> > new GroovyClassLoader(GormHelper.classLoader))
> >
> > Burt
> >
> > On Saturday 06 September 2008 11:37:17 pm Gregory Bond wrote:
> > > playing around some more i've managed to get burt's entire example to
> > > run in a jar file without any groovy scripts in the jar (!) -
> > > unfortunately it's not a general solution but perhaps what i've done
> > > will help achieve one
> > >
> > > the major change i made is to explicitly load the domain classes into
> > > the application - i've attached a zip with the updated code but here's
> > > a quick overview of what i've done (it's not much really):
> > >
> > > 1) i moved Main.groovy from the scripts dir to src/groovy in order to
> > > get it to be compiled
> > >
> > > 2) i've replaced occurrences of classLoader.getResource(...groovy
> > > files...) with appropriate variations on classLoader.loadClass()
> > >
> > > 3) instead of creating the application this way:
> > >
> > > application = new DefaultGrailsApplication(groovyFiles)
> > >
> > > i'm using this (cumbersome, non-general, but working) approach:
> > >
> > > Class bookClass =
> > > GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Book")
> > > Class carClass =
> > > GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Car")
> > > Class personClass =
> > > GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Person")
> > > Class songClass =
> > > GormHelper.classLoader.loadClass("com.burtbeckwith.gorm.domain.Song")
> > > Class[] domainClasses = new Class[4]
> > > domainClasses[0] = bookClass
> > > domainClasses[1] = carClass
> > > domainClasses[2] = personClass
> > > domainClasses[3] = songClass
> > > application = new
> > > DefaultGrailsApplication(domainClasses, new
> > > GroovyClassLoader(GormHelper.classLoader))
> > >
> > > (as a sidenote that probably highlights my ignorance of grails and
> > > groovy: i kept getting compilation errors when i tried to initialize
> > > the domain class array when i declared it e.g. using syntax 'Class[]
> > > domainClasses = new Class[] { bookClass, .... }')
> > >
> > > greg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email