Win10安装Maven并更换阿里源

Win10安装Maven并更换阿里源

前言

根据百度百科的描述,Maven 是基于项目对象模型(POM project object model),可以通过一小段描述信息(配置)来管理项目的构建,报告和文档的软件项目管理工具。

Maven的核心功能就是管理项目之间的依赖关系。使用 Java 做过 Web 项目的人都知道,我们使用 Maven 构建项目后,可以在项目中通过 pom.xml 文件的配置来获取 jar 包,而不是通过手动去添加。

Maven 中一共有三个仓库:

  • 本地仓库,Maven会将工程中依赖的构件(Jar包)从远程下载到本机一个目录下管理,默认的本地仓库在 C:\Users\用户名\.m2\repository 目录下
  • 私服,又称为第三方仓库,内部中心仓库。一般是由公司自己设立的,只为本公司内部共享使用。它既可以作为公司内部构件协作和存档;也可作为公用类库镜像缓存,减少在外部访问和下载的频率,使用私服为了减少对中央仓库的访问。
  • 中央仓库是 Maven 内置的远程公共仓库,由 Maven 官方自己维护,里面有大量的常用类库,并包含了世界上大部分流行的开源项目构件。目前是以 java 为主。

添加 Jar 包的过程:

  1. 根据 pom.xml 文件的配置,先查找本地仓库有无该 jar 包。本地仓库有该 jar 包,直接使用即可
  2. 如果本地仓库没有该 Jar 包,且配置了私服,则优先查找私服,再查找中央仓库。如果没有配置私服,则默认查找中央仓库
  3. 找到该 jar 包之后,下载到本地仓库中,下次需要直接从本地仓库取。

安装Maven

安装 Maven 的过程很简单,直接去 Apache Maven 官方主页下载安装包安装即可。

Apache Maven 官方主页的网址是:https://maven.apache.org/

当然我们也可以直接通过百度搜索“maven”,找到 Apache Maven 官方的页面,点击进入。

m1

进入官方页面后,点击左侧导航栏的 Download 标签,进入下载页面。

m2

进入下载页面后往下拉,找到对应的下载链接。可以看到当前官方给出的下载版本是 maven-3.6.3 。我们是在 Windows 下安装 Maven,下载二进制压缩存档(zip)即可。

m3

可以看到,其实这个 zip 包并不大,整体还不到 10M,很快就可以下载完成了。

m4

下载完成后,我们选择在自己喜欢的位置解压安装即可。

m5

配置环境变量

Maven 和 JDK 一样,安装完成之后,还需要配置环境变量。

我们在电脑桌面上找到“此电脑”图标,鼠标右击,选择“属性”

m6

点击“高级系统设置”->“环境变量”->“新建”

m7

新建一个 MAVEN_HOME 环境变量,路径就是刚才 Maven 安装的路径的 bin 目录

m8

接着,在系统变量中找到 path 变量并选中,点击“编辑”

m9

在新的弹窗中点击“新建”,输入 %MAVEN_HOME% ,点击确定,对刚才打开的几个弹窗依次点击“确定”。

m10

到这一步,Maven 就已经配置完成了,我们可以通过命令行检测一下。

打开 cmd,输入 mvn -v 命令,命令行中正常显示 Maven 的版本信息,则证明配置成功。

m11

更换Maven源

我们之前提到了,Maven 获取 Jar 包时会到私服或中央仓库中去查找。

然后 Maven 官方维护的中央仓库地址在国外,下载速度极其缓慢,十分影响用户体验。目前国内用的最多的方法是使用阿里的镜像 maven 源,速度会快上很多

我们找到刚才 maven 解压的目录,进入 conf 文件夹,找到 setting.xml 文件,使用记事本打开,删除掉里面所有的内容,将以下内容填入。

<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	  <mirrors>
			<mirror>
				<id>nexus-aliyun</id>
				<mirrorOf>central</mirrorOf>
				<name>Nexus aliyun</name>
				<url>http://maven.aliyun.com/nexus/content/groups/public</url>
			</mirror> 
	   </mirrors>
</settings>

其中,mirrorOf 标签中写 central 的意思是,会从阿里云的中央仓库进行下载。

更换了阿里的 Maven 源之后,绝大部分的依赖都能轻松下载,好用到飞起。

然而,有一些框架中存在自定义 Jar 包,比如 JEECG,只能从 JEECG 的私服下载依赖,在阿里云仓库下载可能会存在失败的情况。

这种情况我们直接修改 mirrorOf 标签,添加 JEECG 私服,这样依赖包就可以从 JEECG 私服下载了。

<mirrors>
       <mirror>
            <id>nexus-aliyun</id>
            <mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
            <name>Nexus aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </mirror> 
 </mirrors>

参考文章

maven(一) maven到底是个啥玩意~

后端环境jdk、maven安装

Maven私服设置

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值