direnv is a tool that allows you to switch environment variables for each directory, which is useful when you are developing multiple projects in parallel.
On Mac, if you define the following function in ~ / .direnvrc
...
# ~/.direnvrc
use_java() {
if [ "$#" -ne 1 ]; then
echo "usage: use java VERSION" >&2
return 1
fi
local v
v="$1"
if [ "$v" -le "8" ]; then
v="1.$v"
fi
export JAVA_HOME="$(/usr/libexec/java_home -v "$v")"
PATH_add $JAVA_HOME/bin
}
For each project, just write like this
# [Project directory]/.envrc
use_java 11 #Use JDK 11
Note that direnv has a (subtly) useful feature that allows you to write ʻuse_hoge as ʻuse hoge
, so you can also write this.
use java 11
For languages other than Java, ʻuse` is provided as standard.
https://github.com/direnv/direnv/blob/master/stdlib.sh https://github.com/direnv/direnv/blob/master/man/direnv-stdlib.1.md
Recommended Posts