nexus私服搭建
# 下载安装
nexus-2.14.20-02-bundle.tar.gz下载地址 (opens new window)
tar -zxvf nexus-2.14.20-02-bundle.tar.gz
.bin/nexus start
# ./nexus stop
2
3
由于Nexus的UI服务默认不可以由root用户启动,所以如果需要用root用户启动需要在/etc/profile目录下添加如下配置:
export RUN_AS_USER=root
NexusUI服务页面的默认端口是8081,如果需要对端口进行修改,可以在${NEXUS_HOME}/conf/nexus.properties文件中修改以下参数:
application-port=8081
输入用户名密码进行登录:admin/admin123
# Nexus仓库类型介绍
hosted,本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库。
- 3rd party 第三方仓库,如对接公司的jar包,可以上传到这里
- Releases 本地项目发布稳定版
- Snapshots 本地项目发布的快照版本
proxy,代理仓库,公网上发布的jar,它们被用来代理远程的公共仓库,如maven中央仓库。
- Central 中央仓库 https://repo1.maven.org/maven2/ 添加阿里云仓库 https://maven.aliyun.com/repository/central
- Apache Snapshots Apache快照仓库
group,仓库分组,用来合并多个hosted/proxy仓库,当你的项目希望在多个repository使用资源时就不需要多次引用了,只需要引用一个group即可。
# 配置profile
<profiles>
<!-- 指定全局jdk版本及编码 -->
<profile>
<id>myNexus</id>
<activation>
<jdk>1.8</jdk>
<activeByDefault>true</activeByDefault>
</activation>
<repository>
<id>nexusCentral</id>
<name>central</name>
<url>http://*</url>
</repository>
<repository>
<id>nexusReleases</id>
<name>releases</name>
<url>http://*</url>
</repository>
<repository>
<id>nexusSnapshots</id>
<name>snapshots</name>
<url>http://*</url>
</repository>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<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>
<!-- 激活 -->
<activeProfiles>
<activeProfile>myNexus</activeProfile>
</activeProfiles>
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>nexusMirror</id>
<mirrorOf>*</mirrorOf>
<name>nexus maven</name>
<url>http://192.168.56.160:8081/nexus/content/groups/public/</url>
</mirror>
2
3
4
5
6
mirrorOf为*
, 代表所有的jar都从这个url里面下载, 可以配置多个仓库的id,用逗号隔开
# 1、镜像和仓库的区别
# 仓库
仓库主要分为两类:
- remote repository:相当于公共的仓库,大家都能访问到,一般可以用URL的形式访问
- local repository:存放在本地磁盘的一个文件夹,例如,windows上默认是C:\Users\{用户名}.m2\repository目录
repository里存放的都是各种jar包和maven插件。当向仓库请求插件或依赖的时候,会先检查local repository,如果local repository有则直接返回,否则会向remote repository请求,并缓存到local repository。也可以把做的东西放到本地仓库,仅供本地使用;或上传到远程仓库,供大家使用。
# 镜像(mirror)
mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址
# 没有配置mirror
# 配置了mirror
在这种情况下,当我们需要换仓库地址的时候只需要在setting.xml中增加mirror就可以了,而不需要修改pom
# 2、仓库中寻找插件或jar的顺序
(1),在本地仓库中寻找,如果没有则进入下一步
(2),在全局配置的私服仓库(settings.xml中配置的并有激活)中寻找,如果没有则进入下一步。
(3),在项目自身配置的私服仓库(pom.xml)中寻找,如果没有则进入下一步。
(4),在中央仓库中寻找,如果没有则终止寻找。地址在 maven-model-builder-3.8.1.jar
中有配置
参考:镜像(mirror)和仓库(repository)的区别 - 简书 (jianshu.com) (opens new window)
# 发布jar到私服
1、在pom.xml里面指定发布的hosted地址
<distributionManagement>
<repository>
<!-- 此id需要与setting.xml里面的server id一致 -->
<id>nexusReleases</id>
<name>nexusReleases</name>
<url>http://192.168.56.160:8081/nexus/content/repositories/zxp/</url>
</repository>
<snapshotRepository>
<id>nexusSnapshots</id>
<name>nexusSnapshots</name>
<url>http://192.168.56.160:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
2
3
4
5
6
7
8
9
10
11
12
13
2、修改部署用户的密码 deployment /deployment123
3、修改setting.xml
<servers>
<!-- 发布稳定版 -->
<server>
<id>nexusReleases</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<!-- 发布快照版 -->
<server>
<id>nexusSnapshots</id>
<username>deployment</username>
<password>deployment123</password>
</server>
</servers>
2
3
4
5
6
7
8
9
10
11
12
13
14
4、上传deploy
# 开机启动
vi /etc/systemd/system/nexus.service
[Unit]
Description=nexus-server
After=network.target
[Service]
Type=forking
ExecStart=/mnt/app/nexus-2.14/bin/nexus start
ExecStop=/mnt/app/nexus-2.14/bin/nexus stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2
3
4
5
6
7
8
9
10
11
12
systemctl daemon-reload #重启系统服务
systemctl start nexus.service #启动redis服务
systemctl enable nexus.service #设置开机自启动
systemctl disable nexus.service #停止开机自启动
systemctl status nexus.service #查看服务当前状态
systemctl restart nexus.service #重新启动服务
systemctl list-units --type=service #查看所有已启动的服务
2
3
4
5
6
7
vim /etc/init.d/nexus
#!/bin/bash
#chkconfig:2345 20 90
#description:nexus
#processname:nexus
export JAVA_HOME=/mnt/app/jdk1.8.0_301
export NEXUS_HOME=/mnt/app/nexus-2.14
export RUN_AS_USER=root
case $1 in
start) $NEXUS_HOME/bin/nexus start;;
stop) $NEXUS_HOME/bin/nexus stop;;
status) $NEXUS_HOME/bin/nexus status;;
restart) $NEXUS_HOME/bin/nexus restart;;
dump) $NEXUS_HOME/bin/nexus dump;;
console) $NEXUS_HOME/bin/nexus console;;
*) echo "require console | start | stop | restart | status | dump " ;;
esac
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19