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>Abstract </h2> <p>The script GroovyWrapper described on this page, creates a self-excutable jar from a groovy script.</p> <h2>Description</h2> <p>The script GroovyWrapper allows you to distribute your groovy script as a standalone self-executable jar.</p> <p>The generated jar contains the compiled groovy script, and the groovy embeddable jar. Moreover the jar's main-class is set to the compiled groovy script.</p> <p>Having this self-executable jar, you can start the groovy script, like java -jar MyScriptAsJar.jar</p> <h2>Groovy Wrapper Script</h2> <p>The Groovy Wrapper Script below is quite simple.</p> <p>First it parses its command line arguments using groovy's CliBuilder.</p> <ul> <li>The argument <code>-m</code> defines the groovy script name, w/o {{.class} suffix</li> <li>The optional argument <code>-d</code> allows defines the jar fileame, by default the jar filename is build as {{<br /> groovy-script-name.jar}}.</li> <li>The argument <code>-c</code> compile the groovy script in the GroovyWrapper. Use this argument if your groovy script is not compiled by some build process.</li> </ul> <p>Next if checks the arguments, compiles the script if specified.<br /> Then the environment variable <code>GROOVY_HOME</code> is looked up for calculating the file location of the groovy embeddable jar.</p> <p>Finnally GroovyWrapper builds the jar using AntBuilder.</p> <p>The source of the GroovyWrapper script:</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> /* * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * Wrap a script and groovy jars to an executable jar */ def cli = new CliBuilder() cli.h( longOpt: 'help', required: false, 'show usage information' ) cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destintation filename, defaults to {mainclass}.jar' ) cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' ) cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' ) //-------------------------------------------------------------------------- def opt = cli.parse(args) if (!opt) { return } if (opt.h) { cli.usage(); return } def mainClass = opt.m def scriptBase = mainClass.replace( '.', '/' ) def scriptFile = new File( scriptBase + '.groovy' ) if (!scriptFile.canRead()) { println "Cannot read script file: '${scriptFile}'" return } def destFile = scriptBase + '.jar' if (opt.d) { destFile = opt.d } //-------------------------------------------------------------------------- def ant = new AntBuilder() if (opt.c) { ant.echo( "Compiling ${scriptFile}" ) org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] ) } def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') ) if (!GROOVY_HOME.canRead()) { ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" ) return } ant.jar( destfile: destFile, compress: true, index: true ) { fileset( dir: '.', includes: scriptBase + '*.class' ) zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' ) zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' ) // add more jars here manifest { attribute( name: 'Main-Class', value: mainClass ) } } ant.echo( "Run script using: \'java -jar ${destFile} ...\'" ) </pre></td></tr></table> <h2>Example</h2> <p>An example for using GroovyWrapper:</p> <p>Say you have groovy script <code>HelloWorld.groovy</code>, use <code>GroovyWrapper</code> for building <code>HelloWorld.jar</code>, as follows:</p> <blockquote> <p>$ groovy GroovyWrapper -c -m HelloWorld</p></blockquote> <p><code>GroovyWrapper</code> will compile the script <code>HelloWorld.groovy</code> to <code>HelloWorld.class</code>, and creates a self-executable jar <code>HelloWorld.jar</code>.</p> <p>Now you can use the <code>HelloWorld.jar</code> for launching the HelloWorld script, simply by running:</p> <blockquote> <p>$ java -jar HelloWorld.jar</p></blockquote>
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