Maven的安装和配置(macOS)
一. 下载Maven
官网下载:https://maven.apache.org/download.cgi
找到“Binary tar.gz archive”行,“Link”列的下载链接。
下载到本地以后,解压后将压缩文件移动到目录中,我这里放到:/Users/用户名/Documents/java 下。
二. 配置环境
1. 按快捷键“Command + Space”打开搜索栏,输入“terminal”启动终端,输入: open .bash_profile ,打开配置文件。
注意:macOS会默认启动zsh终端,如果没有配置过zsh的看看这篇文章:。
2. 加入以下内容:
# Maven
M2_HOME=/Users/用户名/Documents/java/apache-maven-3.6.3
PATH=$PATH:$M2_HOME/bin
export M2_HOME
注意要修改为自己的用户名和maven的文件夹名。
3. 保存文件,关闭文本编辑器,在终端上输入: source .bash_profile ,刷新配置文件。
4. 测试maven是否安装成功,在终端输入: mvn -v 。
三. 设置Maven
1. 打开Maven解压的目录,打开conf文件夹,找到“settings.xml”文件,用文本编辑器打开。
2. 修改以下标签内容:
<localRepository>/Users/用户名/Documents/java/apache-maven-repositorylocalRepository>
<mirrors>
<mirror>
<id>alimavenid>
<mirrorOf>centralmirrorOf>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/repositories/central/url>
mirror>
<mirror>
<id>alimavenid>
<mirrorOf>centralmirrorOf>
<name>aliyun mavenname>
<url>http://maven.aliyun.com/nexus/content/groups/public/url>
mirror>
<mirror>
<id>repo1id>
<mirrorOf>centralmirrorOf>
<name>Human Readable Name for this Mirror.name>
<url>http://repo1.maven.org/maven2/url>
mirror>
<mirror>
<id>repo2id>
<mirrorOf>centralmirrorOf>
<name>Human Readable Name for this Mirror.name>
<url>http://repo2.maven.org/maven2/url>
mirror>
mirrors>
<profile>
<id>jdk-1.8id>
<activation>
<activeByDefault>trueactiveByDefault>
<jdk>1.8jdk>
activation>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
<maven.compiler.compilerVersion>1.8maven.compiler.compilerVersion>
properties>
profile>
缩进的话注意格式,如果格式错误会导致maven无法正常读取配置文件而无法使用。
保存并关闭文本编辑器即可。
3. 重新打开zsh终端,执行命令: mvn help:system ,maven会根据当前环境下载很多基础的jar包,放到刚刚配置的本地仓库的路径。
如果命令结束后,显示“BUILD SUCCESS”,这样maven的环境就已经配置好了。