1. 安装、配置、更新mirrors为阿里云镜像、新建本地仓库、自定义 settings.xml 文件位置

  2. 其他操作系统配置同理

1.使用 Homebrew 安装 Maven

# 安装 Homebrew(如果尚未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 使用 Homebrew 安装 Maven
brew install maven

验证安装

# 检查 Maven 版本
mvn -v

2. 自定义版本安装

  1. Maven 官方下载

  2. 解压复制到自定义文件夹

  3. 配置.bash_profile​或.zshrc​

    • 文件路径为:/path/to/your/maven​

      # Maven环境
      export MAVEN_HOME=/path/to/your/maven/3.9.5
      export PATH=$PATH:$MAVEN_HOME/bin
      
    • 执行:source ~/.zshrc​

  4. 验证是否安装成功

3. 切换阿里源

  • 修改 maven 安装目录的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>alimaven</id>
          <mirrorOf>*</mirrorOf>
          <name>aliyun maven</name>
          <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
      </mirrors>
    

4. 修改本地仓库位置(也可直接使用默认)

  • 创建仓库文件夹

    ​/path/to/your/Maven/maven-repository​

  • 修改conf文件夹下的settings.xml​文件(localRepository​位置)

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

5. 总结配置文件

5.1 .bash_profile

# maven环境
export MAVEN_HOME=/path/to/your/Maven
export PATH=$PATH:$MAVEN_HOME/bin

5.2 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>alimaven</id>
      <mirrorOf>*</mirrorOf>
      <name>aliyun maven</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
</mirrors>

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