Skip to content

Instantly share code, notes, and snippets.

@moeinxyz
Last active June 8, 2020 05:21
Show Gist options
  • Save moeinxyz/c7827dbed05c799826e6c7ac66e20f84 to your computer and use it in GitHub Desktop.
Save moeinxyz/c7827dbed05c799826e6c7ac66e20f84 to your computer and use it in GitHub Desktop.
Integrate SDKMAN and Direnv

Direnv and SDKMAN integration

Motivation

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.

Requirments

  • 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.

Integration

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment