Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>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</p><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjppbnRlcmFjdGl2ZStjb25zb2xlfQ&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="interactive+console"></p><h2>Using the interactive console</h2><p>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.</p><p>If you <a class="confluence-link" href="/display/GROOVY/Installing+Groovy" data-linked-resource-id="2754" data-linked-resource-type="page" data-linked-resource-default-alias="Installing Groovy" data-base-url="http://docs.codehaus.org">install</a> a binary distribution of Groovy then you can run the Groovy Swing console by typing this on the command line.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovyConsole </pre></td></tr></table><p>For a command line interactive shell type</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovysh </pre></td></tr></table><p>To see how to add things to the classpath see below.</p><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjppZGV9&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="ide"></p><h2>Running Groovy scripts from your IDE</h2><p>There is a helper class called <a href="http://groovy.codehaus.org/api/groovy/lang/GroovyShell.html">GroovyShell</a> which has a main(String[]) method for running any Groovy script. You can run any groovy script as follows</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>java groovy.lang.GroovyShell foo/MyScript.groovy [arguments] </pre></td></tr></table><p>You can then run the above Groovy main() in your IDE to run or debug any Groovy script.</p><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjpjb21tYW5kK2xpbmV9&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="command+line"></p><h2>Running Groovy scripts from the command line</h2><p>There are shell scripts called 'groovy' or 'groovy.bat' depending on your platform which is part of the Groovy runtime.<br /> Once the runtime is <a class="confluence-link" href="/display/GROOVY/Installing+Groovy" data-linked-resource-id="2754" data-linked-resource-type="page" data-linked-resource-default-alias="Installing Groovy" data-base-url="http://docs.codehaus.org">installed</a> you can just run groovy like any other script...</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>groovy foo/MyScript.groovy [arguments] </pre></td></tr></table><p>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.</p><ul><li>Classes with a main method of course,</li><li>Classes extending GroovyTestCase are run with JUnit's test runner,</li><li>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.</li></ul><p>To work from the latest and greatest Groovy see <a class="confluence-link" href="/display/GROOVY/Building+Groovy+from+Source" data-linked-resource-id="77733" data-linked-resource-type="page" data-linked-resource-default-alias="Building Groovy from Source" data-base-url="http://docs.codehaus.org">Building Groovy from Source</a>. Once built you'll then have a full binary distribution made for you in <code>groovy/target/install</code>. You can then add <code>groovy/target/install/bin</code> to your path and you can then run groovy scripts easily from the command line.</p><p>To see how to add things to the classpath see below.</p><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjp1bml4K3NjcmlwdHN9&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="unix+scripts"></p><h2>Creating Unix scripts with Groovy</h2><p>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.</p><p>The following is a sample script, which you should copy and save as helloWorld.groovy.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>#!/usr/bin/env groovy println("Hello world") for (a in this.args) { println("Argument: " + a) } </pre></td></tr></table><p>Then to run the script from the command line, just make sure the script is executable then you can call it.</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>chmod +x helloWorld ./helloWorld </pre></td></tr></table><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjpjbGFzc3BhdGh9&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="classpath"></p><p>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.</p><h2>Adding things to the classpath</h2><p>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:</p><ul><li>Add things to your CLASSPATH environment variable</li><li>Pass -classpath (or -cp) into the command you used to create the shell or run the script</li><li>It's also possible to create a <em>~/.groovy/lib</em> directory and add whatever jars you need in there.</li><li>If the jars you need are in a Maven or Ivy repository, you can "grab" them with <a class="confluence-link" href="/display/GROOVY/Grape" data-linked-resource-id="107806787" data-linked-resource-type="page" data-linked-resource-default-alias="Grape" data-base-url="http://docs.codehaus.org">Grape</a>.</li></ul><p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjpoZWFwIHNpemV9&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="heap size"></p><h2>Increasing Groovy's JVM Heap Size</h2><p>To increase the amount of memory allocated to your groovy scripts, set your JAVA_OPTS environment variable. JAVA_OPTS="-Xmx..."</p><h2>Avoiding PermGen out of memory</h2><p>Groovy creates classes dynamically, but the default Java VM does not GC the PermGen. If you are using Java 6 or later, add <code><span class="pun">-</span><span class="pln">XX</span><span class="pun">:+</span><span class="typ">CMSClassUnloadingEnabled</span><span class="pln"> </span></code>-XX:<code>+UseConcMarkSweepGC</code>. <code>UseConcMarkSweepGC</code> is needed to enable <code><span class="typ">CMSClassUnloadingEnabled</span><span class="pln">.</span></code></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced