一、问题
做项目用的是公司的私服,现在学习想用阿里云的镜像,这个问题怎么解决呢?
二、解决方法
(1)在maven的conf文件夹下新建一个setting.xml,然后把另一个中央仓库及别的属性配置进去。
(2)在idea里,打开File-Settings-Maven,然后指定项目对应的setting文件为第一步中新建的文件
特别注意:mirrorOf为*的写法
三、研究历程
首先,我想着安装两个maven,然后maven需要配环境变量,果断放弃。
然后,想着在pom文件里面配置,发现总是connect失败
<repositories>
<repository>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
经过仔细观察发现,could not find的内容还是在我之前配置的私服里面找的,很奇怪,我明明配了阿里云的仓库,怎么不在阿里云里面找呢。后来看到有人说是 * 的问题,mirrorOf为*的时候,会覆盖其他的仓库,所以就算配了阿里云仓库也不起作用,所以改成如下的写法,这样的话如果id是alimaven,就可以使用阿里云了
<mirror>
<id>nexus</id>
<!-- 一开始的版本 -->
<!-- <mirrorOf>*</mirrorOf> -->
<mirrorOf>*,!alimaven</mirrorOf>
<url>http://11.11.11.1/nexus/content/groups/public</url>
</mirror>
然后不报上面的错了,出现如下错误
在网上看到有人说阿里云地址换成了下面的,测试发现还是连接不上
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
后来想到idea可以随机指定setting.xml,然后为了方便起见,新建一个setting_aliyun.xml,配置如下
<?xml version="1.0" encoding="UTF-8"?>
<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">
<localRepository>D:\apache-maven-3.5.4\maven-repository</localRepository>
<pluginGroups>
</pluginGroups>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
弄成这样之后,报错同上,只不过更加方便了,不用每次都在pom.xml中配置仓库。
最后终于发现了是代理的问题,在setting_aliyun.xml中加入代理,终于成功了。