Maven 笔记 0x02:maven 的安装配置

12 篇文章 0 订阅
12 篇文章 0 订阅

参考:https://www.bilibili.com/video/BV1Fz4y167p5


Maven 安装配置

  1. 检查 jdk 版本:jdk 版本最好在 1.7 或以上
  2. 下载 maven:https://maven.apache.org/download.cgi
    • 获得一个压缩包如: apache-maven-3.8.3-bin,解压缩。
  3. 配置 maven 环境变量
    • 解压下载到的 maven 压缩包后,把 maven 的根目录配置到系统环境变量中 MAVEN_HOME ,将 bin 目录配置到 path 变量中。

    PS:maven 解压后存放的目录不要包含中文和空格。

  4. 检查 maven 是否安装成功
    • win+r --> cmd --> mvn -v --> 显示版本号则表示安装好并配置完毕。

更换 Maven 远程仓库

在编译 Maven 项目时,它会从 Maven 的仓库下载这个项目所需要的一些依赖(jar 包)。但仓库的默认地址时在国外,可能导致下载速度很慢,此时可以考虑把中央仓库的地址换成国内的地址(如:阿里巴巴的镜像)。

修改方式:在 maven 解压之后(maven命令安装目录)的 conf 目录下的 settings.xml 中进行修改。(如:D:\Program Files\apache-maven-3.8.3\conf\settings.xml

  <mirrors>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  </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>
    <!-- mirror
     | Specifies a repository mirror site to use instead of a given repository. The repository that
     | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
     | for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
     |
    <mirror>
      <id>mirrorId</id>
      <mirrorOf>repositoryId</mirrorOf>
      <name>Human Readable Name for this Mirror.</name>
      <url>http://my.repository.com/repo/path</url>
    </mirror>
     -->
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
    
    <mirror> 
      <id>nexus-aliyun</id> 
      <mirrorOf>central</mirrorOf> 
      <name>Nexus aliyun</name> 
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>

实测更换国内镜像后,mvn compile下载到本地仓库时,快了很多,之前未修改的话下载到某处就卡住没有下载速度了。

修改 Maven 本地仓库地址

Maven 项目所下载下来的依赖(jar包)会存放到本地仓库中,默认在用户目录下的 m2 文件夹下。当 Maven 项目比较多时,依赖也相应增多,可能会导致本地仓库占用空间变得非常大。所以建议把本地仓库的地址修改到磁盘空间充足的路径下。

修改方式:在 maven 解压之后(maven命令安装目录)的 conf 目录下的 settings.xml 中进行修改。(如:D:\Program Files\apache-maven-3.8.3\conf\settings.xml

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

注意到,这个本地仓库地址时被注释了的,需要拷贝出来,然后修改地址让其生效,如果没有定义本地仓库地址,则默认在用户家文件夹下的.m2/repository中。比如现在转移到 D 盘的 m2/repository

<localRepository>D:\m2\repository</localRepository>

(上面实测过是可以的)如果不成功,可能需要把斜杠按模板那样反过来:

<localRepository>D:/m2/repository</localRepository>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值