This repository was archived by the owner on Apr 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_java.sh
More file actions
executable file
·151 lines (143 loc) · 5.25 KB
/
switch_java.sh
File metadata and controls
executable file
·151 lines (143 loc) · 5.25 KB
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
#!/usr/bin/env bash
# switch_java
# Change current process environment so that specified Java run time is used
# With direnv the following in .envrc will work with this
# . ~/CM/dotfiles/switch_java.sh
# switch_java corretto-11
# For a more general solution try SDKMAN: http://sdkman.io/
# Pros: Works on Linux, Cygwin, MacOS; does not require sudo (installes into ~/.sdkman)
# Cons: Wants to install / control JDK and related tools like Maven, Groovy, ....
# Does not (appear to) allow integration with existing tools from other package managers
# like brew, brew casks, or self installed packages
# Does not appear to have multiple choices of SDKs (Corretto, Zulu, ...)
[[ $_ != "$0" ]] || { printf "%s must be sourced to be useful." "$0"; exit 1; }
if ! type -t rpath &>/dev/null ; then
EXT_CMDS="$(script_source "path_add_remove")" && [ -e "$EXT_CMDS" ] && source "$EXT_CMDS"
fi
#export ANT_OPTS=-Dbuild.sysclasspath=ignore # I do java little and ant less now
function switch_java {
if [ "$1" == "-T" ]; then
trap "set +x" RETURN && set -x # function debugging
shift
fi
local BASE
local LATEST M
case ${OSTYPE} in
(linux*)
BASE=/opt/local/java # my 'standard' for JVMs on Linux
[ -d ${BASE} ] || BASE=/usr/local/java
[ -d ${BASE} ] || BASE=/usr/java
[ -d ${BASE} ] || BASE=/usr/lib/jvm
[ -d ${BASE} ] || BASE=/etc/alternatives # reminder: /java_sdk*
[ ! -d ${BASE} ] && echo "no local Java base" && return
case $1 in
(current)
LATEST="${BASE}/current"
;;
(14|java14)
LATEST="${BASE}/java-14"
;;
(11|java11)
LATEST="${BASE}/java-11"
;;
(8|java8)
LATEST="${BASE}/java8"
;;
(zulu8)
LATEST="${BASE}/zulu-8"
;;
(zulu11)
LATEST="${BASE}/zulu-11"
;;
(default) # CentOS standards
LATEST="/usr/java/default"
;;
(32) # Any point in keeping this?
LATEST="/usr/lib/jvm/java-*.i386"
;;
(*)
echo "usage: ${FUNCNAME[0]} [current|java11|java14|java8|zulu8|zulu11|32]]"
case $(lsb_release -ds) in
(*CentOS*|*Fedora)
update-alternatives --list | grep java
;;
(*Ubuntu*|*Debian*)
update-alternatives --list java
;;
(*)
echo "Unknown variant $(lsb_release -ds)"
;;
esac
esac
;;
(darwin*)
local JAVAS JAVA
# Yakshave: why the ridiculously tricky use of ${!prefix@} set up by this eval was necessary is now beyond me
IFS=$'\r\n' GLOBIGNORE='*' \
command eval "JAVAS=($(/usr/libexec/java_home -V 2>&1 | grep '^[ ] *' | awk '{print $NF}'))"
# Select the first installed java that matches first command line argument
for i in ${!JAVAS[*]}; do
JAVA="${JAVAS[$i]}"
if [ "${JAVA/${1}/}" != "${JAVA}" ]; then
# Removing $1 from this java found a match; get the path
LATEST=${JAVA}
break
fi
done
if [ -z "${LATEST}" ]; then
printf "Select something unique from:\n"
printf "\t%s\n" "${JAVAS[@]}"
else
: # printf "Selected %s\n" "${LATEST}"
fi
;;
esac
if [ -n "${LATEST}" ]; then
shopt -s nullglob
M=("$LATEST")
if [ -n "${M[0]}" ]; then
LATEST="${M[(${#M[@]}-1)]}" # last element of M
[ -n "${JAVA_HOME}" ] && rpath "${JAVA_HOME}/bin"
export JAVA_HOME=${LATEST}
PATH=${JAVA_HOME}/bin:$PATH
else
echo "no match $LATEST"
fi
shopt -u nullglob
fi
}
function switch_maven {
# Change configuration so specified maven settings get used
local BASE=~/.m2
[ -d ${BASE} ] || return # no maven settings here
local LATEST M
M=${BASE}/settings.xml
case $1 in
(artifactory|a*)
LATEST="${BASE}/settings-artifactory.xml"
;;
(proxy|p*)
LATEST="${BASE}/settings-proxyonly.xml"
;;
(minimal|m*)
LATEST="${BASE}/settings-minimalxml"
;;
(*)
echo "usage: switch_maven [artifactory|proxy|minimal]"
esac
if [ -n "${LATEST}" ]; then
if [ -L "$M" ] || [ ! -e "$M" ]; then
\rm -f ${M}
ln -s ${LATEST} ${M}
else
echo "${M} is not a symbolic link; NOT overriding"
fi
fi
if [ -L "${M}" ]; then
ls -l "${M}"
else
echo "no current settings"
fi
}
# Bash completion
EXT_CMDS="$(script_source "external_java")" && [ -e "$EXT_CMDS" ] && source "$EXT_CMDS"