最近在写一个Spring4.x + SpringMVC + Mybatis零配置的文章,使用的源配的是公司的私有仓库,但是为了让其他人能够通过下载代码在自己本机上运行,所以我就改成OSChina的源,现在网上一大把的文章,说这个源好用,比较方便,结果更新源之后,一直是等待的状态,我还以为我配错了,各种谷歌(没听错,就是google,我们公司能用)、百度查问题,结果还是不行。然后我就把源地址通过浏览器打开,结果打不开,我又试着将jboss和maven官方的源地址放到浏览器打开,发现是能正常打开的。后来在网上找原因,发现OSChina的源关闭了,但是也有它们官方的文章说跟天翼云合作,又可以打开了,结果还是打不开!都是坑啊!鉴于OSChina中国Maven源的不稳定性和不友好性,我决定写这个文章提醒大家,别用OSChina的Maven中国源了,还是用官方的吧!如果你配置了OSChina的Maven中国源,结果出现问题,赶紧换回来,下面我来告诉你这么做:

1. 配置maven项目的setting.xml文件

不管你用哪个IDE,eclipse还是Idea,首先你要修改maven对应的setting.xml文件,主要是在这个文件的标签中加入正常的源地址,而且一定记住,你要将标签的地址用浏览器打开一下,看看是否能访问。如果不能访问,就不要配置了。下面是我setting.xml文件里标签配置:

标签的意思是镜像仓库,可以配多个。由于国内的不靠谱和不稳定,还是原装的好!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<mirrors>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ui</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://uk.maven.org/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
</mirrors>

当然,你最好是配置一下本地仓库,如果你已经配置了,就不用管了。目的就是统一,以免maven默认本地仓库与IDE工具配置的不一致。

1
<localRepository>/Users/admin/.m2/repository</localRepository>

然后在你是项目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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<!-- repositories节点是配置maven下载jar的中央仓库,
默认的是国外的,下载奇慢无比,推荐使用自己搭建sonatype nexus中央仓库 -->
<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo1.maven.org/maven2/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-public-repository-group</id>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</snapshots>
</repository>
<repository>
<id>jboss-deprecated</id>
<name>JBoss Deprecated</name>
<url>https://repository.jboss.org/nexus/content/repositories/deprecated/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-maven2-brew</id>
<name>JBoss Maven 2 Brew Repository</name>
<url>http://repository.jboss.org/maven2-brew/</url>
<layout>default</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>io.spring.repo.maven.release</id>
<url>http://repo.spring.io/release/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>io.spring.repo.maven.milestone</id>
<url>http://repo.spring.io/milestone/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

提示:以上的配置都是我自己机器上亲测的,绝对靠谱,时间是2016年8月10号左右,如果你要配置的话,还是建议先用浏览器访问一下这几个地址,看看能不能访问,然后看看那个访问快,就优先把那个放到最前面!