mirror和repository的区别 概念在文章 → Maven:mirror和repository 区别 - bcombetter - 博客园(已剪藏)


[TOC]

# 在项目中使用 Maven 私服**(Nexus)**

(使用时把aliyun配置在nexus里,pom.xml和setting.xml只留私服)

# 配置认证信息+镜像(Maven/conf/setting.xml)

(使用时把aliyun配置在nexus里,pom.xml和setting.xml只留私服)

效果就是Maven的所有请求必须经过镜像站点(Nexus);当Nexus出现问题后,那么Maven将强制不可用。

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

<id>mirror-redirect2nexus</id>

<!-- 此处配置所有的构建均从私有仓库中下载,*代表所有,也可以写成central -->

<!-- <mirrorOf>*</mirrorOf> -->

<!-- <mirrorOf>nexus</mirrorOf> -->

<mirrorOf>*</mirrorOf>

<name>Nexus osc</name>

<url>http://192.168.213.135:8081/repository/maven-public/</url>

</mirror>



<server>

<id>nexus-releases</id>

<username>admin</username>

<password>admin123</password>

</server>

<server>

<id>nexus-snapshots</id>

<username>admin</username>

<password>admin123</password>

</server>

# Snapshots 与 Releases 的区别

  • nexus-releases: 用于发布 Release 版本
  • nexus-snapshots: 用于发布 Snapshot 版本(快照版)

Release 版本与 Snapshot 定义如下:

Release: 1.0.0/1.0.0-RELEASE

Snapshot: 1.0.0-SNAPSHOT

  • 在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。
  • SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar

# 配置自动化部署(IDEA pom.xml部分)

在(IDEA)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
<distributionManagement>  

<repository>

<id>nexus-releases</id>

<name>Nexus Release Repository</name>

<url>http://192.168.213.135:8081/repository/maven-releases/</url>

</repository>

<snapshotRepository>

<id>nexus-snapshots</id>

<name>Nexus Snapshot Repository</name>

<url>http://192.168.213.135:8081/repository/maven-snapshots/</url>

</snapshotRepository>

</distributionManagement>

注意事项:

  • ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致。
  • 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。

# 部署到仓库

mvn deploy

# 上传第三方 JAR 包

Nexus 3.0 不支持页面上传,可使用 maven 命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 如第三方JAR包:aliyun-sdk-oss-2.2.3.jar

mvn deploy:deploy-file

-DgroupId=com.aliyun.oss

-DartifactId=aliyun-sdk-oss

-Dversion=2.2.3

-Dpackaging=jar

-Dfile=D:\aliyun-sdk-oss-2.2.3.jar

-Durl=http://127.0.0.1:8081/repository/maven-3rd/

-DrepositoryId=nexus-releases

注意事项:

  • 建议在上传第三方 JAR 包时,创建单独的第三方 JAR 包管理仓库,便于管理有维护。(maven-3rd)
  • -DrepositoryId=nexus-releases 对应的是 settings.xml 中 Servers 配置的 ID 名称。(授权)

# 配置代理仓库**(IDEA pom.xml部分)**

(使用时把aliyun配置在nexus里,pom.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
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
<repositories>

<repository>

<id>nexus</id>

<name>Nexus Repository</name>

<url>http://192.168.213.135:8081/repository/maven-public/</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

<releases>

<enabled>true</enabled>

</releases>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>nexus</id>

<name>Nexus Plugin Repository</name>

<url>http://192.168.213.135:8081/repository/maven-public/</url>

<snapshots>

<enabled>true</enabled>

</snapshots>

<releases>

<enabled>true</enabled>

</releases>

</pluginRepository>

</pluginRepositories>

# Nexus添加阿里云仓库(http://192.168.213.135:8081 登陆并设置**):**

(使用时把aliyun配置在nexus里,pom.xml和setting.xml只留私服)

    把一下都配上,并设置maven-public 调整到 maven-release/public/centrol 之前.(范例在代码最下方)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
setting.xml

<mirror>

<id>alimaven</id>

<name>aliyun maven</name>

<url>http://maven.aliyun.com/nexus/content/groups/public/</url>

<mirrorOf>central</mirrorOf>

</mirror>

<mirror>

<id>central</id>

<name>Maven Repository Switchboard</name>

<url>http://repo1.maven.org/maven2/</url>

<mirrorOf>central</mirrorOf>

</mirror>

<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>ibiblio</id>

<mirrorOf>central</mirrorOf>

<name>Human Readable Name for this Mirror.</name>

<url>http://mirrors.ibiblio.org/pub/mirrors/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>

<!-- 中央仓库在中国的镜像 -->

<mirror>

<id>maven.net.cn</id>

<name>oneof the central mirrors in china</name>

<url>http://maven.net.cn/content/groups/public/</url>

<mirrorOf>central</mirrorOf>

</mirror>



pom.xml

<repositories>

<repository>

<id>aliyun-repos</id>

<name>Aliyun Repository</name>

<url>http://maven.aliyun.com/nexus/content/groups/public</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</repository>

</repositories>

<pluginRepositories>

<pluginRepository>

<id>aliyun-repos</id>

<name>Aliyun Repository</name>

<url>http://maven.aliyun.com/nexus/content/groups/public</url>

<releases>

<enabled>true</enabled>

</releases>

<snapshots>

<enabled>false</enabled>

</snapshots>

</pluginRepository>

</pluginRepositories>

范例: 如下图,点击 Repositories → Create repository → maven2 proxy

取名: aliyun-repository

URL:http://maven.aliyun.com/nexus/content/groups/public/,其他默认值即可。

2 配置public-repository:

将aliyun的repository排到最上面