No suitable classloader found for grab

The Problem

Looking at the source code, this exception is thrown whenever the supplied ClassLoader's name (or it's superclasses) is not groovy.lang.GroovyClassLoader or org.codehaus.groovy.tools.RootLoader. i.e. The target classloader must be an instance of the aforementioned classes (a bit restrictive IMHO).

A Solution

Currently I don't know how to configure a specific classloader using @Grape/@Grab/@GrabConfig annotations. The closest would be to use @GrabConfig(systemClassLoader=true), and ensure the System classloader is an instance of one of the above ClassLoader classes.

If anyone does know, please let me know (and I'll update this answer).

A Workaround

The following code will programmatically download your Grapes, and load them into the supplied GroovyClassLoader (admittedly, not quite what you want).

def loadGrapes(){
    ClassLoader classLoader = new groovy.lang.GroovyClassLoader()
    Map[] grapez = [[group : 'org.ccil.cowan.tagsoup', module : 'tagsoup', version : '1.2']]
    Grape.grab(classLoader: classLoader, grapez)
    println "Class: " + classLoader.loadClass('org.ccil.cowan.tagsoup.jaxp.SAXParserImpl')
}

Using @Grab makes code untestable, at least as of 01/26/2011.