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>Classes are defined in Groovy similarly to Java. Methods can be class (static) or instance based and can be public, protected, private and support all the usual Java modifiers like synchronized. <span style="color: black;">Package</span> and class imports use the Java syntax (including static imports). Groovy automatically imports the following:</p> <ul> <li>java.lang</li> <li>java.io</li> <li>java.math</li> <li>java.net</li> <li>java.util</li> <li>groovy.lang</li> <li>groovy.util</li> </ul> <p>One difference between Java and Groovy is that by default things are public unless you specify otherwise.</p> <p>Groovy also merges the idea of fields and properties together to make code simpler, please refer to the <a class="confluence-link" href="/display/GROOVY/Groovy+Beans" data-linked-resource-id="2716" data-linked-resource-type="page" data-linked-resource-default-alias="Groovy Beans" data-base-url="http://docs.codehaus.org">Groovy Beans</a> section for details of how they work.</p> <p>Each class in Groovy is a Java class at the bytecode / JVM level. Any methods declared will be available to Java and vice versa. You can specify the types of parameters or return types on methods so that they work nicely in normal Java code. Also you can implement interfaces or overload Java methods using this approach.</p> <p>If you omit the types of any methods or properties they will default to java.lang.Object at the bytecode/JVM level.</p> <p>You can also use another class implemented in Groovy. e.g.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> //Callee.groovy class Callee { void hello() { println "hello, world" } } </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> //Caller.groovy c = new Callee() c.hello() </pre></td></tr></table> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> groovy -cp . caller </pre></td></tr></table> <p>Make sure the classpath is OK.</p> <h2>Scripts</h2> <p>Groovy supports plain scripts, which do not have a class declaration. Imports are supported at the front of a script in the same way that they can be a the front of a class. Here's the hello world script:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> println "Nice cheese Gromit!" </pre></td></tr></table> <p>You can <a class="confluence-link" href="/display/GROOVY/Running" data-linked-resource-id="2787" data-linked-resource-type="page" data-linked-resource-default-alias="Running" data-base-url="http://docs.codehaus.org">run</a> scripts in the <a class="confluence-link" href="/display/GROOVY/Running#Running-interactiveterminal" data-anchor="interactive+terminal" data-linked-resource-id="2787" data-linked-resource-type="page" data-linked-resource-default-alias="Running#interactive+terminal" data-base-url="http://docs.codehaus.org">interactive terminal</a>, from the <a class="confluence-link" href="/display/GROOVY/Running#Running-commandline" data-anchor="command+line" data-linked-resource-id="2787" data-linked-resource-type="page" data-linked-resource-default-alias="Running#command+line" data-base-url="http://docs.codehaus.org">command-line</a>, in your <a class="confluence-link" href="/display/GROOVY/Running#Running-ide" data-anchor="ide" data-linked-resource-id="2787" data-linked-resource-type="page" data-linked-resource-default-alias="Running#ide" data-base-url="http://docs.codehaus.org">IDE</a>, as a <a class="confluence-link" href="/display/GROOVY/Running#Running-unixscripts" data-anchor="unix+scripts" data-linked-resource-id="2787" data-linked-resource-type="page" data-linked-resource-default-alias="Running#unix+scripts" data-base-url="http://docs.codehaus.org">Unix script</a>, or <a class="confluence-link" href="/display/GROOVY/Embedding+Groovy" data-linked-resource-id="2752" data-linked-resource-type="page" data-linked-resource-default-alias="Embedding Groovy" data-base-url="http://docs.codehaus.org">embeded</a> in your own Java code.</p> <p>If you compile the above script to bytecode using <a class="confluence-link" href="/display/GROOVY/Compiling+Groovy" data-linked-resource-id="2735" data-linked-resource-type="page" data-linked-resource-default-alias="Compiling Groovy" data-base-url="http://docs.codehaus.org">groovyc</a>, you get a single class named after the name of the script. e.g. if this was saved in Foo.script you'd get a Foo.class file.<br /> You can run this Java code on the command line (assuming you're classpath has groovy.jar and asm.jar).</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> java Foo </pre></td></tr></table> <p>This will execute the autogenerated main(String[] args) method in the bytecode which instantiates the Foo class, which extends the </p> <p><table class="wysiwyg-macro" data-macro-name="unmigrated-inline-wiki-markup" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3VubWlncmF0ZWQtaW5saW5lLXdpa2ktbWFya3VwOmF0bGFzc2lhbi1tYWNyby1vdXRwdXQtdHlwZT1CTE9DS30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>{link:Script|http://groovy.codehaus.org/apidocs/groovy/lang/Script.html}{link}</pre></td></tr></table> class and then call its <em>run()</em> method. You may also use this class directly in Java code, passing in variables to the script.</p> <table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="java" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6amF2YX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import groovy.lang.Binding; import groovy.lang.Script; public class UseFoo { public static void main(String[] args) { // lets pass in some variables Binding binding = new Binding(); binding.setVariable("cheese", "Cheddar") binding.setVariable("args", args) Script foo = new Foo(binding); foo.run(); } } </pre></td></tr></table> <p>There's no need to use a Binding if you don't want to; Foo will have a no-argument constructor as well. Though using a Binding you can easily pass in variables. After the end of the script any variables created will be in the Binding for you to access in Java.</p> <p>Unlike classes, variables are not required to be declared (<em>def</em> is not required) in scripts. Variables referenced in a script are automatically created and put into the Binding.</p> <h2>Scripts and functions</h2> <p>If you just want to write some simple scripts and need some simple functions you can declare functions without writing a class. <br /> One difference from normal class-based groovy is that the <em>def</em> keyword is required to define a function outside of a class.</p> <p>Here's an example of a simple script with a function. Note that if ever you need things like static or instance variables and so forth then maybe its time to actually write a class <img class="emoticon emoticon-smile" data-emoticon-name="smile" border="0" src="/s/en_GB/3278/15/_/images/icons/emoticons/smile.png" alt="(smile)" title="(smile)" /></p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> def foo(list, value) { println "Calling function foo() with param ${value}" list << value } x = [] foo(x, 1) foo(x, 2) assert x == [1, 2] println "Creating list ${x}" </pre></td></tr></table>
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