-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmaven-helper.sh
executable file
·567 lines (491 loc) · 12.5 KB
/
maven-helper.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#!/bin/sh
# This script uses the SciJava Maven repository at https://maven.scijava.org/
# to fetch an artifact, or to determine the state of it.
# error out whenever a command fails
set -e
root_url () {
test snapshots != "$2" || {
if curl -fs https://maven.scijava.org/service/local/repositories/sonatype-snapshots/content/"$1"/maven-metadata.xml > /dev/null 2>&1
then
echo https://maven.scijava.org/service/local/repositories/sonatype-snapshots/content
else
echo https://maven.scijava.org/content/repositories/snapshots
fi
return
}
echo https://maven.scijava.org/service/local/repo_groups/public/content
}
die () {
echo "$*" >&2
exit 1
}
# Helper (thanks, BSD!)
get_mtime () {
stat -c %Y "$1"
}
case "$(uname -s 2> /dev/null)" in
MINGW*)
get_mtime () {
date -r "$1" +%s
}
;;
Darwin)
get_mtime () {
stat -f %m "$1"
}
;;
esac
# Parse <groupId>:<artifactId>:<version> triplets (i.e. GAV parameters)
groupId () {
echo "${1%%:*}"
}
artifactId () {
result="${1#*:}"
echo "${result%%:*}"
}
version () {
result="${1#*:}"
case "$result" in
*:*)
echo "${1##*:}"
;;
esac
}
# Given an xml, extract the first <tag>
extract_tag () {
result="${2%%</$1>*}"
case "$result" in
"$2")
;;
*)
echo "${result#*<$1>}"
;;
esac
}
# Given an xml, extract the last <tag>
extract_last_tag () {
result="${2##*<$1>}"
case "$result" in
"$2")
;;
*)
echo "${result%%</$1>*}"
;;
esac
}
# Given an xml, skip all <tag> sections
skip_tag () {
result="$2"
while true
do
case "$result" in
*"<$1>"*)
result="${result%%<$1>*}${result#*</$1>}"
;;
*)
break
;;
esac
done
echo "$result"
}
# Given the xml of a POM, find the parent GAV
parent_gav_from_pom_xml () {
pom="$1"
parent="$(extract_tag parent "$pom")"
test -n "$parent" || return
groupId="$(extract_tag groupId "$parent")"
artifactId="$(extract_tag artifactId "$parent")"
version="$(extract_tag version "$parent")"
echo "$groupId:$artifactId:$version"
}
# Given a GAV parameter, determine the base URL of the project
project_url () {
gav="$1"
artifactId="$(artifactId "$gav")"
infix="$(groupId "$gav" | tr . /)/$artifactId"
version="$(version "$gav")"
case "$version" in
*SNAPSHOT)
echo "$(root_url $infix snapshots)/$infix"
;;
*)
# Release could be in either releases or thirdparty; try releases first
project_url="$(root_url $infix releases)/$infix"
header=$(curl -Is "$project_url/")
case "$header" in
HTTP/1.?" 200 OK"*)
;;
*)
project_url="$(root_url $infix thirdparty)/$infix"
;;
esac
echo "$project_url"
;;
esac
}
# Given a GAV parameter, determine the URL of the .jar file
jar_url () {
gav="$1"
artifactId="$(artifactId "$gav")"
version="$(version "$gav")"
infix="$(groupId "$gav" | tr . /)/$artifactId/$version"
case "$version" in
*-SNAPSHOT)
url="$(root_url $infix snapshots)/$infix/maven-metadata.xml"
metadata="$(curl -s "$url")"
timestamp="$(extract_tag timestamp "$metadata")"
buildNumber="$(extract_tag buildNumber "$metadata")"
version=${version%-SNAPSHOT}-$timestamp-$buildNumber
echo "$(root_url $infix snapshots)/$infix/$artifactId-$version.jar"
;;
*)
echo "$(root_url $infix releases)/$infix/$artifactId-$version.jar"
;;
esac
}
# Given a GAV parameter, return the URL to the corresponding .pom file
pom_url () {
url="$(jar_url "$1")"
echo "${url%.jar}.pom"
}
# Given a POM file, find its GAV parameter
gav_from_pom () {
pom="$(cat "$1")"
parent="$(extract_tag parent "$pom")"
pom="$(skip_tag parent "$pom")"
pom="$(skip_tag dependencies "$pom")"
pom="$(skip_tag profiles "$pom")"
pom="$(skip_tag build "$pom")"
groupId="$(extract_tag groupId "$pom")"
test -n "$groupId" || groupId="$(extract_tag groupId "$parent")"
artifactId="$(extract_tag artifactId "$pom")"
version="$(extract_tag version "$pom")"
test -n "$version" || version="$(extract_tag version "$parent")"
echo "$groupId:$artifactId:$version"
}
# Given a GAV parameter, find its parent's GAV
parent_gav () {
gav="$1"
groupId="$(groupId "$gav")"
artifactId="$(artifactId "$gav")"
version="$(version "$gav")"
test -n "$version" || version="$(latest_version "$gav")"
pom="$(read_pom "$groupId:$artifactId:$version")"
parent_gav_from_pom_xml "$pom"
}
# Given a POM file, find its parent's GAV
parent_gav_from_pom () {
pom="$(cat "$1")"
parent_gav_from_pom_xml "$pom"
}
# Given a POM file, extract its packaging
packaging_from_pom () {
pom="$(cat "$1")"
pom="$(skip_tag parent "$pom")"
pom="$(skip_tag dependencies "$pom")"
pom="$(skip_tag profiles "$pom")"
pom="$(skip_tag build "$pom")"
packaging="$(extract_tag packaging "$pom")"
echo "${packaging:-jar}"
}
# Given a GAV parameter possibly lacking a version, determine the latest version
latest_version () {
metadata="$(curl -s "$(project_url "$1")"/maven-metadata.xml)"
latest="$(extract_tag release "$metadata")"
test -n "$latest" || latest="$(extract_tag latest "$metadata")"
test -n "$latest" || latest="$(extract_last_tag version "$metadata")"
echo "$latest"
}
# Given a GA parameter, invalidate the cache in SciJava's Nexus' group/public
SONATYPE_DATA_CACHE_URL=https://maven.scijava.org/service/local/data_cache/repositories/sonatype/content
SONATYPE_SNAPSHOTS_DATA_CACHE_URL=https://maven.scijava.org/service/local/data_cache/repositories/sonatype-snapshots/content
invalidate_cache () {
ga="$1"
artifactId="$(artifactId "$ga")"
infix="$(groupId "$ga" | tr . /)/$artifactId"
curl --netrc -i -X DELETE \
$SONATYPE_DATA_CACHE_URL/$infix/maven-metadata.xml &&
curl --netrc -i -X DELETE \
$SONATYPE_SNAPSHOTS_DATA_CACHE_URL/$infix/maven-metadata.xml &&
version="$(latest_version "$ga")" &&
infix="$infix/$version" &&
curl --netrc -i -X DELETE \
$SONATYPE_DATA_CACHE_URL/$infix/$artifactId-$version.pom &&
if test "$artifactId" = "${artifactId#pom-}"
then
curl --netrc -i -X DELETE \
$SONATYPE_DATA_CACHE_URL/$infix/$artifactId-$version.jar
fi
}
# Generate a temporary file; not thread-safe
tmpfile () {
i=1
while test -f /tmp/precompiled.$i"$1"
do
i=$(($i+1))
done
echo /tmp/precompiled.$i"$1"
}
# Given a GAV or a path, read the POM
read_pom () {
case "$1" in
pom.xml|*/pom.xml|*\\pom.xml)
cat "$1"
;;
*)
curl -s "$(pom_url "$1")"
;;
esac
}
# Given a GAV parameter (or pom.xml path) and a name, resolve a property (falling back to parents)
get_property () {
gav="$1"
key="$2"
case "$key" in
imagej1.version)
latest_version net.imagej:ij
return
;;
project.groupId)
groupId "$gav"
return
;;
project.version)
version "$gav"
return
;;
esac
while test -n "$gav"
do
pom="$(read_pom "$gav")"
properties="$(extract_tag properties "$pom")"
property="$(extract_tag "$key" "$properties")"
if test -n "$property"
then
echo "$property"
return
fi
gav="$(parent_gav_from_pom_xml "$pom")"
done
die "Could not resolve \${$2} in $1"
}
# Given a GAV parameter and a string, expand properties
expand () {
gav="$1"
string="$2"
result=
while true
do
case "$string" in
*'${'*'}'*)
result="$result${string%%\$\{*}"
string="${string#*\$\{}"
key="${string%\}*}"
result="$result$(get_property "$gav" "$key")"
string="${string#$key\}}"
;;
*)
echo "$result$string"
break
;;
esac
done
}
# Given a GAV parameter, make a list of its dependencies (as GAV parameters)
get_dependencies () {
pom="$(read_pom "$1")"
while true
do
case "$pom" in
*'<dependency>'*)
dependency="$(extract_tag dependency "$pom")"
scope="$(extract_tag scope "$dependency")"
case "$scope" in
''|compile)
groupId="$(expand "$1" "$(extract_tag groupId "$dependency")")"
artifactId="$(extract_tag artifactId "$dependency")"
version="$(expand "$1" "$(extract_tag version "$dependency")")"
echo "$groupId:$artifactId:$version"
;;
esac
pom="${pom#*</dependency>}"
;;
*)
break;
esac
done
}
# Given a GAV parameter and a space-delimited list of GAV parameters, expand
# the list by the first parameter and its dependencies (unless the list already
# contains said parameter)
get_all_dependencies () {
case " $2 " in
*" $1 "*)
;; # list already contains the depdendency
*)
gav="$1"
set "" "$2 $1"
for dependency in $(get_dependencies "$gav")
do
set "" "$(get_all_dependencies "$dependency" "$2")"
done
;;
esac
echo "$2"
}
# Given a GAV parameter, download the .jar file
get_jar () {
url="$(jar_url "$1")"
tmpfile="$(tmpfile .jar)"
curl -s "$url" > "$tmpfile"
test "<html" != "$(head -c 5 "$tmpfile")" ||
curl -s "${url%.jar}.nar" > "$tmpfile"
test PK = "$(head -c 2 "$tmpfile")"
echo "$tmpfile"
}
# Given a GAV parameter, get the commit from the manifest of the deployed .jar
commit_from_gav () {
jar="$(get_jar "$1")"
unzip -p "$jar" META-INF/MANIFEST.MF |
sed -n -e 's/^Implementation-Build: *//pi' |
tr -d '\r'
rm "$jar"
}
# Given a GAV parameter, determine whether the .jar file is already in plugins/
# or jars/
is_jar_installed () {
artifactId="$(artifactId "$1")"
version="$(version "$1")"
file=$artifactId-$version.jar
test -f "$file" || file=../plugins/$file
test -f "$file" || return 1
case "$version" in
*-SNAPSHOT)
# is the file younger than a day?
mtime="$(get_mtime "$file")"
test "$(($mtime-$(date +%s)))" -gt -86400
;;
esac
}
# Given a .jar file, determine whether it is an ImageJ 1.x plugin
is_ij1_plugin () {
unzip -l "$1" plugins.config > /dev/null 2>&1
}
# Given a GAV parameter, download the .jar file and its dependencies as needed
# and install them into plugins/ or jars/, respectively
install_jar () {
for gav in $(get_all_dependencies "$1")
do
if ! is_jar_installed "$gav"
then
tmp="$(get_jar "$gav")"
name="$(artifactId "$gav")-$(version "$gav").jar"
if test -d ../plugins && is_ij1_plugin "$tmp"
then
mv "$tmp" "../plugins/$name"
else
mv "$tmp" "$name"
fi
fi
done
}
# Determine whether a local project (specified as pom.xml) needs to be deployed
is_deployed () {
gav="$(gav_from_pom "$1")" &&
commit="$(commit_from_gav "$gav")" &&
test -n "$commit" &&
dir="$(dirname "$1")" &&
(cd "$dir" &&
git diff --quiet "$commit".. -- .)
}
# The main part
case "$1" in
commit)
commit_from_gav "$2"
;;
deps|dependencies)
get_dependencies "$2"
;;
all-deps|all-dependencies)
get_all_dependencies "$2" |
tr ' ' '\n' |
grep -v '^$'
;;
latest-version)
latest_version "$2"
;;
invalidate-cache)
invalidate_cache "$2"
;;
gav-from-pom)
gav_from_pom "$2"
;;
parent-gav)
parent_gav "$2"
;;
pom-url)
pom_url "$2"
;;
parent-gav-from-pom)
parent_gav_from_pom "$2"
;;
packaging-from-pom)
packaging_from_pom "$2"
;;
property-from-pom|get-property|property)
if test $# -lt 3
then
get_property pom.xml "$2"
else
get_property "$2" "$3"
fi
;;
install)
install_jar "$2"
;;
is-deployed)
is_deployed "$2"
;;
*)
test $# -eq 0 || echo "Unknown command: $1" >&2
die "Usage: $0 [command] [argument...]"'
Commands:
commit <groupId>:<artifactId>:<version>
Gets the commit from which the given artifact was built.
dependencies <groupId>:<artifactId>:<version>
Lists the direct dependencies of the given artifact.
all-dependencies <groupId>:<artifactId>:<version>
Lists all dependencies of the given artifact, including itself and
transitive dependencies.
latest-version <groupId>:<artifactId>[:<version>]
Prints the current version of the given artifact (if "SNAPSHOT" is
passed as version, it prints the current snapshot version rather
than the release one).
invalidate-cache <groupId>:<artifactId>
Invalidates the version cached in the SciJava Nexus from OSS Sonatype,
e.g. after releasing a new version to Sonatype. Requires appropriate
credentials in $HOME/.netrc for https://maven.scijava.org/.
parent-gav <groupId>:<artifactId>[:<version>]
Prints the GAV parameter of the parent project of the given artifact.
pom-url <groupId>:<artifactId>:<version>
Gets the URL of the POM describing the given artifact.
gav-from-pom <pom.xml>
Prints the GAV parameter described in the given pom.xml file.
parent-gav-from-pom <pom.xml>
Prints the GAV parameter of the parent project of the pom.xml file.
packaging-from-pom <pom.xml>
Prints the packaging type of the given project.
property-from-pom <pom.xml> <property-name>
Prints the property specified in the pom.xml file (or in its parents).
install <groupId>:<artifactId>:<version>
Installs the given artifact and all its dependencies; if the artifact
or dependency to install is an ImageJ 1.x plugin and the parent
directory contains a subdirectory called "plugins", it will be
installed there, otherwise into the current directory.
is-deployed <pom.xml>
Tests whether the specified project is deployed alright. Fails
with exit code 1 if not.
'
;;
esac