-
Notifications
You must be signed in to change notification settings - Fork 70
/
run-with-gradle.sh
executable file
·140 lines (115 loc) · 3.15 KB
/
run-with-gradle.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
#!/usr/bin/env bash
# shellcheck disable=SC2214,SC2215
if ((BASH_VERSINFO[0] < 4))
then
cat <<EOM >&2
$0: This script requires Bash 4.0 or higher.
If you are on Mac, the native /bin/bash is from 2006 or older; consider using a
more recent Bash from Homebrew.
EOM
exit 3
fi
export PS4='+${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]:+${FUNCNAME[0]}():} '
# Edit these to suit
readonly package=hm.binkley.md
readonly artifactId=modern-java-practices
readonly version=0-SNAPSHOT
jvm_flags=()
# No editable parts below here
set -e
set -u
set -o pipefail
readonly progname="${0##*/}"
printf -v pbold "\e[1m"
printf -v pred "\e[31m"
printf -v preset "\e[0m"
function print-help() {
cat <<EOH
Usage: $progname [OPTIONS] [-- ARGUMENTS]
Runs a single-jar JVM project.
Options:
-C, --alt-class=CLASS
execute CLASS as the alternate main class, otherwise assume
the jar is executable
-J, --jvm-option=OPTION
adds OPTION to the JVM options
-d, --debug print script execution to STDERR
-h, --help display this help and exit
Examples:
$progname Runs the executable jar with no arguments to main
$progname -C a-class Runs the main from "a-class"
$progname -- an-arg Runs the executable jar passing "an-arg" to main
EOH
}
function bad-option() {
local opt="$1"
cat <<EOM
$progname: invalid option -- '$opt'
Try '$progname --help' for more information.
EOM
}
function runtime-classname() {
echo "$package.$alt_class"
}
function outdated-to-jar() {
local compare="$1"
[[ -n "$(find "$compare" -type f -newer "$jar")" ]]
}
function build-config-outdated-gradle() {
local settings=settings.gradle
local build=build.gradle
for f in gradle.properties "$settings" "$build"; do
[[ -e "$f" ]] && outdated-to-jar "$f" && return
done
false
}
function jar-outdated() {
[[ ! -e "$jar" ]] || outdated-to-jar src/main
}
function rebuild-if-needed() {
if jar-outdated || build-config-outdated-gradle; then
./gradlew --warning-mode=all jar
fi
}
alt_class=''
debug=false
while getopts :C:J:j:dh-: opt; do
[[ $opt == - ]] && opt=${OPTARG%%=*} OPTARG=${OPTARG#*=}
case $opt in
C | alt-class) alt_class=$OPTARG ;;
J | jvm-option) jvm_flags=("${jvm_flags[@]}" "$OPTARG") ;;
d | debug) debug=true ;;
h | help)
print-help
exit 0
;;
*)
bad-option "$OPTARG"
exit 2
;;
esac
done
shift $((OPTIND - 1))
$debug && set -x
if [[ ! -x "./gradlew" ]]; then
echo "$progname: Not executable: ./gradlew" >&2
exit 2
fi
readonly jar=build/libs/$artifactId-$version.jar
case "$alt_class" in
'') jvm_flags=("${jvm_flags[@]}" -jar "$jar") ;;
*)
readonly runtime_classname="$(runtime-classname "$package.$alt_class")"
jvm_flags=("${jvm_flags[@]}" -cp "$jar" "$runtime_classname")
;;
esac
rebuild-if-needed
expected='TheFoo(label=I AM FOOCUTUS OF BORG)'
captured_output=$(exec java "${jvm_flags[@]}" "$@")
case $captured_output in
$expected ) echo "$captured_output" ;;
* )
echo "$pbold$pred$0: ERROR: expected: $expected, actual: $captured_output$preset"
false
;;
esac