1
- /*
2
- * Copyright 2002-2007 the original author or authors.
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
1
+ /**
2
+ * from https://github.com/sdanzan/groovy-wrapper/blob/master/GroovyWrapper.groovy
3
+ *
4
+ * Ryan: missing features: if i missing, blows up in args
5
+ * also, doesn't appear to add main class
7
6
*
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
7
*
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
8
+ * Copyright 2002-2007 the original author or authors.
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
16
22
17
23
/*
18
- * Original script at http://groovy.codehaus.org/WrappingGroovyScript
19
- */
24
+ * Original script at http://groovy.codehaus.org/WrappingGroovyScript
25
+ */
20
26
21
27
/**
22
- * Wrap a script and groovy jars to an executable jar
23
- */
28
+ * Wrap a script and groovy jars to an executable jar
29
+ */
24
30
def cli = new CliBuilder ()
25
31
cli. h( longOpt : ' help' , required : false , ' show usage information' )
26
32
cli. d( longOpt : ' destfile' , argName : ' destfile' , required : false , args : 1 , ' jar destination filename, defaults to {mainclass}.jar' )
27
33
cli. m( longOpt : ' mainclass' , argName : ' mainclass' , required : true , args : 1 , ' fully qualified main class, eg. HelloWorld' )
28
34
cli. c( longOpt : ' groovyc' , required : false , ' Run groovyc' )
29
35
cli. i( longOpt : ' include' , required : false , args : 1764 , valueSeparator : ' ' as char , ' a list of jars to include into the destination jar' )
30
36
cli. x( longOpt : ' exclude' , required : false , args : 1764 , valueSeparator : ' ' as char , ' a list of file patterns to exclude from included jars' )
31
-
37
+ cli. g( longOpt : ' grab' , required : false , ' Include ~/.groovy/grapes from @Grab statements' )
38
+ cli. l( longOpt : ' lib' , required : false , ' Include all from ~/.groovy/lib' )
32
39
// --------------------------------------------------------------------------
33
40
def opt = cli. parse(args)
34
41
if (! opt) { return }
35
42
if (opt. h) {
36
- cli. usage();
37
- return
43
+ cli. usage();
44
+ return
38
45
}
39
46
40
47
def mainClass = opt. m
41
48
def scriptBase = mainClass. replace( ' .' , ' /' )
42
49
def scriptFile = new File ( scriptBase + ' .groovy' )
43
50
if (! scriptFile. canRead()) {
44
- println " Cannot read script file: '${ scriptFile} '"
45
- return
51
+ println " Cannot read script file: '${ scriptFile} '"
52
+ return
46
53
}
47
54
def destFile = scriptBase + ' .jar'
48
55
if (opt. d) {
49
- destFile = opt. d
56
+ destFile = opt. d
50
57
}
51
58
52
59
// --------------------------------------------------------------------------
53
60
def ant = new AntBuilder ()
54
61
55
62
if (opt. c) {
56
- ant. echo( " Compiling ${ scriptFile} " )
57
- org.codehaus.groovy.tools.FileSystemCompiler . main( [ scriptFile ] as String [] )
63
+ ant. echo( " Compiling ${ scriptFile} " )
64
+ org.codehaus.groovy.tools.FileSystemCompiler . main( [ scriptFile ] as String [] )
58
65
}
59
66
60
67
def GROOVY_HOME = new File ( System . getenv(' GROOVY_HOME' ) )
61
- if (! GROOVY_HOME . canRead()) {
62
- ant. echo( " Missing environment variable GROOVY_HOME: '${ GROOVY_HOME} '" )
63
- return
68
+ def HOME = new File (System . getenv(' HOME' ))
69
+ if (! GROOVY_HOME . canRead() || GROOVY_HOME == " " ) {
70
+ ant. echo( " Missing environment variable GROOVY_HOME: '${ GROOVY_HOME} '" )
71
+ return
64
72
}
65
73
66
74
def supJars = []
67
75
76
+ def grapes = []
77
+ def libs = []
78
+
68
79
ant. jar( destfile : destFile, compress : true , index : true ) {
69
- classes = new File ( scriptBase ). getParent() + ' /*.class'
70
- if ( classes. startsWith( ' /' ) ) classes = ' *.class'
71
- fileset( dir : ' .' , includes : classes )
72
-
73
- // Embedded Groovy jars
74
- zipgroupfileset( dir : GROOVY_HOME , includes : ' embeddable/groovy-all-*.jar' )
75
- zipgroupfileset( dir : GROOVY_HOME , includes : ' lib/commons*.jar' )
76
-
77
- // Other jars to include from -i option
78
- opt. is. each {
79
- if (it. endsWith( ' .jar' )) {
80
- jarFile = new File ( it )
81
- zipfileset( src : it ) {
82
- // exclude patterns from -x option
83
- opt. xs. each {
84
- exclude( name : it )
80
+
81
+ classes = ((scriptFile. getParent()) ? scriptFile. getParent() : " " ) + ' /*.class'
82
+ if ( classes. startsWith( ' /' ) ) classes = ' *.class'
83
+ fileset( dir : ' .' , includes : classes )
84
+
85
+ // Embedded Groovy jars
86
+ zipgroupfileset( dir : GROOVY_HOME , includes : ' embeddable/groovy-all-*.jar' )
87
+ zipgroupfileset( dir : GROOVY_HOME , includes : ' lib/commons*.jar' )
88
+ if (opt. g) {
89
+ // Selective Grap (doesn't do dependencies, but good enough). Generally the @Grab will be commented out when compiling to jar
90
+ sfLines = scriptFile. readLines()
91
+ for (a in sfLines) {
92
+ if (a. contains(" @Grab" )) {
93
+ s = a. indexOf(" (" )
94
+ e = a. indexOf(" )" )
95
+ c = a. substring(s, e + 1 )
96
+ c = c. replace(" (" , " " ). replace(" )" , " " ). replace(' "' , " " ). replace(" '" , " " ) // stripping
97
+ g = c. split(" :" ) // this is what I want to get from the grapes dir
98
+ j = " ${ g[0]} /${ g[1]} /jars/${ g[1]} -${ g[2]} .jar"
99
+ grapes. add(j)
100
+ zipgroupfileset(dir : new File (HOME , " .groovy/grapes" ), includes : " ${ g[0]} /${ g[1]} /jars/${ g[1]} -${ g[2]} .jar" )
101
+
102
+ }
103
+ }
104
+ }
105
+ if (opt. l) {
106
+ // All in lib
107
+ zipgroupfileset(dir : HOME , includes : ' .groovy/lib/*.jar' )
108
+ libs = new File (HOME , ' .groovy/lib' ). list()
109
+ }
110
+
111
+
112
+ // Other jars to include from -i option
113
+ if (opt. is) {
114
+ opt. is. each {
115
+ if (it. endsWith(' .jar' )) {
116
+ jarFile = new File (it)
117
+ zipfileset(src : it) {
118
+ // exclude patterns from -x option
119
+ if (opt. xs) {
120
+ opt. xs. each {
121
+ exclude(name : it)
122
+ }
123
+ }
124
+ }
125
+ supJars << new File (it). getName()
126
+ }
85
127
}
86
- }
87
- supJars << new File ( it ). getName()
88
128
}
89
- }
90
129
91
- manifest {
92
- attribute( name : ' Main-Class' , value : mainClass )
93
- }
130
+ manifest {
131
+ attribute( name : ' Main-Class' , value : mainClass )
132
+ }
94
133
}
95
134
96
135
supJars. each {
97
- ant. echo( " Added supplemental jar: " + it )
136
+ ant. echo( " Added supplemental jar: " + it )
137
+ }
138
+ grapes. each{
139
+ ant. echo(" Added jar from grapes: $it " )
98
140
}
99
- ant. echo( " Run script using: \' java -jar ${ destFile} ...\' " )
100
141
142
+ libs. each{
143
+ ant. echo(" Added jar from lib: $it " )
144
+ }
145
+ ant. echo( " Run script using: \' java -jar ${ destFile} ...\' " )
0 commit comments