Ubuntu2204安装Nexus实现私有NuGet服务
0 引言
Nuget是ASP .NET Gallery的一员,是免费、开源的包管理工具,专注于在.Net / .Net Core应用开发过程中第三方组件库的管理,相对于传统单纯的dll引用要方便、科学得多。其中nuget.org是最著名的Nuget公开库,但是企业内部开发的(业务)公共组件不可能都往公开库上传,所以,企业内部需要一个私有的Nuget仓库来支持。【3】
微软提供了Nuget.server库来搭建,非常简单,缺点是只能安装和部署在Windows服务器上,不能安装部署在linux上。安装方式参考链接,非常详细的步骤。【2】
由于Nuget.server只能部署在Windows上,而已有服务器为Linux系统(Ubuntu22.04),而Nexus可以全平台部署,支持NuGet还支持其他多种格式,便于扩展。
1 准备
2 安装Java OpenJDK 8
-
打开Ubuntu终端,输入如下命令。
sudo apt update sudo apt install openjdk-8-jdk
-
验证安装,在终端输入如下命令
java --version
3 安装Nexus Repository Manager
-
从Nexus官网下载最新版的Nexus安装包,选择UNIX安装包。
-
解压,得到两个文件夹
tar -zxvf nexus-3.67.1-01-unix.tar.gz
-
移动文件夹到opt目录。
mv nexus-3.41.1-01 /opt/nexus mv sonatype-work /opt/
-
设置启动用户
sudo vim /opt/nexus/bin/nexus.rc
将
run_as_user
修改为系统用户名,保存并退出 -
修改配置文件,根据实际需要修改Nexus的堆内存空间。
sudo vim /opt/nexus/bin/nexus.vmoptions
将下列字段修改为实际值,如
1024m
。-Xms1024m -Xmx1024m -XX:MaxDirectMemorySize=1024m
保存并退出。
-
设置Nexus服务IP地址。
cd sonatype-work/nexus3/etc vim nexus.properties
application-port=8081 application-host=127.0.0.1
取消注释,并将ip地址修改为本地主机ip。保存并退出。
4 设置系统服务
Nexus服务默认可以从/opt/nexus/bin/nexus
二进制文件启动,为了便于管理,为Nexus设置系统服务。
-
创建新的服务文件。
sudo vim /etc/systemd/system/nexus.service
-
输入如下内容,将
User
修改为系统用户。[Unit] Description=nexus service After=network.target [Service] Type=forking LimitNOFILE=65536 ExecStart=/opt/nexus/bin/nexus start ExecStop=/opt/nexus/bin/nexus stop User=tk-ai Restart=on-abort [Install] WantedBy=multi-user.target
保存并退出。
-
重新加载系统服务管理器,应用新服务。
sudo systemctl daemon-reload
-
通过
nexus.service
启动Nexus服务。sudo systemctl start nexus.service sudo systemctl enable nexus.service
-
查看服务状态。
sudo systemctl status nexus.service
说明Nexus服务已启动。
-
打开浏览器,输入主机IP加端口8081,即可进入Nexus Repository管理界面。
-
登录管理器,默认用户名为
admin
,密码存储在/opt/sonatype-work/nexus3/admin.properties
。登录成功后根据提示,下一步。
修改密码。
根据需要选择模式。
完成设置。
完成配置后,可以查看Nexus下的一些默认仓库。
仓库说明:
其实Nexus默认已经创建好了Nuget的仓库,并且是创建了3个不同类型的仓库:nuget-group,nuget-hosted,nuget.org-proxy。
-
nuget.org-proxy
类型是proxy,表示代理仓库。我们向它请求包(package)的时候,如果本地有,它就从本地提供,如果本地没有,它会从nuget.org下载到本地,然后给我提供这个包。
-
nuget-hosted
类型是hosted,表示托管仓库。我们一般把自己开发的包上传到该仓库中。
-
nuget-group
类型是group,表示仓库组,它结合了nuget.org-proxy和nuget-hosted,能对外提供上述两者中的包。简而言之,nuget-hosted负责包上传,nuget.org-proxy负责代理包,nuget-group负责提供包。
-
5 上传NuGet包。
-
-
将
nuget.exe
放置到Visual Studio IDE路径下,VS2022社区版路径为C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE。 -
添加Nuget Realms。
因为Nexus认证Nuget是通过Realms来认证,因此,要添加Nuget Realms。
-
查看NuGet API Key。
-
Windows
键,找到Developer Powershell Visual Studio Community
,单击打开。 -
添加NuGet源。
# nuget source Add -Name [源名称] http://172.16.170.179:8081/repository/nuget-hosted/" -UserName [用户名] -Password [密码] nuget source Add -Name **NuGet -Source "http://172.16.170.179:8081/repository/nuget-hosted/" -UserName admin -Password **@123456
添加成功后在VS可看到新添加的源。
-
推送NuGet包。
# nuget push [NuGet包文件] [NuGet API Key] -source [源名称] nuget push test.1.0.0.nupkg [NuGet API Key] -source TkNuGet
-
登录NuGet管理器即可查看到推送的NuGet包。
-
刷新VS中对应的NuGet源,即可查看到推送的包。点击安装即可使用。