-
Notifications
You must be signed in to change notification settings - Fork 16
/
jgo.sh
executable file
·439 lines (400 loc) · 10.6 KB
/
jgo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
# A script to execute a main class of a Maven artifact
# which is available locally or from Maven Central.
#
# Works using the maven-dependency-plugin to stash the artifact
# and its deps to a temporary location, then invokes java.
#
# It would be more awesome to enhance the exec-maven-plugin to support
# running something with a classpath built from the local Maven repository
# cache. Then you would get all the features of exec-maven-plugin.
# But this script works in a pinch for simple cases.
# Define some useful functions.
notice() { test $quiet || echo "$@"; }
info() { test $verbose && echo "[INFO] $@"; }
err() { echo "$@" 1>&2; }
die() { err "$@"; exit 1; }
check() {
for tool in $@
do
which "$tool" >/dev/null ||
die "The '$tool' utility is required but not found"
done
}
doLink() {
case "$links" in
soft)
# Use symlinks.
check ln cp
(test $verbose && set -x; ln -s "$1" "$2") ||
(test $verbose && set -x; cp "$1" "$2") ||
die "Cannot copy '$1' into jgo workspace '$2'"
;;
none)
# Do not use links.
check cp
(test $verbose && set -x; cp "$1" "$2") ||
die "Cannot copy '$1' into jgo workspace '$2'"
;;
*)
# Use hard links (the default).
check ln cp
(test $verbose && set -x; ln "$1" "$2") ||
(test $verbose && set -x; cp "$1" "$2") ||
die "Cannot copy '$1' into jgo workspace '$2'"
;;
esac
}
isWindows() {
case "$(uname)" in
CYGWIN*)
echo "cygwin"
;;
MINGW*)
echo "mingw"
;;
*)
;;
esac
}
goodPath() {
# NB: Non-POSIX Windows programs, including mvn and java,
# do not understand the POSIX-compliant '/' prefix. They need
# absolute paths to begin with a drive letter (e.g., 'C:').
if [ "$(isWindows)" ]
then
echo "$(cygpath -aw "$(dirname "$1")")\\$(basename "$1")"
else
echo "$1"
fi
}
m2Path() {
test "$M2_REPO" && echo "$M2_REPO" || echo "$HOME/.m2"
}
trim() {
check sed
echo "$*" | sed 's/^ *//' | sed 's/ *$//'
}
expand() {
local expanded="$@"
for shortcut in "${shortcuts[@]}"
do
key=$(trim "${shortcut%=*}")
val=$(trim "${shortcut#*=}")
case "$expanded" in
$key*)
expanded="$val${expanded#$key}"
;;
esac
done
echo "$expanded"
}
launchJava() {
check java
(
goodCP=$(goodPath "$workspace/*")
test $verbose && set -x
java -cp "$goodCP" "${jvm_args[@]}" "$mainClass" "${app_args[@]}"
)
}
# Parse configuration file.
configFile="$HOME/.jgorc"
cacheDir="$HOME/.jgo"
m2Repo="$(m2Path)/repository"
repositories=()
shortcuts=()
test -f "$configFile" &&
while read line
do
case "$line" in
'#'*)
# skip comment
;;
\[*\])
ltrim=${line:1}
section=${ltrim%?}
;;
*=*)
case "$section" in
repositories)
repositories+=("$line")
;;
settings)
key=$(trim "${line%=*}")
val=$(trim "${line#*=}")
case "$key" in
cacheDir) cacheDir="$val";;
m2Repo) m2Repo="$val";;
links) links="$val";;
esac
;;
shortcuts)
shortcuts+=("$line")
;;
*)
;;
esac
;;
esac
done <"$configFile"
# Parse arguments.
jvm_args=()
app_args=()
while test $# -gt 0
do
if [ "$endpoint" ]
then
# Argument to the main class.
app_args+=("$1")
else
# Argument to the JVM, or jgo itself.
case "$1" in
-m)
manageDeps=1
;;
-v)
verbose=1
;;
-vv)
verbose=2
;;
-u)
updateCache=1
;;
-U)
updateMaven=1
updateCache=1
;;
-q)
quiet=1
;;
-*)
jvm_args+=("$1")
;;
*)
endpoint="$1"
;;
esac
fi
shift
done
# Parse the endpoint.
endpoint=$(expand "$endpoint")
test "$endpoint" || endpoint=usage
eRemain=$endpoint
while test "$eRemain"
do
# Process the artifact string before the plus.
artifact=${eRemain%%+*}
let flen=${#artifact}+1
eRemain=${eRemain:$flen}
artifact=$(expand "$artifact")
c=""
m=""
case "$artifact" in
*:*:*:*:*:*) # G:A:V:C:mainClass
die "Too many elements in artifact '$artifact'"
;;
*:*:*:*:*) # G:A:V:C:mainClass
g=${artifact%%:*}; remain=${artifact#*:}
a=${remain%%:*}; remain=${remain#*:}
v=${remain%%:*}; remain=${remain#*:}
c=${remain%%:*}
m=${remain#*:}
;;
*:*:*:*) # G:A:V:mainClass
g=${artifact%%:*}; remain=${artifact#*:}
a=${remain%%:*}; remain=${remain#*:}
v=${remain%%:*}
m=${remain#*:}
;;
*:*:*) # G:A:mainClass or G:A:V
g=${artifact%%:*}; remain=${artifact#*:}
a=${remain%%:*}; remain=${remain#*:}
case "$remain" in
[0-9a-f]*|RELEASE|LATEST|MANAGED)
v="$remain"
;;
*)
v="RELEASE"
m="$remain"
;;
esac
;;
*:*) # G:A
g=${artifact%%:*}
a=${artifact#*:}
v="RELEASE"
;;
*)
echo "Usage: jgo [-v] [-u] [-U] [-m] [-q] <jvm-args> <endpoint> <main-args>"
echo
echo " -v : verbose mode flag"
echo " -u : update/regenerate cached environment"
echo " -U : force update from remote Maven repositories (implies -u)"
echo " -m : use endpoints for dependency management (see README)"
echo " -q : quiet mode flag to suppress regular output"
echo " <jvm-args> : any list of arguments to the JVM"
echo " <endpoint> : the artifact(s) + main class to execute"
echo " <main-args> : any list of arguments to the main class"
echo
echo "The endpoint should have one of the following formats:"
echo
echo "- groupId:artifactId"
echo "- groupId:artifactId:version"
echo "- groupId:artifactId:mainClass"
echo "- groupId:artifactId:version:mainClass"
echo "- groupId:artifactId:version:classifier:mainClass"
echo
echo "If version is omitted, then RELEASE is used."
echo "If version is MANAGED, then the <version> tag is omitted in"
echo "the dependency xml and must be managed by another endpoint."
echo "If mainClass is omitted, it is auto-detected."
echo "You can also write part of a class beginning with an @ sign,"
echo "and it will be auto-completed."
echo
echo "Multiple artifacts can be concatenated with pluses,"
echo "and all of them will be included on the classpath."
echo "However, you should not specify multiple main classes."
exit 1
;;
esac
info "Artifact:"
info "- groupId = $g"
info "- artifactId = $a"
info "- version = $v"
test "$c" && info "- classifier = $c" || info "- classifier = <none>"
test "$m" && mainClass="$m"
deps="$deps<dependency><groupId>$g</groupId><artifactId>$a</artifactId>"
test "$v" != "MANAGED" && deps="$deps<version>$v</version>"
test "$c" && deps="$deps<classifier>$c</classifier>"
deps="$deps</dependency>"
if [ "$manageDeps" ] && [ "$v" != "MANAGED" ]
then
depMgmt="$depMgmt<dependency><groupId>$g</groupId><artifactId>$a</artifactId><version>$v</version>"
test "$c" && depMgmt="$depMgmt<classifier>$c</classifier>"
depMgmt="$depMgmt<type>pom</type><scope>import</scope></dependency>"
fi
done
# Create a workspace in the jgo cache directory
check sed rm mkdir
workspace="$cacheDir/$(echo "$endpoint" | sed 's/[:+]/\//g' | sed 's/[^0-9a-zA-Z/\.-]/_/g')"
test $updateCache && rm -rf "$workspace"
mkdir -p "$workspace"
info "Workspace = $workspace"
if [ -f "$workspace/mainClass" ]
then
# Workspace is already populated; just use it
check cat
mainClass=$(cat "$workspace/mainClass")
launchJava
exit $?
fi
notice 'First time start-up may be slow. Downloaded dependencies will be cached for shorter start-up times in subsequent executions.'
# Synthesize a dummy Maven project.
for repository in "${repositories[@]}"
do
key=$(trim "${repository%=*}")
val=$(trim "${repository#*=}")
repos="$repos<repository><id>$key</id><url>$val</url></repository>"
done
check cat
tmpPOM="$workspace/pom.xml"
cat >"$tmpPOM" <<EOL
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>$g-BOOTSTRAPPER</groupId>
<artifactId>$a-BOOTSTRAPPER</artifactId>
<version>0</version>
<dependencyManagement>
<dependencies>$depMgmt</dependencies>
</dependencyManagement>
<dependencies>$deps</dependencies>
<repositories>$repos</repositories>
</project>
EOL
check mvn
test $updateMaven && mvnArgs=-U
test "$verbose" = "2" && mvnArgs=-X
goodPOM=$(goodPath "$tmpPOM")
buildLog=$(test $verbose && set -x; mvn -B $mvnArgs -f "$goodPOM" dependency:resolve 2>&1)
if [ $? -ne 0 ]
then
err "Failed to bootstrap the artifact."
err
err "Possible solutions:"
err "* Double check the endpoint for correctness (https://search.maven.org/)."
err "* Add needed repositories to ~/.jgorc [repositories] block (see README)."
err "* Try with an explicit version number (release metadata might be wrong)."
err
if [ "$verbose" ]
then
err "Here is the Maven log:"
err "$buildLog"
else
err "Rerun with the -v flag to see the Maven log."
fi
exit 2
fi
# Build a workspace of symlinked artifacts.
check grep sed
echo "$buildLog" | grep ':\(compile\|runtime\)' | sed 's/\[INFO\] *//' |
while read gav
do
case "$gav" in
*:*:*:*:*:*) # G:A:P:C:V:S
g=${gav%%:*}; remain=${gav#*:}
a=${remain%%:*}; remain=${remain#*:}
p=${remain%%:*}; remain=${remain#*:}
c=${remain%%:*}; remain=${remain#*:}
v=${remain%%:*}
s=${remain#*:}
;;
*:*:*:*:*) # G:A:P:V:S
g=${gav%%:*}; remain=${gav#*:}
a=${remain%%:*}; remain=${remain#*:}
p=${remain%%:*}; remain=${remain#*:}
c=""
v=${remain%%:*}
s=${remain#*:}
;;
esac
# NB: test-jar packaging means jar packaging + tests classifier.
test "$p" = test-jar && p=jar && c=tests
g=$(echo "$g" | sed 's/\./\//g')
test "$c" && artName="$a-$v-$c" || artName="$a-$v"
doLink "$m2Repo/$g/$a/$v/$artName.$p" "$workspace"
done
# Massage the main class as needed.
if [ -z "$mainClass" ]
then
# Infer the main class from the JAR manifest.
check unzip grep head sed
jarPathPrefix="$workspace/$a"
test "$c" && jarPathPrefix="$jarPathPrefix-$c"
mainClass=$((test $verbose && set -x;
unzip -p "$jarPathPrefix"-[0-9a-f]*.jar META-INF/MANIFEST.MF 2>/dev/null) |
grep Main-Class | head -n1 | sed 's/^Main-Class: *\([a-zA-Z0-9_\.]*\).*/\1/')
info "Inferred main class: $mainClass"
fi
test "$mainClass" || die "No main class given, and none found."
if [ "z${mainClass:0:1}" = 'z@' ]
then
# Autocomplete the main class by scanning the JARs.
check jar grep sed
mainPath=$(echo "${mainClass:1}" | sed 's/\//./g')
allClasses=$((test $verbose && set -x;
for jar in "$workspace/"*.jar; do jar tf "$jar"; done) |
grep '\.class$' | sed 's/\//./g')
completedClass=$(echo "$allClasses" |
grep "$mainPath\.class$" | head -n1 | sed 's/\.class$//')
test "$completedClass" || completedClass=$(echo "$allClasses" |
grep "$mainPath.*\.class$" | head -n1 | sed 's/\.class$//')
test "$completedClass" ||
die "No autocompletions found for '$mainClass'"
mainClass=$completedClass
info "Autocompleted main class: $mainClass"
fi
echo "$mainClass" >"$workspace/mainClass"
# Launch it!
launchJava