-
Notifications
You must be signed in to change notification settings - Fork 1
/
maven_utils.sh
112 lines (91 loc) · 3.05 KB
/
maven_utils.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
#!/bin/bash
#
# maven util functions for build.
#
################################################################################
# api functions:
#
# - mvu::mvn_cmd
################################################################################
#
#_ source guard begin _#
[ -n "${source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F:+has_value}" ] && return
source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=$(realpath -- "${BASH_SOURCE[0]}")
# the value of source guard is the canonical dir path of this script
readonly source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F=${source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F%/*}
#_ source guard end _#
set -eEuo pipefail
# shellcheck source=common_utils.sh
source "$source_guard_364DF1B5_9CA2_44D3_9C62_CDF6C2ECB24F/common_utils.sh"
#################################################################################
# maven operation functions
#################################################################################
readonly MVU_DEFAULT_MVN_OPTS=(
-V --no-transfer-progress
)
mvu::_find_mvn_cmd_path() {
if [ -n "${_MVU_MVN_PATH:-}" ]; then
echo "$_MVU_MVN_PATH"
return
fi
local -r maven_wrapper_name="mvnw"
# 1. find the mvnw from project root dir
if [[ -n "${PROJECT_ROOT_DIR:-}" && -e "$PROJECT_ROOT_DIR/$maven_wrapper_name" ]]; then
_MVU_MVN_PATH="$PROJECT_ROOT_DIR/$maven_wrapper_name"
echo "$_MVU_MVN_PATH"
return
fi
# 2. find mvnw from parent dirs
local d
d=$(realpath -- "$PWD")
while true; do
local mvnw_path="$d/$maven_wrapper_name"
[ -x "$mvnw_path" ] && {
_MVU_MVN_PATH="$mvnw_path"
echo "$_MVU_MVN_PATH"
return
}
[ "/" = "$d" ] && break
d=${d%/*}
done
# 3. find mvn from $PATH
if command -v mvn &>/dev/null; then
_MVU_MVN_PATH=mvn
echo "$_MVU_MVN_PATH"
return
fi
cu::die "$(
echo "fail to find mvn cmd!"
echo "found locations:"
echo " - \$PROJECT_ROOT_DIR/mvnw($PROJECT_ROOT_DIR/mvnw)"
echo " - \$PWD/mvnw($PWD/mvnw) and its parent dirs"
echo " - mvn on \$PATH"
)"
}
mvu::mvn_cmd() {
(($# > 0)) || cu::die "${FUNCNAME[0]} requires arguments! But no provided"
cu::log_then_run "$(mvu::_find_mvn_cmd_path)" \
"${MVU_MVN_OPTS[@]}" \
"$@"
}
mvu::get_mvn_local_repository_dir() {
(($# == 0)) || cu::die "${FUNCNAME[0]} requires no arguments! But provided $#: $*"
if [ -z "${_MVU_MVN_LOCAL_REPOSITORY_DIR:-}" ]; then
echo "$_MVU_MVN_LOCAL_REPOSITORY_DIR"
fi
_MVU_MVN_LOCAL_REPOSITORY_DIR="$(
mvu::mvn_cmd --no-transfer-progress help:evaluate -Dexpression=settings.localRepository |
grep '^/'
)"
[ -n "${_MVU_MVN_LOCAL_REPOSITORY_DIR:-}" ] || cu::die "Fail to find maven local repository directory"
}
################################################################################
# auto run logic when source
################################################################################
mvu::__auto_run_when_source() {
# set VAR if absent
if [ -z "${MVU_MVN_OPTS[*]:-}" ]; then
MVU_MVN_OPTS=("${MVU_DEFAULT_MVN_OPTS[@]}")
fi
}
mvu::__auto_run_when_source