In a time when you work on multiple projects with different development environments (mostly in JVM ecosystem), It's hard and repetitive work to change your env or java version, etc. I found the integration between sdkman and direnv as a solution for myself.
- Install sdkman SDKMAN! is a tool for managing parallel versions of multiple Software Development Kits on most Unix based systems.
- Install direnv direnv is an extension for your shell. It augments existing shells with a new feature that can load and unload environment variables depending on the current directory.
Add the following lines to ~/.direnvrc
use_sdk() {
[[ -s "${SDKMAN_DIR}/bin/sdkman-init.sh" ]] && source "${SDKMAN_DIR}/bin/sdkman-init.sh"
while (( "$#" >= 2 )); do
local candidate=$1
local candidate_version=$2
sdk use $candidate $candidate_version
shift 2
done
}
For each project, use your loved tools from SDKMAN by adding a few lines to .envrc
in the root directory of the project.
SDK_OFFLINE_MODE=true use sdk $CANDIDATE $VERSION
For example java 8 is being used for one project, simply add the following line to the .envrc
SDK_OFFLINE_MODE=true use sdk java 8.0.242.hs-adpt