views:

692

answers:

5

I am working on some code to load some bootstrapping data into my Grails app. Something is not working with one of the classes I am trying to create, so it would be very convenient to be able to run that code interactively against the grails runtime environment and I am wondering if there is a way to do that.

I know about the Grails console, but that doesn't seem to load the bootstrapped data that I want to interact with. I also saw this thread on debugging - do any of the IDEs allow interactive shells into the runtime? It seems like Debug Plugin has plans to offer this, but is not there yet.

I found this script that allows you to execute a script from Grails context, but I'd like something more interactive.

I am on Grails 1.1.

+5  A: 

Using the Grails console run your bootstrap class manually, then run the code in question.

new BootStrap().init()
codeLes
Here's snippet to run the init closure:new BootStrap().init()
chanwit
thanks chanwit! I should have made such a brief answer community editable... next time I will.
codeLes
+4  A: 

For Grails 1.1 there is a web-based console plugin which allowes to execute code against a running grails instance via a web interface:

http://grails.org/Console+Plugin

Siegfried Puchbauer
+2  A: 

Finally found exactly what I needed to do!

If you add to your bootstrap the following code:

 def init = {servletContext ->
    if (GrailsUtil.environment == GrailsApplication.ENV_DEVELOPMENT) {
      def appCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
      def grailsApp = appCtx.getBean(GrailsApplication.APPLICATION_ID);
      Binding b = new Binding();
      b.setVariable("ctx", appCtx);
      def console = new Console(grailsApp.classLoader, b);
      console.run()
    }   }

When the application starts up it will also open up a Groovy Console, which will allow you to interact with the domain classes interactively. So, for example, you can add a new object, and then view it in the web app.

Jean Barmash
+1  A: 

I use IDEA. I've created a controller titled testController just for this type of "experimentation". It works fine for me. You can update code on controller and retest using degub in IDEA and calling controller via URL without redeploying your app.

albertoprb
A: 

You can open a debug session to your Grails app from your IDE. When you hit a breakpoint, you can run arbitrary code right from your IDE. In IntelliJ IDEA you open this Code fragment evaluation panel by invoking Run -> Evaluate Expression. Eclipse an NetBeans probably have the same feature.

rlovtang
Don't think Eclipse (or STS) have such an "Evaluate Expression" feature which will work with Groovy code. Wish it did.
Scott Warren