手把手教你在 IDEA 中配置 Maven

配置前的准备工作:
1、下载 maven 安装包:
下载地址:http://maven.apache.org/download.cgi
2、安装 java,并且保证在环境变量中配置了 java 的安装路径
3、把下载的 maven 安装包,解压到你要安装的文件夹中,我是安装在 F 盘:
F:\apache-maven-3.8.1
4、配置 maven 的环境变量,我是配置到用户变量中了,如图:

然后在 path 里面配置一下 bin 路径,如图:

5、检验是否安装成功,在 cmd 中输入命令:mvn -v。如图:

出现上图,表示成功。
配置本地仓库和阿里云镜像
maven 的默认本地仓库为:
~\.m2\repository
为什么要配置本地仓库?
答:我们在别的盘有下载好的仓库,想让 maven 调用已经下载好的仓库,就要配置。
1、如何配置?
在 maven 安装目录下找到 conf 目录下的 settings.xml 文件,找到如下图:

复制:<localRepository>/path/to/local/repo</localRepository>这句到如下位置:

并更改本地仓库路径,我实在 F 盘下。
2、配置阿里云镜像:
为什么要使用阿里云镜像?
答:为了下载 jar 包快
还是刚才的 settings.xml 文件,找到 mirrors 标签,然后添加到如图位置:

<mirror> <id>nexus-aliyun</id> <mirrorOf>central</mirrorOf> <name>Nexus aliyun</name> <url>http://maven.aliyun.com/nexus/content/groups/public</url> </mirror>
IDEA 中配置 Maven
1、在 idea 中新建一个工程,我这里新建工程名为 maven_workspace。
2、打开 idea 中的设置,如图:

图中右半部分显示的 maven 的安装目录和仓库地址,是 idea 中自带的 maven 的默认地址,这里咱们要重新配置成自己安装的 maven。
3、配置自己安装的 maven


4、创建 maven 工程
创建一个 module,如下图:

直接点 next

配置好目录后,点 finish 即可。
5、导入 junit 包
写配置文件,如下图:

6、创建包
在 main 目录下的 java 下新建一个类,填写 com.atguigu.maven.Hello。如图:

写下代码:

在 test 目录下的 java 目录下创建测试工程,如图:

写下代码:

运行程序:

感谢观看,喜欢的点个关注
版权声明: 本文为 InfoQ 作者【打工人!】的原创文章。
原文链接:【http://xie.infoq.cn/article/a83d723a75355096fc1ebcae6】。文章转载请联系作者。
评论