Maven配置阿里云镜像源与私有仓库

配置文件

maven安装路径\conf\setting.xml

阿里云镜像源

在mirrors标签下添加阿里云镜像源配置

  <mirrors>
    <mirror>
      <id>aliyun-mirror</id>
      <mirrorOf>central</mirrorOf>
      <name>阿里云公共仓库</name>
      <url>https://maven.aliyun.com/repository/public</url>
    </mirror>
  </mirrors>

本地仓库路径

从maven仓库下载的包会保存在配置的路径下
默认是${user.home}/.m2/repository

  <localRepository>${env.MAVEN_REPOSITORY}</localRepository>

这里是读取环境变量中的MAVEN_REPOSITORY
这样多个系统可以配置环境变量来统一配置文件
当然也完全可以把本地仓库路径写在这里

私有仓库配置

配置访问私有仓库的用户名密码

  <servers>
    <server>
      <id>nexus</id>
      <username>{私有仓库访问用户名}</username>
      <password>{私有仓库访问密码}</password>
    </server>
  </servers>

配置访问私有仓库的地址

  <profiles>
    <profile>
      <id>nexus-repository</id>
      <repositories>
        <repository>
          <id>nexus</id>
          <url>http://{私有仓库IP端口}/repository/maven-public</url>
        </repository>
      </repositories>
    </profile>
  </profiles>

启用私有仓库

  <activeProfiles>
    <activeProfile>nexus-repository</activeProfile>
  </activeProfiles>

maven3.8.1+

3.8.1及更高版本的maven在mirrors标签中默认有如下配置

  <mirrors>
    <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>

这段配置用于拦截HTTP请求
如果私有仓库未使用HTTPS协议
那么需要在mirrorOf中加上排除私有仓库的配置
示例如下

  <mirrors>
    <mirror>
      <id>maven-default-http-blocker</id>
      <mirrorOf>external:http:*,!nexus</mirrorOf>
      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>
  </mirrors>
posted @ 2024-08-26 13:52  PlaidShirtWholesaler  阅读(133)  评论(0)    收藏  举报