Commit | Line | Data |
---|---|---|
8e2d4a4c JM |
1 | /* From: |
2 | * http://groovy.jmiguel.eu/groovy.codehaus.org/WrappingGroovyScript.html | |
3 | */ | |
4 | /* | |
5 | * Copyright 2002-2007 the original author or authors. | |
6 | * | |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
8 | * you may not use this file except in compliance with the License. | |
9 | * You may obtain a copy of the License at | |
10 | * | |
11 | * http://www.apache.org/licenses/LICENSE-2.0 | |
12 | * | |
13 | * Unless required by applicable law or agreed to in writing, software | |
14 | * distributed under the License is distributed on an "AS IS" BASIS, | |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
16 | * See the License for the specific language governing permissions and | |
17 | * limitations under the License. | |
18 | */ | |
19 | ||
20 | /** | |
21 | * Wrap a script and groovy jars to an executable jar | |
22 | */ | |
23 | def cli = new CliBuilder() | |
24 | cli.h( longOpt: 'help', required: false, 'show usage information' ) | |
25 | cli.d( longOpt: 'destfile', argName: 'destfile', required: false, args: 1, 'jar destintation filename, defaults to {mainclass}.jar' ) | |
26 | cli.m( longOpt: 'mainclass', argName: 'mainclass', required: true, args: 1, 'fully qualified main class, eg. HelloWorld' ) | |
27 | cli.c( longOpt: 'groovyc', required: false, 'Run groovyc' ) | |
28 | ||
29 | //-------------------------------------------------------------------------- | |
30 | def opt = cli.parse(args) | |
31 | if (!opt) { return } | |
32 | if (opt.h) { | |
33 | cli.usage(); | |
34 | return | |
35 | } | |
36 | ||
37 | def mainClass = opt.m | |
38 | def scriptBase = mainClass.replace( '.', '/' ) | |
39 | def scriptFile = new File( scriptBase + '.groovy' ) | |
40 | if (!scriptFile.canRead()) { | |
41 | println "Cannot read script file: '${scriptFile}'" | |
42 | return | |
43 | } | |
44 | def destFile = scriptBase + '.jar' | |
45 | if (opt.d) { | |
46 | destFile = opt.d | |
47 | } | |
48 | ||
49 | //-------------------------------------------------------------------------- | |
50 | def ant = new AntBuilder() | |
51 | ||
52 | if (opt.c) { | |
53 | ant.echo( "Compiling ${scriptFile}" ) | |
54 | org.codehaus.groovy.tools.FileSystemCompiler.main( [ scriptFile ] as String[] ) | |
55 | } | |
56 | ||
57 | def GROOVY_HOME = new File( System.getenv('GROOVY_HOME') ) | |
58 | if (!GROOVY_HOME.canRead()) { | |
59 | ant.echo( "Missing environment variable GROOVY_HOME: '${GROOVY_HOME}'" ) | |
60 | return | |
61 | } | |
62 | ||
63 | ant.jar( destfile: destFile, compress: true, index: true ) { | |
64 | //fileset( dir: '.', includes: scriptBase + '*.class' ) | |
65 | fileset( dir: '.', includes: '*.class' ) | |
66 | ||
67 | zipgroupfileset( dir: GROOVY_HOME, includes: 'embeddable/groovy-all-*.jar' ) | |
68 | zipgroupfileset( dir: GROOVY_HOME, includes: 'lib/commons*.jar' ) | |
69 | // add more jars here | |
70 | ||
71 | manifest { | |
72 | attribute( name: 'Main-Class', value: mainClass ) | |
73 | } | |
74 | } | |
75 | ||
76 | ant.echo( "Run script using: \'java -jar ${destFile} ...\'" ) |