Skip to content

Files

Latest commit

8c48d37 · Apr 21, 2021

History

History
207 lines (156 loc) · 6.95 KB

Mac-Java-jenv-환경구성.md

File metadata and controls

207 lines (156 loc) · 6.95 KB

Mac Java jenv 환경구성

원하는 자바 버전 설치

https://mkyong.com/java/how-to-install-java-on-mac-osx/

jenv 설치

brew install jenv

셸 별 설정

Bash

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.bash_profile

$ echo 'eval "$(jenv init -)"' >> ~/.bash_profile

Zsh

$ echo 'export PATH="$HOME/.jenv/bin:$PATH"' >> ~/.zshrc

$ echo 'eval "$(jenv init -)"' >> ~/.zshrc

jenv 사용

쉽게 보면 다음 절차로 구성된다.

  1. 원하는 버전 자바 설치 - 이건 jenv 명령과 무관, 위 링크 참고
  2. jenv add /path/to/java_home - 설치한 자바를 jenv에서 인식해서 사용할 수 있도록 추가
  3. jenv local <version> - 현재 폴더 및 하위 폴더에서 사용할 자바 버전 지정

간단해서 별다른 설명이 필요 없다. 그냥 제공되는 help 만 봐도 알 수 있다.

~ 🦑🍕🍺 ❯ jenv --help  
jenv 0.5.4
Usage: jenv <command> [<args>]

Some useful jenv commands are:
   commands    List all available jenv commands
   local       Set or show the local application-specific Java version
   global      Set or show the global Java version
   shell       Set or show the shell-specific Java version
   rehash      Rehash jenv shims (run this after installing executables)
   version     Show the current Java version and its origin
   versions    List all Java versions available to jenv
   which       Display the full path to an executable
   whence      List all Java versions that contain the given executable
   add         Add JDK into jenv. A alias name will be generated by parsing "java -version"

See `jenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/jenv/jenv/blob/master/README.md

java11 설치

~ 🦑🍕🍺 ❯ brew install java11
Updating Homebrew...
==> Homebrew is run entirely by unpaid volunteers. Please consider donating:
  https://github.com/Homebrew/brew#donations
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
Updated 12 formulae.

==> Downloading https://homebrew.bintray.com/bottles/openjdk%4011-11.0.10.catalina.bottle.tar.gz
==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/6dd0a8c323dd861d68d43b6cce0f860225c86ebc6dd583403834b306fbaf1456?response-content-dispos
######################################################################## 100.0%
==> Pouring [email protected]
==> Caveats
For the system Java wrappers to find this JDK, symlink it with
  sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk

openjdk@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have openjdk@11 first in your PATH, run:
  echo 'export PATH="/usr/local/opt/openjdk@11/bin:$PATH"' >> ~/.zshrc

For compilers to find openjdk@11 you may need to set:
  export CPPFLAGS="-I/usr/local/opt/openjdk@11/include"

==> Summary
🍺  /usr/local/Cellar/openjdk@11/11.0.10: 654 files, 297.3MB
~ 🦑🍕🍺 ❯ 

안내 받은 대로 링크 생성

~ 🦑🍕🍺 ❯ sudo ln -sfn /usr/local/opt/openjdk@11/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-11.jdk
Password:
~ 🦑🍕🍺 ❯ 

jenv add

~ 🦑🍕🍺 ❯ jenv help add
Usage: jenv add /path/to/java_home

Add JDK into jenv. A alias name will be generated by parsing "java -version"
~ 🦑🍕🍺 ❯ jenv add /Library/Java/JavaVirtualMachines/openjdk-11.jdk/Contents/Home/
openjdk64-11.0.10 added
11.0.10 added
11.0 added
11 added
~ 🦑🍕🍺 ❯

jenv local

~ 🦑🍕🍺 ❯ jenv help local
Usage: jenv local <version>
       jenv local --unset

Sets the local application-specific Java version by writing the
version name to a file named `.java-version'.

When you run a Java command, jenv will look for a `.java-version'
file in the current directory and each parent directory. If no such
file is found in the tree, jenv will use the global Java version
specified with `jenv global'. A version specified with the
`JENV_VERSION' environment variable takes precedence over local
and global versions.

For backwards compatibility, jenv will also read version
specifications from `.jenv-version' files, but a `.java-version'
file in the same directory takes precedence.

<version> should be a string matching a Java version known to jenv.
The special version string `system' will use your default system Java.
Run `jenv versions' for a list of available Java versions.
zgroup-server git:master 🦑🍕🍺 ❯ java -version                                                                                                  ✭
openjdk version "14.0.2" 2020-07-14
OpenJDK Runtime Environment AdoptOpenJDK (build 14.0.2+12)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 14.0.2+12, mixed mode, sharing)
zgroup-server git:master 🦑🍕🍺 ❯ jenv versions                                                                                                  ✭
* system (set by /Users/user/gitRepo/zepeto/zgroup-server/.java-version)
  1.8
  1.8.0.282
  11
  11.0
  11.0.10
  openjdk64-1.8.0.282
  openjdk64-11.0.10
zgroup-server git:master 🦑🍕🍺 ❯ jenv local 11                                                                                                  ✭
zgroup-server git:master 🦑🍕🍺 ❯ java -version                                                                                                  ✭
openjdk version "11.0.10" 2021-01-19
OpenJDK Runtime Environment (build 11.0.10+9)
OpenJDK 64-Bit Server VM (build 11.0.10+9, mixed mode)
zgroup-server git:master 🦑🍕🍺 ❯

혹시 jenv local 11 실행 후 java -version으로 확인했을 때 새로 지정한 버전이 아니라 기존 버전이 표시되면 터미널을 새로 열어서 확인하면 제대로 나온다.

.java-version 파일 ignore

jenv를 사용하면 .java-version 파일이 생기는데 버전 관리에 포함할 필요가 없는 파일이다.
그렇다고 프로젝트별 .gitignore에 수동으로 꼬박꼬박 넣어주기는 귀찮다.

이럴 때 ~/.gitignore.java-version 파일을 넣어두고, git config --global core.excludesFile = PATH_TO_.gitignore 명령을 사용하면,
현재 사용자 계정에서 만드는 모든 git 리포지토리에서 .java-version 파일은 자동으로 ignore 된다.

위 설명대로 설정한 결과는 대략 다음과 같다.

~ 🦑🍕🍺 ❯ cat ~/.gitconfig                                                                                                                                      

[core]
	excludesFile = /Users/user/.gitignore
[user]
	name = Homo Efficio
	email = [email protected]

~ 🦑🍕🍺 ❯ cat ~/.gitignore 
# Folder view configuration files
.DS_Store
Desktop.ini


# Thumbnail cache files
._*
Thumbs.db


# Compiled files
*.pyc
*.out
*.class


# Application specific files
venv
node_modules
.sass-cache


# jenv
.java-version


# nodeJS
node_modules/