1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

direnv で java を切り替える

Posted at

direnv は、ディレクトリごとに環境変数を切り替えられるツールで、複数のプロジェクトを並行開発している場合などに便利です。

Mac なら、以下のような関数を ~/.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
}

各プロジェクトでは、こんな風に書くだけでOK

# [プロジェクトのディレクトリ]/.envrc
use_java 11 # JDK 11 を使う

なお、direnv には use_hogeuse hoge と書ける、(微妙に)便利な機能があるので、こうも書けます。

use java 11

なお、Java以外の言語については use が標準で提供されています。

https://github.com/direnv/direnv/blob/master/stdlib.sh
https://github.com/direnv/direnv/blob/master/man/direnv-stdlib.1.md

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?