How to Install Maven on Windows, Linux, and Mac – 如何在Windows、Linux和Mac上安装Maven

最后修改: 2016年 12月 19日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Simply put, Maven is a command-line tool for building and managing any Java-based project.

简单地说,Maven是一个命令行工具,用于构建和管理任何基于Java的项目。

The Maven Project provides a simple ZIP file containing a precompiled version of Maven for our convenience. There is no installer. It’s up to us to set up our prerequisites and environment to run Maven.

Maven项目为我们提供了一个简单的ZIP文件,其中包含预编译的Maven版本,方便我们使用。这里没有安装程序。这就需要我们设置运行Maven的先决条件和环境。

The installation of Apache Maven is a simple process of extracting the archive followed by configuring Maven such that the mvn executable is available in the OS classpath.

安装Apache Maven是一个简单的过程,先解压存档,然后配置Maven,使mvn可执行文件在操作系统classpath中可用。

1.1. Prerequisites

1.1.先决条件

Maven is written in Java. So, to run Maven, we need a system that has Java installed and configured properly. We can download an OS-compatible Java JDK from Oracle’s download site, for example.  It’s recommended to install it to a pathname without spaces.

Maven是用Java编写的。因此,要运行Maven,我们需要一个已经安装并正确配置了Java的系统。例如,我们可以从Oracle的下载网站下载一个与操作系统兼容的Java JDK。 建议将其安装到不含空格的路径名下。

Once Java is installed, we need to ensure that the commands from the Java JDK are in our PATH environment variable.

一旦Java安装完毕,我们需要确保Java JDK的命令在我们的PATH环境变量中。

To do so, we will run the command below to get the currently installed version info:

要做到这一点,我们将运行下面的命令来获得当前安装的版本信息。

java -version

2. Installing Maven on Windows

2.在Windows上安装Maven

To install Maven on Windows, we head over to the Apache Maven site to download the latest version and select the Maven zip file, for example, apache-maven-3.8.4-bin.zip.

要在Windows上安装Maven,我们到Apache Maven网站下载最新版本,并选择Maven压缩文件,例如,apache-maven-3.8.4-bin.zip

Then, we unzip it to the folder where we want Maven to live.

然后,我们将其解压到我们希望Maven所在的文件夹中。

2.1. Adding Maven to the Environment Path

2.1.将Maven添加到环境路径中

We add both M2_HOME and MAVEN_HOME variables to the Windows environment using system properties and point them to our Maven folder.

我们使用系统属性在Windows环境中添加M2_HOMEMAVEN_HOME变量,并将其指向Maven文件夹。

Then, we update the PATH variable by appending the Maven bin folder — %M2_HOME%\bin — so that we can run the Maven command everywhere.

然后,我们更新PATH变量,加入Mavenbin文件夹–%M2_HOME%/bin –,这样我们就可以在任何地方运行Maven命令。

To verify it, we run:

为了验证它,我们运行。

mvn -version

The command above should display the Maven version, the Java version, and the operating system information. That’s it. We’ve set up Maven on our Windows system.

上面的命令应显示Maven版本、Java版本和操作系统信息。就这样了。我们已经在Windows系统上设置了Maven。

3. Installing Maven on Linux

3.在Linux上安装Maven

To install Maven on the Linux operating system, we download the latest version from the Apache Maven site and select the Maven binary tar.gz file, for example, apache-maven-3.8.4-bin.tar.gz.

要在Linux操作系统上安装Maven,我们从Apache Maven网站下载最新版本,并选择Maven二进制tar.gz文件,例如,apache-maven-3.8.4-bin.tar.gz

Redhat, Ubuntu, and many other Linux distribution are using the BASH as their default shell. In the below section, we will be using bash commands.

Redhat、Ubuntu和其他许多Linux发行版都使用BASH作为它们的默认外壳。在下面的章节中,我们将使用bash命令。

First, let’s create a location for Maven:

首先,让我们为Maven创建一个位置。

$ mkdir -p /usr/local/apache-maven/apache-maven-3.8.4

Then, we extract the archive to our Maven location:

然后,我们提取存档到我们的Maven位置。

$ tar -xvf apache-maven-3.8.4-bin.tar.gz -C /usr/local/apache-maven/apache-maven-3.8.4

3.1. Adding Maven to the Environment Path

3.1.将Maven添加到环境路径中

We open the command terminal and edit the .bashrc file using the below command:

我们打开命令终端,使用以下命令编辑.bashrc文件。

$ nano ~/.bashrc

Next, let’s add Maven-specific lines to the file:

接下来,让我们在文件中添加Maven专用行。

export M2_HOME=/usr/local/apache-maven/apache-maven-3.8.4 
export M2=$M2_HOME/bin 
export MAVEN_OPTS=-Xms256m -Xmx512m 
export PATH=$M2:$PATH

Once we save the file, we can reload the environment configuration without restarting:

一旦我们保存了该文件,我们就可以重新加载环境配置而不需要重新启动。

$ source ~/.bashrc

Finally, we can verify if Maven has been added:

最后,我们可以验证Maven是否已经被添加。

$ mvn -version

The output should be similar to the below:

输出应该与下面类似。

Apache Maven 3.8.4 (81a9f75f19aa7275152c262bcea1a77223b93445; 2021-01-07T15:30:30+01:29)
Maven home: /usr/local/apache-maven/apache-maven-3.8.4

Java version: 1.8.0_75, vendor: Oracle Corporation

Java home: /usr/local/java-current/jdk1.8.0_75/jre

We have successfully installed Maven on our Linux system.

我们已经在Linux系统上成功安装了Maven。

3.2. Installing Maven on Ubuntu

3.2.在Ubuntu上安装Maven

In a terminal, we run apt-cache search maven to get all the available Maven packages:

在终端,我们运行apt-cache search maven来获取所有可用的Maven包。

$ apt-cache search maven
....
libxmlbeans-maven-plugin-java-doc - Documentation for Maven XMLBeans Plugin
maven - Java software project management and comprehension tool
maven-debian-helper - Helper tools for building Debian packages with Maven
maven2 - Java software project management and comprehension tool

The Maven package always comes with the latest Apache Maven.

Maven包总是带有最新的Apache Maven。

We run the command sudo apt-get install maven to install the latest Maven:

我们运行sudo apt-get install maven命令来安装最新的Maven。

$ sudo apt-get install maven

This will take a few minutes to download. Once downloaded, we can run the mvn -version to verify our installation.

这将需要几分钟的时间来下载。一旦下载,我们可以运行mvn -version来验证我们的安装。

4. Installing Maven on Mac OS X

4.在Mac OS X上安装Maven

To install Maven on Mac OS X operating system, we download the latest version from the Apache Maven site and select the Maven binary tar.gz file, for example, apache-maven-3.8.4-bin.tar.gz.

要在Mac OS X操作系统上安装Maven,我们从Apache Maven网站下载最新版本,并选择Maven二进制tar.gz文件,例如,apache-maven-3.8.4-bin.tar.gz

Then we extract the archive to our desired location.

然后我们提取档案到我们想要的位置。

4.1. Adding Maven to the Environment Path

4.1.将Maven添加到环境路径中

First, let’s open the terminal and switch to the directory where the files were extracted to and then log in as superuser.

首先,让我们打开终端,切换到文件被提取到的目录,然后以超级用户身份登录。

Second, we need to remove the tar.gz archive:

第二,我们需要删除tar.gz存档。

rm Downloads/apache-maven*bin.tar.gz

Third, we have to fix the permissions and switch the Maven contents:

第三,我们必须修复权限并切换Maven内容。

chown -R root:wheel Downloads/apache-maven* 
mv Downloads/apache-maven* /opt/apache-maven

Then, let’s archive the Admin session and add Maven binaries to the path and append:

然后,让我们把管理会话归档,把Maven二进制文件添加到路径中并附加。

exit 
nano $HOME/.profile 
export PATH=$PATH:/opt/apache-maven/bin

Finally, we use Ctrl+x to save and exit from nano.

最后,我们使用Ctrl+x来保存并退出nano

To load the new setup, let’s run:

为了加载新的设置,让我们运行。

bash

Now, we test if Maven is installed successfully using the command below:

现在,我们用以下命令测试Maven是否安装成功。

mvn -version

We are now ready to use Maven on our Mac OS X.

现在我们已经准备好在Mac OS X上使用Maven了。

4.2. Adding Maven to the Environment Path for macOS Catalina or Higher

4.2.在macOS Catalina或更高版本的环境路径中添加Maven

macOS is abandoning the Bourne-Again Shell (bash), the command interpreter for most GNU / Linux distributions, in favor of the Z shell (zsh). This shell can be thought of as an extended version of bash.

macOS正在放弃Bourne-Again Shell(bash),这是大多数GNU/Linux发行版的命令解释器,而支持Z shell(zsh)。这个shell可以被认为是bash的一个扩展版本。

Zsh sets itself apart with its advanced command completion mechanism, typo correction, and even feature-adding module system.

Zsh以其先进的命令完成机制、错别字纠正、甚至功能添加的模块系统使自己与众不同。

In the case of macOS Catalina or a higher version where the default shell is zsh, we have to append to a different file instead:

如果是macOS Catalina或更高版本的默认shell是zsh,我们必须附加到一个不同的文件。

nano ~/.zshenv  
export PATH=$PATH:/opt/apache-maven/bin

To reload the environment, we need to issue:

为了重新加载环境,我们需要发布。

source ~/.zshenv 

The rest of the operations remain the same.

其余操作保持不变。

4.3. HighSierra Compatibility

4.3.HighSierra的兼容性

For HighSierra, we’ll need to additionally add Maven binaries to the path and append:

对于HighSierra,我们需要在路径中额外增加Maven二进制文件,并进行追加。

nano $HOME/.bashrc
export PATH=$PATH:/opt/apache-maven/bin

We use Ctrl+x to save and exit from nano. Then we run bash to load the new setup.

我们使用Ctrl+x来保存并退出nano。然后我们运行bash来加载新的设置。

5. Conclusion

5.结论

In this article, we learned how to install Maven for development on the major operating systems.

在本文中,我们学习了如何在主要操作系统上安装Maven进行开发。

To learn how to get started with Spring and Maven, check out the tutorial here.

要了解如何开始使用Spring和Maven,请查看教程这里