Linux 安装配置 maven

前言

1、概要

Maven 是构建 Java 项目最常用的工具,它不仅能管理 Java 项目中使用的第三方 Jar 包依赖,也能用于项目构建,版本管理与分发等等。

本文是关于如何在 CentOS 中安装与配置 maven。

2、相关环境

  • CentOS 7.9

  • JDK 1.8

  • Maven 3.6.3

3、注意事项

  • 安装过程中需要使用 wget 与 unzip 两个工具,可以使用如下命令提前安装

sudo yum -y install wget unzip
  • 安装用户是具有 sudo 权限的非 root 用户。

正文

1、下载安装包

下载地址:https://maven.apache.org/download.cgi

注:

  • 安装包选择 binary 包进行下载

  • 有 tar.gz 与 zip 两种类型的安装包可下载,内容功能无任何不同,只是文件压缩格式不同

如下是从服务器上直接使用 wget 下载的示例

$ wget -O /tmp/apache-maven-3.6.3-bin.zip https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip
--2021-03-26 14:29:10--  https://downloads.apache.org/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip
Resolving downloads.apache.org (downloads.apache.org)... 88.99.95.219, 2a01:4f8:10a:201a::2
Connecting to downloads.apache.org (downloads.apache.org)|88.99.95.219|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9602303 (9.2M) [application/zip]
Saving to: ‘apache-maven-3.6.3-bin.zip’

1% [>                                                                                                                             ] 106,496     4.59KB/s  eta 33m 42s

2、安装

maven 的安装非常简单,直接解压到指定的目录即可完成安装。

$ sudo unzip /tmp/apache-maven-3.6.3-bin.zip -d /usr/local
Archive:  /tmp/apache-maven-3.6.3-bin.zip
   creating: /usr/local/apache-maven-3.6.3/
   creating: /usr/local/apache-maven-3.6.3/lib/
   creating: /usr/local/apache-maven-3.6.3/boot/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/freebsd32/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/freebsd64/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/linux32/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/linux64/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/osx/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/windows32/
   creating: /usr/local/apache-maven-3.6.3/lib/jansi-native/windows64/
   creating: /usr/local/apache-maven-3.6.3/bin/
   creating: /usr/local/apache-maven-3.6.3/conf/
   creating: /usr/local/apache-maven-3.6.3/conf/logging/
   creating: /usr/local/apache-maven-3.6.3/lib/ext/
  inflating: /usr/local/apache-maven-3.6.3/README.txt  
  inflating: /usr/local/apache-maven-3.6.3/LICENSE  
  inflating: /usr/local/apache-maven-3.6.3/NOTICE  
  inflating: /usr/local/apache-maven-3.6.3/lib/cdi-api.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-cli.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-io.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-lang3.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/guava.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/guice.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/javax.inject.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/jcl-over-slf4j.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/jsoup.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/jsr250-api.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/org.eclipse.sisu.inject.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/org.eclipse.sisu.plexus.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-cipher.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-component-annotations.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-interpolation.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-sec-dispatcher.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-utils.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/slf4j-api.license  
  inflating: /usr/local/apache-maven-3.6.3/boot/plexus-classworlds.license  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/freebsd32/libjansi.so  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/freebsd64/libjansi.so  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/linux32/libjansi.so  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/linux64/libjansi.so  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/osx/libjansi.jnilib  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/windows32/jansi.dll  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/windows64/jansi.dll  
  inflating: /usr/local/apache-maven-3.6.3/bin/m2.conf  
  inflating: /usr/local/apache-maven-3.6.3/bin/mvn.cmd  
  inflating: /usr/local/apache-maven-3.6.3/bin/mvnDebug.cmd  
  inflating: /usr/local/apache-maven-3.6.3/bin/mvn  
  inflating: /usr/local/apache-maven-3.6.3/bin/mvnDebug  
  inflating: /usr/local/apache-maven-3.6.3/bin/mvnyjp  
  inflating: /usr/local/apache-maven-3.6.3/conf/logging/simplelogger.properties  
  inflating: /usr/local/apache-maven-3.6.3/conf/settings.xml  
  inflating: /usr/local/apache-maven-3.6.3/conf/toolchains.xml  
  inflating: /usr/local/apache-maven-3.6.3/lib/ext/README.txt  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-native/README.txt  
  inflating: /usr/local/apache-maven-3.6.3/boot/plexus-classworlds-2.6.0.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-embedder-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-settings-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-settings-builder-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-plugin-api-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-model-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-model-builder-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-builder-support-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-api-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-util-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-shared-utils-3.2.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-io-2.5.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/guice-4.2.1-no_aop.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/guava-25.1-android.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/javax.inject-1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/jsr250-api-1.0.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-utils-3.2.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-sec-dispatcher-1.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-cipher-1.7.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/slf4j-api-1.7.29.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-lang3-3.8.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-core-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-repository-metadata-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-artifact-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-provider-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-impl-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-spi-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/org.eclipse.sisu.inject-0.3.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-component-annotations-2.1.0.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-compat-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/plexus-interpolation-1.25.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/wagon-provider-api-3.3.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/org.eclipse.sisu.plexus-0.3.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/cdi-api-1.0.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/commons-cli-1.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/wagon-http-3.3.4-shaded.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/jsoup-1.12.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/jcl-over-slf4j-1.7.29.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/wagon-file-3.3.4.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-connector-basic-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-resolver-transport-wagon-1.4.1.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/maven-slf4j-provider-3.6.3.jar  
  inflating: /usr/local/apache-maven-3.6.3/lib/jansi-1.17.1.jar 

校验

$ cd /usr/local
$ ll
total 0
drwxr-xr-x. 6 root root 99 Nov  7  2019 apache-maven-3.6.3
drwxr-xr-x. 2 root root  6 Apr 11  2018 bin
drwxr-xr-x. 2 root root  6 Apr 11  2018 etc
drwxr-xr-x. 2 root root  6 Apr 11  2018 games
drwxr-xr-x. 2 root root  6 Apr 11  2018 include
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib
drwxr-xr-x. 2 root root  6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root  6 Apr 11  2018 libexec
drwxr-xr-x. 2 root root  6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root 49 Apr 30  2020 share
drwxr-xr-x. 2 root root  6 Apr 11  2018 src

3、配置 Maven 环境变量

(1) 直接配置 /etc/profile

sudo vim /etc/profile

在文件末尾添加如下配置,并保存退出

# maven config
export M2_HOME=/usr/local/apache-maven-3.6.3
export PATH=$M2_HOME/bin:$PATH

生效

$ source /etc/profile

校验

$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_271-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.21.1.el7.x86_64", arch: "amd64", family: "unix"
$ echo $M2_HOME
/usr/local/apache-maven-3.6.3
$ echo $PATH
/usr/local/apache-maven-3.6.3/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin

(2) 使用软链接配置 maven

这种方法就是先建立一个软链接到 /usr/local/apache-maven-3.6.3,然后配置 M2_HOME=/usr/local/maven,这种做的好处是如果后期需要对 maven 进行版本升级,那我们只需要将 /usr/local/maven 的软链接指向新的 maven 版本位置即可,不用更改配置文件内容。

所以如果你的 maven 版本有升级的需求就建议使用这些方法。

$ cd /usr/local
$ sudo ln -s /usr/local/apache-maven-3.6.3 maven
ll
total 0
drwxr-xr-x. 6 root root 112 Mar 26 15:15 apache-maven-3.6.3
drwxr-xr-x. 2 root root   6 Apr 11  2018 bin
drwxr-xr-x. 2 root root   6 Apr 11  2018 etc
drwxr-xr-x. 2 root root   6 Apr 11  2018 games
drwxr-xr-x. 2 root root   6 Apr 11  2018 include
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib
drwxr-xr-x. 2 root root   6 Apr 11  2018 lib64
drwxr-xr-x. 2 root root   6 Apr 11  2018 libexec
lrwxrwxrwx. 1 root root  29 Mar 26 15:15 maven -> /usr/local/apache-maven-3.6.3
drwxr-xr-x. 2 root root   6 Apr 11  2018 sbin
drwxr-xr-x. 5 root root  49 Apr 30  2020 share
drwxr-xr-x. 2 root root   6 Apr 11  2018 src

配置

sudo vim /etc/profile

在文件末尾添加如下配置,并保存退出

# maven config
export M2_HOME=/usr/local/maven
export PATH=$M2_HOME/bin:$PATH

生效

$ source /etc/profile

校验

$ mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/maven
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: /usr/java/jdk1.8.0_271-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.21.1.el7.x86_64", arch: "amd64", family: "unix"
$ echo $M2_HOME
/usr/local/maven
$ echo $PATH
/usr/local/maven/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/java/default/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin

附录

1、Maven 官网:https://maven.apache.org/

2、Maven 官方安装文档:https://maven.apache.org/install.html

You may also like...

发表评论

您的电子邮箱地址不会被公开。