Is there a way to access domain objects in a groovy script? I’ve tried importing them as usual, but I still get a NoClassDefFoundError. I’ve also tried adding the package to the depends list, but that does not help either.
I am invoking the script via grails MyScript param1 param2. If I run it through grails run-script scripts/MyScript then I can import fine, but cannot pass params.
I have seen this question that mimics my problem exactly, however, the solution does not seem to work for me.
import foo.Bar
includeTargets << grailsScript("Init")
includeTargets << grailsScript("_GrailsArgParsing")
target(main: "Does Foo") {
depends(configureProxy, classpath, parseArguments)
println argsMap
Bar.get(1)//Throws java.lang.NoClassDefFoundError
}
setDefaultTarget(main)
How can I import Domain classes and access script parameters in my Groovy script?
You have to run your script with run-script not as a normal script
see run-script documentation
I don’t think run-script suppor command line arguments actually look at run script source code
.
You can rewrite a run-script clone that pass arguments to running script as bean (like it already do for grailsApplication). I think it’s not difficult do to.
Or you can use an external properties files included in your config to pass arguments to grails application, and so to running script.