Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Groovy scripts are a number of statements and class declarations in a text file. Groovy scripts can be used similarly to other scripting languages. There are various ways of running Groovy scripts

Anchor
interactive+console
interactive+console

Using the interactive console

Groovy has a Swing interactive console that allows you to type in commands and execute them rather like using an SQL query tool. History is available and such like so you can move forwards and backwards through commands etc.

If you install a binary distribution of Groovy then you can run the Groovy Swing console by typing this on the command line.

Code Block
none
none
groovyConsole

For a command line interactive shell type

Code Block
none
none
groovysh

To see how to add things to the classpath see below.

Anchor
ide
ide

Running Groovy scripts from your IDE

There is a helper class called GroovyShell which has a main(String[]) method for running any Groovy script. You can run any groovy script as follows

Code Block
none
none
java groovy.lang.GroovyShell foo/MyScript.groovy [arguments]

You can then run the above Groovy main() in your IDE to run or debug any Groovy script.

Anchor
command+line
command+line

Running Groovy scripts from the command line

There are shell scripts called 'groovy' or 'groovy.bat' depending on your platform which is part of the Groovy runtime.
Once the runtime is installed you can just run groovy like any other script...

Code Block
none
none
groovy foo/MyScript.groovy [arguments]

If you are using Groovy built from CVS Head (after Beta-5, see below if you want to upgrade), apart from Groovy scripts, you may also now run different kind of classes from the command-line.

  • Classes with a main method of course,
  • Classes extending GroovyTestCase are run with JUnit's test runner,
  • Classes implementing the Runnable interface are instanciated either with a constructor with String[] as argument, or with a no-args constructor, then their run() method is called.

To work from the latest and greatest Groovy see Building Groovy from Source. Once built you'll then have a full binary distribution made for you in groovy/target/install. You can then add groovy/target/install/bin to your path and you can then run groovy scripts easily from the command line.

To see how to add things to the classpath see below.

Anchor
unix+scripts
unix+scripts

Creating Unix scripts with Groovy

You can write unix scripts with Groovy and execute them directly on the command line as if they were normal unix shell scripts. Providing you have installed the Groovy binary distribution (see above) and 'groovy' is on your PATH then the following should work.

The following is a sample script, which you should copy and save as helloWorld.groovy.

Code Block
none
none
#!/usr/bin/env groovy
println("Hello world")
for (a in this.args) {
  println("Argument: " + a)
}

Then to run the script from the command line, just make sure the script is executable then you can call it.

Code Block
none
none
chmod +x helloWorld
./helloWorld

Anchor
classpath
classpath

Note that you will not be able to provided any arguments to groovy when running with the shebang(#!) on Linux because the arguments are treated as part of the name of the command to run. Mac OS X will interpret additional arguments correctly.

Adding things to the classpath

When running command line scripts or interactive shells you might want to add things to your classpath such as JDBC drivers or JMS implementations etc. To do this, you have a few choices:

  • Add things to your CLASSPATH environment variable
  • Pass -classpath (or -cp) into the command you used to create the shell or run the script
  • It's also possible to create a ~/.groovy/lib directory and add whatever jars you need in there.
  • If the jars you need are in a Maven or Ivy repository, you can "grab" them with Grape.

Anchor
heap size
heap size

Increasing Groovy's JVM Heap Size

To increase the amount of memory allocated to your groovy scripts, set your JAVA_OPTS environment variable. JAVA_OPTS="-Xmx..."

Avoiding PermGen out of memory

Groovy creates classes dynamically, but the default Java VM does not GC the PermGen. If you are using Java 6 or later, add -XX:+CMSClassUnloadingEnabled -XX:+UseConcMarkSweepGC. UseConcMarkSweepGC is needed to enable CMSClassUnloadingEnabled.


Groovy and the Cloud

Groovy can run anywhere the JVM runs - including many cloud platforms, likewise, continuous integration is just as easy. You can kick the tyres at CloudBees for hosting your groovy app, this quick start will get you going (with the correct settings and permgen):

This will setup a CI job (running tests and build, in Jenkins) and deploy the "pet store" app that uses the grails "pet store" demo app (you can use it for free). Prefer to set it up yourself, read here for details.