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
Sign Up
Dashboard
GMaven
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><table class="wysiwyg-macro" data-macro-name="excerpt" data-macro-parameters="atlassian-macro-output-type=BLOCK" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2V4Y2VycHQ6YXRsYXNzaWFuLW1hY3JvLW91dHB1dC10eXBlPUJMT0NLfQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Executing Groovy code from Maven is a snap with GMaven.</p></td></tr></table></p> <p>Groovy source can be configured inline from POM configuration, or from an external file or URL source, have additional property configuration, better defaults and well just makes you feel happy <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> <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzptYXhMZXZlbD0yfG1pbkxldmVsPTF9&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="maxLevel=2|minLevel=1"> <h1>Default Variables</h1> <p>By default a few variables are bound into the scripts environment:</p> <table class="confluenceTable"><tbody> <tr> <th class="confluenceTh"><p> project </p></th> <td class="confluenceTd"><p> The maven project, with auto-resolving properties </p></td> </tr> <tr> <th class="confluenceTh"><p> pom </p></th> <td class="confluenceTd"><p> Alias for <em>project</em> </p></td> </tr> <tr> <th class="confluenceTh"><p> session </p></th> <td class="confluenceTd"><p> The executing <code>MavenSession</code> </p></td> </tr> <tr> <th class="confluenceTh"><p> settings </p></th> <td class="confluenceTd"><p> The executing <code>Settings</code> </p></td> </tr> <tr> <th class="confluenceTh"><p> log </p></th> <td class="confluenceTd"><p> A SLF4J Logger instance </p></td> </tr> <tr> <th class="confluenceTh"><p> ant </p></th> <td class="confluenceTd"><p> An AntBuilder instance for easy access to Ant tasks </p></td> </tr> <tr> <th class="confluenceTh"><p> fail() </p></th> <td class="confluenceTd"><p> A helper to throw MojoExecutionException </p></td> </tr> </tbody></table> <h1>Examples</h1> <p>These examples all using Maven <em>executions</em> which means that it binds the <code>execute</code> goal to a build phase. The configuration of <code>source</code> inside of an <code>execution</code> is only available for that execution. If you want to be able to <code>mvn groovy:execute</code> from the command-line, then you need 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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <configuration> <source> println "Hi" </source> </configuration> </plugin> </pre></td></tr></table> <h2>Execute an <em>Inline</em> Groovy Script</h2> <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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <source> if (project.packaging != 'pom') { log.info('Copying some stuff...') def dir = new File(project.basedir, 'target/classes/META-INF') ant.mkdir(dir: dir) ant.copy(todir: dir) { fileset(dir: project.basedir) { include(name: 'LICENSE.txt') include(name: 'NOTICE.txt') } } } </source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <h2>Execute a <em>Local</em> Groovy Script</h2> <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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <source>${pom.basedir}/src/main/script/myscript.groovy</source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <h2>Execute a <em>Remote</em> Groovy Script</h2> <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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <source>http://mydomain.com/myscript.groovy</source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <h1>Class Dependencies</h1> <h2>Using <em>Java</em> Classes</h2> <p>Scripts can use Java classes. To include custom classes to be available to the script define a classpath which contains additional artifacts to included in the scripts classloader.</p> <p>This is the preferred mechanism to extend a scripts classpath instead of adding dependencies to the plugin, which may cause problems if the classpath needs to be different on more than one plugin execution or when it differs between usage in more than one module in a multi-module build.</p> <p>Artifact definitions will pick up missing details from the POM's <code>dependencyManagement</code> section.</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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <classpath> <element> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> </element> </classpath> <source> import org.apache.commons.lang.SystemUtils if (SystemUtils.IS_OS_WINDOWS) { println('Go buy a Mac!') } </source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <h2>Using <em>Groovy</em> Classes</h2> <p>By setting the <code>scriptpath</code> you can load additional Groovy classes.</p> <p>For example if you have a class named Helper defined in a file named <code>${pom.basedir}/src/main/script/Helper.groovy</code> your scripts can use that class:</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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <scriptpath> <element>${pom.basedir}/src/main/script</element> </scriptpath> <source> import Helper Helper.callSomeStatic() </source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <h1>Custom Properties</h1> <p>By setting the <code>properties</code> parameter additional project properties can be added for an execution.</p> <p>Properties will automatically resolve when accessed. This means that if you have defined a property with a value of <code>${session} </code> that when you access it from your script, the returned value will be the <code>MavenSession</code> instance, just as one would expect when using <code>@parameter expression="${session}"</code> in a Mojo annotation. Though as noted below, default values are handled slightly differently than what is done currently for Mojo annotations.</p> <table class="wysiwyg-macro" data-macro-name="warning" data-macro-parameters="title=WARNING" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e3dhcm5pbmc6dGl0bGU9V0FSTklOR30&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="RICH_TEXT"><tr><td class="wysiwyg-macro-body"> <p>Property expressions are <strong>not</strong> GStrings, they are Maven expressions.<br /> You need to use the ${foo} syntax for them, $foo will <strong>not</strong> work.</p></td></tr></table> <p>Because of this property resolution, the value of a property might not be a String as one might expect when talking about normal Java properties. But, just for us Groovy doesn't really care what the type is.</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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <properties> <hello>world</hello> </properties> <source> println project.properties['hello'] // or if you prefer, these work just the same: // println project.properties.hello // println project.properties.get('hello') // println project.properties.getProperty('hello') </source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <p>By setting defaults additional project properties can be added for an execution which will be used only if the project or system does not override.</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> <plugin> <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <executions> <execution> <phase>generate-resources</phase> <goals> <goal>execute</goal> </goals> <configuration> <defaults> <hello>world</hello> </defaults> <source> println project.properties['hello'] </source> </configuration> </execution> </executions> </plugin> </pre></td></tr></table> <p>To provide a system override:</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> mvn -Dhello=jason </pre></td></tr></table> <h1>Script Helpers</h1> <h2>Using <code>fail()</code></h2> <p>Sometimes executed Groovy scripts need to fail the build. To do so with out causing evil stack traces or assert experssions to get spat out, you need to throw a <code>MojoExecutionException</code>. To help make this a little bit easier there is a <code>fail()</code> closure bound the the default namespace for the executed script.</p> <p>Failing with a simple string:</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> fail("That ain't no woman! It's a man, man!") </pre></td></tr></table> <p>Failing with an exception detail:</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> try { .... } catch (Exception e) { fail(e) } </pre></td></tr></table> <p>Failing with an exception detail and a message:</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> try { .... } catch (Exception e) { fail("She's the village bicycle! Everybody's had a ride.", e) } </pre></td></tr></table> <h1>Resources</h1> <h2>Goal Documentation</h2> <ul> <li><a href="http://groovy.codehaus.org/gmaven-generated/gmaven-plugin/execute-mojo.html">groovy:execute</a></li> </ul> <h2>Groovy Development</h2> <ul> <li><a class="confluence-link" href="/display/GROOVY/Using+Ant+from+Groovy" data-linked-resource-id="2713" data-linked-resource-type="page" data-linked-resource-default-alias="Using Ant from Groovy" data-base-url="http://docs.codehaus.org">Using Ant from Groovy</a></li> <li><a href="http://ant.apache.org/manual">Ant User Manual</a></li> <li><a href="http://groovy.codehaus.org/groovy-jdk">Groovy JDK Methods</a></li> <li><a href="http://groovy.codehaus.org/api">Groovy Javadocs</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