Install Maven and Setup Java on OSX

最近在 Macbook 上安裝 Maven,遇到一些問題,記錄一下安裝過程。 開啟 Terminal 執行 command: $ brew install maven

執行結果:

==> Downloading https://www.apache.org/dyn/closer.cgi?path=maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin
==> Best Mirror http://ftp.twaren.net/Unix/Web/apache/maven/maven-3/3.3.3/binaries/apache-maven-3.3.3-bin.tar.
######################################################################## 100.0%
�  /usr/local/Cellar/maven/3.3.3: 92 files, 9.3M, built in 4 seconds

安裝完成,測試 mvn,執行 command: $ mvn --version

執行結果:

Error: JAVA_HOME is not defined correctly.
  We cannot execute /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java

看起來是 JAVA_HOME 路徑設定的問題,開啟 .bash_profile: $ vi .bash_profile

可以看到原本的 JAVA_HOME 設定如下:

export JAVA_HOME=/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

修改為:

export JAVA_HOME=$(/usr/libexec/java_home)

儲存後關閉檔案,重新載入 .bash_profile: $ source .bash_profile

接著,再重新執行一次 mvn: $ mvn --version

執行結果:

Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T19:57:37+08:00)
Maven home: /usr/local/Cellar/maven/3.3.3/libexec
Java version: 1.7.0_51, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/jre
Default locale: zh_TW, platform encoding: UTF-8
OS name: "mac os x", version: "10.10.2", arch: "x86_64", family: "mac"

以上 Maven 就安裝、設定完成了。

Reference:

  1. How to install Maven on OSX 10.10 (Yosemite)?
  2. Maven, JAVA_HOME is not defined correctly on Mac OSX

Comments

Comments powered by Disqus