Skip to main content

MAVEN安装配置与IDEA整合

...About 3 minBusinessGuideInstallMaven

MAVEN安装配置与IDEA整合

Windows

下载

官方地址:http://maven.apache.org/download.cgiopen in new window

解压

下载好以后,解压安装到一个目录,我的目录为 D:\Develop\apache-maven-3.5.2

配置环境变量

右键单击"此电脑"-属性-高级系统设置-高级-环境变量…-新建…

新建系统变量MAVEN_HOME为你解压的目录 D:\Develop\apache-maven-3.5.2 ,如下图新建MAVEN_HOME变量:

/img/guide/maven/20210206164532135.png
/img/guide/maven/20210206164532135.png

找到变量名为Path的系统变量,编辑该变量,在变量值最前面添加 %MAVEN_HOME%\bin; 如下图所示

/img/guide/maven/20210206164532136.png
/img/guide/maven/20210206164532136.png

然后点击确定,一步一步确认关闭.

验证

mvn -version

如图所示,这样maven就算安装好了。

/img/guide/maven/20210206164532137.png
/img/guide/maven/20210206164532137.png

配置maven本地仓库

打开D:\Develop\apache-maven-3.5.2/conf/settings.xml文件:

/img/guide/maven/20210206164532138.png
/img/guide/maven/20210206164532138.png

创建本地仓库的文件夹,专门用来存放maven下载的依赖.在配置文件中注释的实例下添加自己的本地仓库路径,我的本地仓库地址为D:Develop epository_boot

/img/guide/maven/20210206164532139.png
/img/guide/maven/20210206164532139.png
<localRepository>D:Develop
epository_boot</localRepository>

配置国内镜像

maven中央库在国外,有时候访问会很慢,尤其是下载较大的依赖的时候,有时候速度会很慢,甚至会出现无法下载的情况,为了解决依赖下载速度的问题,需要配置maven国内镜像,这里配置为阿里的镜像

/img/guide/maven/20210206164532140.png
/img/guide/maven/20210206164532140.png
    <!-- 阿里云仓库 -->
	<mirror>
		<id>alimaven</id>
		<mirrorOf>central</mirrorOf>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
	</mirror>

	<!-- 中央仓库1 -->
	<mirror>
		<id>repo1</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://repo1.maven.org/maven2/</url>
	</mirror>

	<!-- 中央仓库2 -->
	<mirror>
		<id>repo2</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://repo2.maven.org/maven2/</url>
	</mirror>

配置Intellij IDEA

  • 打开 IDEA 选择File - Settings
/img/guide/maven/20210206164533141.png
/img/guide/maven/20210206164533141.png
  • 选择Build,Execution,Deployment
    • Build Tools - Maven
/img/guide/maven/20210206164533142.png
/img/guide/maven/20210206164533142.png

Mac OS

下载

官方地址:http://maven.apache.org/download.cgiopen in new window

解压

tar -zxvf apache-maven-3.6.0-bin.tar.gz -C /Users/baihe/Software/apache-maven-3.6.1

配置环境变量

vim ~/.bash_profile

# 写入
export MAVEN_HOME=/Users/baihe/Software/apache-maven-3.6.1
export PATH=$PATH:$MAVEN_HOME/bin

# 更新环境变量配置
source ~/.bash_profile

验证

mvn -version

如图所示,这样maven就算安装好了。

/img/guide/maven/aHR0cHM6L.png
/img/guide/maven/aHR0cHM6L.png

配置maven本地仓库

Maven很像一个很大仓库,装了很多的jar包,我们需要的时候就去拿,这就涉及到一个“本地仓库”的问题,默认情况,会在/user/name/.m2下,所以我们需要去更改一下。

# 创建仓库目录
cd /Users/baihe/Software/apache-maven-3.6.1
mkdir repository

# 修改自定义的本地仓库地址
vim /Users/baihe/Software/apache-maven-3.6.1/conf/settings.xml

# 修改本地的仓库地址
<localRepository> /Users/baihe/Software/apache-maven-3.6.1/repository </localRepository>

# 保存退出
:wq

配置国内镜像

maven中央库在国外,有时候访问会很慢,尤其是下载较大的依赖的时候,有时候速度会很慢,甚至会出现无法下载的情况,为了解决依赖下载速度的问题,需要配置maven国内镜像,这里配置为阿里的镜像

/img/guide/maven/20210206164532140.png
/img/guide/maven/20210206164532140.png
    <!-- 阿里云仓库 -->
	<mirror>
		<id>alimaven</id>
		<mirrorOf>central</mirrorOf>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
	</mirror>

	<!-- 中央仓库1 -->
	<mirror>
		<id>repo1</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://repo1.maven.org/maven2/</url>
	</mirror>

	<!-- 中央仓库2 -->
	<mirror>
		<id>repo2</id>
		<mirrorOf>central</mirrorOf>
		<name>Human Readable Name for this Mirror.</name>
		<url>http://repo2.maven.org/maven2/</url>
	</mirror>

配置Intellij IDEA

  • 打开 IDEA 选择File - Settings

  • 选择Build,Execution,Deployment

    • Build Tools - Maven
/img/guide/maven/aHR0cHM6M.png
/img/guide/maven/aHR0cHM6M.png

至此Maven就安装配置并整合到IDEA中了.