本文介绍如何在CentOS 7安装Maven
用yum install(不推荐)
yum install傻瓜式安装,不用任何配置,但maven版本太旧了(3.0.5),不推荐!
sudo yum install maven
下载安装
1. 到Maven官网下载maven压缩包apache-maven-3.8.3-bin.tar.gz
2. 把压缩包上传到Linux服务器,我用Vagrant虚拟机,所以只需要把压缩包放到shared folder然后执行vagrant reload就可以同步文件
D:\sandbox\code\demo>vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
default: No guest additions were detected on the base box for this VM! Guest
default: additions are required for forwarded ports, shared folders, host only
default: networking, and more. If SSH fails on this machine, please install
default: the guest additions and repackage the box to continue.
default:
default: This is not an error message; everything may continue to work properly,
default: in which case you may ignore this message.
==> default: Rsyncing folder: /cygdrive/d/sandbox/code/demo/ => /vagrant
==> default: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> default: flag to force provisioning. Provisioners marked to run always will still run.
D:\sandbox\code\demo>vagrant ssh
Last login: Thu Nov 11 12:37:59 2021 from 10.0.2.2
[vagrant@localhost ~]$
3. 解压压缩包到指定的安装路径
[vagrant@localhost tmp]$ sudo tar xf /tmp/apache-maven-3.8.3-bin.tar.gz -C /opt
4. 设置环境变量
4.1 确认JAVA安装路径,根据如下命令可以知道JDK 11的安装路径是/usr/lib/jvm/java-11-openjdk
[vagrant@localhost profile.d]$ which java
/usr/bin/java
[vagrant@localhost profile.d]$ cd /usr/lib/jvm
[vagrant@localhost jvm]$ ll
total 0
lrwxrwxrwx. 1 root root 26 Nov 9 15:21 java -> /etc/alternatives/java_sdk
lrwxrwxrwx. 1 root root 29 Nov 9 15:11 java-11 -> /etc/alternatives/java_sdk_11
lrwxrwxrwx. 1 root root 37 Nov 9 15:11 java-11-openjdk -> /etc/alternatives/java_sdk_11_openjdk
drwxr-xr-x. 8 root root 97 Nov 9 15:11 java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64
lrwxrwxrwx. 1 root root 32 Nov 9 15:21 java-1.8.0 -> /etc/alternatives/java_sdk_1.8.0
lrwxrwxrwx. 1 root root 40 Nov 9 15:21 java-1.8.0-openjdk -> /etc/alternatives/java_sdk_1.8.0_openjdk
drwxr-xr-x. 7 root root 135 Nov 9 15:21 java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64
lrwxrwxrwx. 1 root root 34 Nov 9 15:21 java-openjdk -> /etc/alternatives/java_sdk_openjdk
lrwxrwxrwx. 1 root root 21 Nov 10 02:06 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 24 Nov 9 15:11 jre-11 -> /etc/alternatives/jre_11
lrwxrwxrwx. 1 root root 32 Nov 9 15:11 jre-11-openjdk -> /etc/alternatives/jre_11_openjdk
lrwxrwxrwx. 1 root root 42 Nov 9 15:11 jre-11-openjdk-11.0.13.0.8-1.el7_9.x86_64 -> java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64
lrwxrwxrwx. 1 root root 27 Nov 9 15:21 jre-1.8.0 -> /etc/alternatives/jre_1.8.0
lrwxrwxrwx. 1 root root 35 Nov 9 15:21 jre-1.8.0-openjdk -> /etc/alternatives/jre_1.8.0_openjdk
lrwxrwxrwx. 1 root root 51 Nov 9 15:21 jre-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64 -> java-1.8.0-openjdk-1.8.0.312.b07-1.el7_9.x86_64/jre
lrwxrwxrwx. 1 root root 29 Nov 9 15:21 jre-openjdk -> /etc/alternatives/jre_openjdk
4.2 配置环境变量
编辑文件/etc/profile.d/maven.sh
sudo vi /etc/profile.d/maven.sh
编辑如下内容,注意JAVA_HOME根据实际JDK安装路径,参考4.1
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
export M2_HOME=/opt/maven
export MAVEN_HOME=/opt/maven
export PATH=${M2_HOME}/bin:${PATH}
导入环境变量
[vagrant@localhost profile.d]$ sudo chmod +x /etc/profile.d/maven.sh
[vagrant@localhost profile.d]$ ll
total 64
-rw-r--r--. 1 root root 771 Nov 16 2020 256term.csh
-rw-r--r--. 1 root root 841 Nov 16 2020 256term.sh
-rw-r--r--. 1 root root 660 Apr 1 2020 bash_completion.sh
-rw-r--r--. 1 root root 196 Mar 24 2017 colorgrep.csh
-rw-r--r--. 1 root root 201 Mar 24 2017 colorgrep.sh
-rw-r--r--. 1 root root 1741 Nov 16 2020 colorls.csh
-rw-r--r--. 1 root root 1606 Nov 16 2020 colorls.sh
-rw-r--r--. 1 root root 80 Apr 1 2020 csh.local
-rw-r--r--. 1 root root 1706 Nov 16 2020 lang.csh
-rw-r--r--. 1 root root 2703 Nov 16 2020 lang.sh
-rw-r--r--. 1 root root 123 Jul 30 2015 less.csh
-rw-r--r--. 1 root root 121 Jul 30 2015 less.sh
-rwxr-xr-x. 1 root root 132 Nov 11 14:29 maven.sh
-rw-r--r--. 1 root root 81 Apr 1 2020 sh.local
-rw-r--r--. 1 root root 164 Jan 27 2014 which2.csh
-rw-r--r--. 1 root root 169 Jan 27 2014 which2.sh
[vagrant@localhost profile.d]$ source /etc/profile.d/maven.sh
5. 确认安装完成
[vagrant@localhost java-11-openjdk]$ mvn -version
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /opt/maven
Java version: 11.0.13, vendor: Red Hat, Inc., runtime: /usr/lib/jvm/java-11-openjdk-11.0.13.0.8-1.el7_9.x86_64
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.45.1.el7.x86_64", arch: "amd64", family: "unix"
参考
How to Install Apache Maven on CentOS 7
|