Ubuntu 搭建 Maven 私服仓库 Nexus 笔记

此文章发布于 54 个月前,部分信息可能已经过时,请自行斟酌确认。

Maven 搭建私服可以供公司内部其它同事使用,这样当一个人引用并下载了某个依赖,会被缓存到公司内的私服服务器,这样别的同事在使用的时候就可以直接从内网下载,速度大大提升。

这里记录下最近搭建 Nexus 私服仓库的过程。

官方地址

nexussonatype 公司的免费产品,大多数私服仓库都是通过这个工具搭建的。

官网:https://www.sonatype.com/nexus-repository-oss
文档:https://help.sonatype.com/repomanager3

下载地址

截止目前(2019/11/23)最新下载地址:

https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.19.1-01-unix.tar.gz
https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.19.1-01-win64.zip

参考文档

官网文档:https://help.sonatype.com/repomanager3/installation/run-as-a-service

Ubuntu 安装 Nexus 并以服务方式启动

1、下载 Linux 版压缩包解压到任意目录,这里微酷解压到 /home/nexus

2、新建运行 nexus 的用户 nexus,并设置目录所有者

adduser nexus

chown -R nexus:nexus /home/nexus

3、修改 bin/nexus.rc 文件,将运行用户设置为刚才添加的用户,记得前面的注释去掉。

20191123212754.png

4、创建系统服务

这里我根据官方文档以 systemd 方式运行服务。

新建文件 /etc/systemd/system/nexus.service,增加以下内容

[Unit]
Description=nexus service
After=network.target
  
[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/home/nexus/nexus-3.19.1-01/bin/nexus start
ExecStop=/home/nexus/nexus-3.19.1-01/bin/nexus stop
User=nexus
Restart=on-abort
  
[Install]
WantedBy=multi-user.target

重载服务配置开机启动

sudo systemctl daemon-reload
sudo systemctl enable nexus.service
sudo systemctl start nexus.service

检测是否正常启动

tail -f /home/nexus/sonatype-work/nexus3/log/nexus.log

The tail command verifies that the service has been started successfully. If successful, you should see a message notifying you that it is listening for HTTP.

第一次启动有点慢,大约等一分钟左右。启动成功后运行上面的命令

20191123212953.png

简单配置

访问地址

启动成功后访问:http://192.168.10.10:8081 (IP输入自己的)

Nexus 默认的端口是 8081,可以在 etc/nexus-default.properties 配置中修改

20191122195318124.png

登录密码

默认用户 admin,默认密码已经不再是之前的 admin123,密码是随机的保存在以下文件中:

D:\nexus\sonatype-work\nexus3\admin.password

使用文件中的密码登录,登录成功后会提示修改密码,修改密码后上面的文件会自动删除。

默认仓库

nexus 默认已经配置了 maven 中央仓库的代理,这里为了加快下载速度我又添加了阿里云的镜像。

配置代理阿里镜像仓库加快下载速度:

安装后默认的 maven-public 是代理 maven 官方的仓库,下载速度有点慢,添加上阿里云的放在第一位会加快下载速度。

登录后台点击:create repository

20191123213811.png

类型为:maven2 (proxy)

填写名称和代理 URL 地址即可:

20191122221035754.png

阿里镜像地址为:http://maven.aliyun.com/nexus/content/repositories/central/

然后把新加的添加到 maven-public 并且调到第一位。

20191122221000633.png

Maven 项目使用私服

pom.xml 文件中添加以下配置即可。

<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/
        </url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/
        </url>
    </pluginRepository>
</pluginRepositories>

Gradle 中使用私服

参考:https://weiku.co/article/403/

常见问题

空间不足启动失败

第一次启动失败,发现是虚拟机空间不足,扩展了空间后启动没问题了。

20191122165503070.png

模糊搜索

Search - Maven 中搜索时想模糊匹配要加通配符*,不然搜不出结果,不过顶部的搜索框可以。

20191123214018.png

常用仓库地址

最后修改:2019 年 11 月 29 日 05 : 16 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论