Ubuntu 16.04安装Apache Maven

安裝JDK

直接通過apt-get安裝OpenJDK 8

apt install default-jdk

撰寫此文時,通過此方法安裝的JDK版本為:

openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-8u151-b12-0ubuntu0.16.04.2-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

如果想用OracleJDK的話:

apt install software-properties-common
add-apt-repository ppa:webupd8team/java
apt update
apt install oracle-java8-installer

透過該方法安裝的OracleJDK的version為:

java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

下載Maven

可以在Apache Maven Project官方下載頁找到最新版Maven下載地址。
下載Binary archive即可:

wget http://httpd-mirror.sergal.org/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz

添加環境變量

假設maven已解壓到/home/maven,現配置M2_HOMEPATH環境變量:

vim /etc/profile.d/maven.sh

寫入以下文本:

export M2_HOME=/home/maven
export PATH=${M2_HOME}/bin:${PATH}

關閉編輯器並使其生效:

chmod +x /etc/profile.d/maven.sh
source /etc/profile.d/maven.sh

驗證安裝

執行mvn -v命令檢驗Maven是否已正確安裝,應顯示類似於如下的版本信息:

Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-24T14:49:05-05:00)
Maven home: /home/maven
Java version: 1.8.0_161, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: ANSI_X3.4-1968
OS name: "linux", version: "2.6.32-042stab126.2", arch: "amd64", family: "unix"

References: