Command Line Tools

The Grails console

Grails ships with an extended version of the regular Groovy console. To run the console simply type the following command from the root of a Grails project:

grails console

The console is a Swing GUI interface that lets you type arbitrary commands and execute them. It is a good way to learn Grails and try out the various dynamic finders available on your domain model.

The Grails interactive shell

In addition to the console there is the interactive shell. It works similar to the console but with a text UI only. To run the shell simply type the following command from the root of a Grails project:

grails shell

There are instructions on how to use it when it loads, but essentially you type in the commands you want to execute separated by carriage returns and you then type go followed by a carriage return to execute the commands.

General usage

Both console and shell are useful for working interactively with your Grails application:

  • find domain objects with the help of dynamic finder methods
  • call mutator methods on domain objects once you have a reference to them
  • save() or delete() domain objects like you would otherwise do in a controller
  • ... more to come here
    Such an interactive work can be helpful for debugging purposes, for verifying your assumptions about the object state and behavior, to test the effect of dynamic finder methods before putting the code in a controller.

Below is a transcript of an example session with a grails shell for a fictious book store application.

.... more to come here

Packaging a WAR

To package a WAR file for deployment onto an application server you can do:

grails war

By default this will package for a production environment you can change this by doing:

grails test war // for test enviroment
grails dev war  // for development environment
grails prod war // for production

If you want to package for X other environment you can do:

grails -Dgrails.env=blah war

A full list of the available targets is in the Command Line

Labels

 
(None)
  1. Feb 01, 2008

    Dave Fuller says:

    The grails shell should include the ability to call "grails shell <scriptname>" ...

    The grails shell should include the ability to call "grails shell <script-name>" and have the script executed within the shell and then have the shell exit.  This would save a tremendous amount of effort as opposed to what is involved to do something with gant scripts when you want to import/export objects within the context of the application.

     As an example, I could have something like:
    --------------ImportBook.groovy--------------
    b = new Book()
    b.title = "War and Peace"
    b.validate
    b.save()
    ---------------------------------------------------

    And at the command line, call "grails shell scripts/ImportBook.groovy" and have the script executed within the application context.  This would eliminate having to get a session manager, etc. which is just silly when the shell has everything you need.