maven配置阿里云仓库

maven配置阿里云仓库

介绍

中央仓库就是 Maven 的一个默认的远程仓库,Maven 的安装文件中自带了中央仓库的配置($M2_HOME/lib/maven-model-builder.jar)

在很多情况下,默认的中央仓库无法满足项目的需求,这时就需要在pom.xml文件中配置仓库,在pom文件中的配置仅对当前项目有效

避免代码重复性,减少冗余,可在 settings.xml 文件中配置 settings.xml文件中配置可参考:https://developer.aliyun.com/mvn/guide

注意:Maven 自带的中央仓库使用的Id为central 如果其他的仓库声明也是用该Id就会覆盖中央仓库的配置

在maven的settings.xml中配置

对使用该maven的所有项目有效

打开 maven 的配置文件( windows 机器一般在 maven 安装目录的 conf/settings.xml ),在<mirrors></mirrors>标签中添加 mirror 子节点:

1
2
3
4
5
6
<mirror>
  <id>aliyunmaven</id>
  <mirrorOf>*</mirrorOf>
  <name>阿里云公共仓库</name>
  <url>https://maven.aliyun.com/repository/public</url>
</mirror>

如果想使用其它代理仓库,可在<repositories></repositories>节点中加入对应的仓库使用地址。以使用 central 代理仓为例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<repository>
  <id>central</id>
  <url>https://maven.aliyun.com/repository/central</url>
  <releases>
    <enabled>true</enabled>
  </releases>
  <snapshots>
    <enabled>true</enabled>
  </snapshots>
</repository>

在项目根目录下的pom.xml中配置

只对本项目有效

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<repositories>
   <repository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
   </repository>
</repositories>

<pluginRepositories>
   <pluginRepository>
      <id>public</id>
      <name>aliyun nexus</name>
      <url>https://maven.aliyun.com/nexus/content/groups/public/</url>
      <releases>
         <enabled>true</enabled>
      </releases>
      <snapshots>
         <enabled>false</enabled>
      </snapshots>
   </pluginRepository>
</pluginRepositories>

问题:

当url为http时出现如下错误:

1
2
3
4
5
6
7
8
Blocked mirror for repositories: [public (http://maven.aliyun.com/nexus/content/groups/public/, default, releases+snapshots)]

Since Maven 3.8.1 http repositories are blocked.

Possible solutions:
- Check that Maven pom files do not contain http repository http://maven.aliyun.com/nexus/content/groups/public/
- Add a mirror(s) for http://maven.aliyun.com/nexus/content/groups/public/ that allows http url in the Maven settings.xml
- Downgrade Maven to version 3.8.1 or earlier in settings

将http改为https

参考:
https://developer.aliyun.com/mvn/guide
https://www.cnblogs.com/h-c-g/p/9928658.html

Licensed under CC BY-NC-SA 4.0
Built with Hugo
主题 StackJimmy 设计