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
<h2><code><groovy></code></h2> <h3>Description</h3> <p>Executes a series of Groovy statements. Statements can either be read in from a text file using the <em>src</em> attribute or from between the enclosing Groovy tags.</p> <h3>Required taskdef</h3> <p>Assuming groovy-all-VERSION.jar is in <em>my.classpath</em> you will need to declare this task at some point in the build.xml prior to using this task.</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> <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="my.classpath"/> </pre></td></tr></table> <h3><groovy> attributes</h3> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p> Attribute </p></th> <th class="confluenceTh"><p> Description </p></th> <th class="confluenceTh"><p> Required </p></th> </tr> <tr> <td class="confluenceTd"><p> src </p></td> <td class="confluenceTd"><p> File containing Groovy statements.<br /> The directory containing the file is added to the classpath </p></td> <td class="confluenceTd"><p> Yes, unless statements enclosed within tags </p></td> </tr> <tr> <td class="confluenceTd"><p> classpath </p></td> <td class="confluenceTd"><p> the classpath to use </p></td> <td class="confluenceTd"><p> No </p></td> </tr> <tr> <td class="confluenceTd"><p> classpathref </p></td> <td class="confluenceTd"><p> the classpath to use, given as reference to a PATH defined elsewhere </p></td> <td class="confluenceTd"><p> No </p></td> </tr> </tbody></table> <h3>Parameters specified as nested elements</h3> <h4><classpath></h4> <p>Groovy's classpath attribute is a PATH like structure and can also be set via a nested classpath element.</p> <h4><arg> (since 1.1)</h4> <p>Arguments can be set via one or more nested <arg> elements using the standard Ant <a href="http://ant.apache.org/manual/using.html#arg">command line conventions</a>.</p> <h3>Available bindings</h3> <p>A number of bindings are in scope for use within your Groovy statements.</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p> Name </p></th> <th class="confluenceTh"><p> Description </p></th> </tr> <tr> <td class="confluenceTd"><p> ant </p></td> <td class="confluenceTd"><p> an instance of AntBuilder that knows about the current ant project </p></td> </tr> <tr> <td class="confluenceTd"><p> project </p></td> <td class="confluenceTd"><p> the current ant project </p></td> </tr> <tr> <td class="confluenceTd"><p> properties </p></td> <td class="confluenceTd"><p> a Map of ant properties </p></td> </tr> <tr> <td class="confluenceTd"><p> target </p></td> <td class="confluenceTd"><p> the owning target that invoked this groovy script </p></td> </tr> <tr> <td class="confluenceTd"><p> task </p></td> <td class="confluenceTd"><p> the wrapping task, can access anything needed in <a href="http://www.jajakarta.org/ant/ant-1.6.1/docs/ja/manual/api/org/apache/tools/ant/Task.html">org.apache.tools.ant.Task</a> </p></td> </tr> <tr> <td class="confluenceTd"><p> args </p></td> <td class="confluenceTd"><p> command line arguments, if any </p></td> </tr> </tbody></table> <h3>Examples</h3> <p>Hello world, version 1:</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> <groovy> println "Hello World" </groovy> </pre></td></tr></table> <p>Hello world, version 2:</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> <groovy> ant.echo "Hello World" </groovy> </pre></td></tr></table> <p>List all xml files in the current directory:</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> <groovy> xmlfiles = new File(".").listFiles().findAll{ it =~ "\.xml$" } xmlfiles.sort().each { println it.toString() } </groovy> </pre></td></tr></table> <p>List all xml files within a 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> <zipfileset id="found" src="foobar.jar" includes="**/*.xml"/> <groovy> project.references.found.each { println it.name } </groovy> </pre></td></tr></table> <p>To run a 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> <groovy src="/some/directory/some/file.groovy"> <classpath> <pathelement location="/my/groovy/classes/directory"/> </classpath> </groovy> </pre></td></tr></table> <p>To find all the 'Builder' classes having an 'org.*' package within a directory of jars:</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> <property name="local.target" value="C:/Projects/GroovyExamples"/> <groovy> import java.util.jar.JarFile def classes = [] def resourceNamePattern = /org\/.*\/.*Builder.class/ def jarNamePattern = /.*(beta|commons).*jar$/ def libdir = new File("${properties['local.target']}/lib") libdir.listFiles().grep(~jarNamePattern).each { candidate -> new JarFile(candidate).entries().each { entry -> if (entry.name ==~ resourceNamePattern) classes += entry.name } } properties["builder-classes"] = classes.join(' ') </groovy> <echo message='${builder-classes}'/> </pre></td></tr></table> <p>Which might result in something like:</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> org/apache/commons/cli/PatternOptionBuilder.class org/apache/commons/cli/OptionBuilder.class org/codehaus/groovy/tools/groovydoc/GroovyRootDocBuilder.class org/custommonkey/xmlunit/HTMLDocumentBuilder.class org/custommonkey/xmlunit/TolerantSaxDocumentBuilder.class </pre></td></tr></table> <p>FileScanner version of above (with a slight variation on collecting the names):</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> <groovy> import java.util.jar.JarFile def resourceNamePattern = /org\/.*\/.*Builder.class/ def candidates = ant.fileScanner { fileset(dir: '${local.target}/lib') { include(name: '*beta*.jar') include(name: '*commons*.jar') } } def classes = candidates.collect { new JarFile(it).entries().collect { it.name }.findAll { it ==~ resourceNamePattern } }.flatten() properties["builder-classes"] = classes.join(' ') </groovy> </pre></td></tr></table> <p>Here is an example of calling out to a web service from your Ant 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> <?xml version="1.0" encoding="UTF-8"?> <project name="SOAP example" default="main" basedir="."> <property environment="env"/> <property name="celsius" value="0"/> <target name="main"> <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"> <classpath> <fileset dir="${env.GROOVY_HOME}" includes="embeddable/groovy-all-*.jar,lib/ivy*.jar"/> </classpath> </taskdef> <groovy> @Grab('org.codehaus.groovy.modules:groovyws:0.5.1') import groovyx.net.ws.WSClient def url = 'http://www.w3schools.com/webservices/tempconvert.asmx?WSDL' def proxy = new WSClient(url, this.class.classLoader) proxy.initialize() ant.echo "I'm freezing at ${properties.celsius} degrees Celsius" properties.result = proxy.CelsiusToFahrenheit(properties.celsius) </groovy> <antcall target="results"/> </target> <target name="results"> <echo message="I'm freezing at ${result} degrees Fahrenheit"/> </target> </project> </pre></td></tr></table> <p>which will output the following (along with some informational messages):</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> main: ... [echo] I'm freezing at 0 degrees Celsius results: [echo] I'm freezing at 32 degrees Fahrenheit BUILD SUCCESSFUL </pre></td></tr></table> <h3>Setting arguments</h3> <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> <target name="run"> <groovy> <arg line="1 2 3"/> <arg value="4 5"/> println args.size() println args[2] args.each{ ant.echo(message:it) } </groovy> </target> </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> Buildfile: GROOVY-2087.xml run: [groovy] 4 [groovy] 3 [echo] 1 [echo] 2 [echo] 3 [echo] 4 5 BUILD SUCCESSFUL </pre></td></tr></table> <h3>Forking Groovy</h3> <p>Since 1.5.7 and 1.6-beta-2, <code><groovy></code> also supports a <code>fork="true"</code> attribute. In fact, many of the attributes from the <code><java></code> Ant task are supported. More details to come ...</p> <h3>More examples</h3> <ul> <li><a href="http://mauszeig.wordpress.com/2006/04/07/ant-groovy-and-the-database/">Ant, Groovy and the Database</a></li> </ul>
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