Manage binaries and build artifacts across your software supply chain.
Nexus Repository OSS 是一款通用的软件包仓库管理(Universal Repository Manager)服务。
默认支持许多主流的软件包格式。
- Bower
- Docker
- Git LFS
- Maven
- npm
- NuGet
- PyPI
- Ruby
- Gems
- Yum
- Proxy
仓库类型
- group(仓库组)这是一个仓库聚合的概念,用户仓库地址选择 Group 的地址,即可访问 Group 中配置的,用于方便开发人员自己设定的仓库。maven-public 就是一个 Group 类型的仓库,内部设置了多个仓库,访问顺序取决于配置顺序,3.x 默认为 Releases、Snapshots、Central,当然你也可以自己设置。
- hosted(宿主仓库)私有仓库,内部项目的发布仓库,专门用来存储我们自己生成的 jar 文件
- proxy(代理仓库)代理类型,从远程中央仓库中寻找数据的仓库(可以点击对应的仓库的 Configuration 页签下 Remote Storage 属性的值即被代理的远程仓库的路径),如可配置阿里云 maven 仓库
仓库 Format
maven2、npm、docker、npm等

默认仓库说明:
- maven-central:maven 中央库,默认从
https://repo1.maven.org/maven2/
拉取 jar
- maven-releases:私库发行版 jar,初次安装请将 Deployment policy 设置为 Allow redeploy
- maven-snapshots:私库快照(调试版本)jar
- maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地 maven 基础配置 settings.xml 或项目 pom.xml 中使用
Nexus 3 安装
Nexus Download
1
2
3
4
5
6
7
8
9
|
# macOS 无需 && chown -R 200 ~/Library/nexus-data
sudo mkdir /opt/nexus && sudo chown ynthm /opt/nexus && cd /opt/nexus
wget https://download.sonatype.com/nexus/3/nexus-3.38.1-01-mac.tgz
x nexus-3.38.1-01-mac.tgz
ln -s nexus-3.38.1-01 app
cd app/bin/
cat nexus.vmoptions
./nexus start
lsof -i:8081
|
1
2
3
4
5
6
|
docker volume create --name nexus-data
docker run -d -p 8081:8081 --name nexus -v nexus-data:/nexus-data sonatype/nexus3
# 默认账号密码 第一次登陆要求改密码
docker exec -it nexus /bin/bash
cat /nexus-data/admin.password
|
https
nexus 提供了 web 界面,默认监听 8081 端口,我们几乎所有的操作都可以在 web 界面上完成。当 docker 连接 nexus 时,默认通过 https,但是这只是针对 docker 仓库的端口(需要配置),而不是针对 web 界面。
也就是说,docker 并不会连接 web 界面所监听的 8081 端口,因此我们无需对 web 界面进行 https 配置。但是话又说回来,反正都要配 https 了,那将 web 界面进行 https 也只是顺手的事。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
brew install nginx
brew info nginx
# Docroot is: /opt/homebrew/var/www
# /opt/homebrew/etc/nginx/nginx.conf
# nginx will load all files in /opt/homebrew/etc/nginx/servers/.
# brew services restart nginx
# nginx -g daemon off;
# /opt/homebrew/Cellar/nginx/1.21.6_1
sudo mkdir /opt/nginx && sudo chown ynthm /opt/nginx && cd /opt/nginx
mkdir -p ssl/ynthm.com
# tar -zcvf cert.tar.gz fullchain.cert.pem ynthm.com.key.pem
# scp banwagong:/root/cert.tar.gz ~/Downloads
code /opt/homebrew/etc/nginx/servers/nexus-nginx.conf
|
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
|
server {
listen 80;
listen [::]:80;
server_name nexus.ynthm.com;
return 301 https://nexus.ynthm.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name nexus.ynthm.com;
# 允许大文件上传
client_max_body_size 1G;
ssl_certificate /opt/nginx/ssl/ynthm.com/fullchain.cert.pem;
ssl_certificate_key /opt/nginx/ssl/ynthm.com/ynthm.com.key.pem;
access_log /opt/nginx/log/nexus.ynthm.access.log;
error_log /opt/nginx/log/nexus.ynthm.error.log;
location / {
proxy_pass http://127.0.0.1:8081/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto "https";
}
}
|
1
2
3
4
5
|
nginx -t # 测试配置
nginx # 启动
port-listen
sudo vim /etc/hosts
|
127.0.0.1 nexus.ynthm.com
管理界面
Nexus Repository 管理 https://nexus.ynthm.com
1
2
|
# 默认账号密码 第一次登陆要求改密码
cat /opt/nexus/sonatype-work/nexus3/admin.password
|
Maven Repository
Repositories
> Create repository
> maven2 proxy
Name: aliyun-repository
Proxy Remote storage: http://maven.aliyun.com/nexus/content/groups/public/
maven-public
Group Members 添加 aliyun-repository 移动到最上面
参考设置
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
|
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 本地仓库的位置 -->
<localRepository>${user.home}/.m2/repository</localRepository>
<servers>
<!--服务器元素包含配置服务器时需要的信息 -->
<server>
<!--这是server的id(注意不是用户登陆的id),该id与distributionManagement中repository元素的id相匹配。-->
<id>releases</id>
<username>admin</username>
<password>wang0804</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>wang0804</password>
</server>
<server>
<id>maven-public</id>
<username>admin</username>
<password>wang0804</password>
</server>
</servers>
<mirrors>
<mirror>
<!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
<id>maven-public</id>
<!--镜像名称 -->
<name>maven-public</name>
<mirrorOf>*</mirrorOf>
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
</settings>
|
下载依赖
基本说明
Maven 配置私服下载有两种方式:
- setting.xml:该文件配置的是全局模式
- pom.xml:该文件的配置的是项目独享模式
注意:若 pom.xml 和 setting.xml 同时配置了,以 pom.xml 为准。
当我们在 maven 使用 maven-public 仓库地址的时候,会按照如下顺序访问:本地仓库 –> 私服 maven-releases –> 私服 maven-snapshots –> 远程阿里云 maven 仓库 –> 远程中央仓库。
下载插件
下面是一个使用 pom.xml 配置样例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<pluginRepositories>
<pluginRepository>
<id>maven-nexus</id>
<name>maven-nexus</name>
<url>http://localhost:8081/nexus/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
|
setting.xml
setting.xml 文件配置样例如下。配置后不需要再配置 pom.xml 文件,即可通过私服下载 jar 依赖包。
1
2
3
4
5
6
7
8
9
10
11
12
|
<mirrors>
<mirror>
<!--该镜像的唯一标识符。id用来区分不同的mirror元素。 -->
<id>maven-public</id>
<!--镜像名称 -->
<name>maven-public</name>
<!--*指的是访问任何仓库都使用私服-->
<mirrorOf>*</mirrorOf>
<!--该镜像的URL。构建系统会优先考虑使用该URL,而非使用默认的服务器URL。 -->
<url>http://localhost:8081/repository/maven-public/</url>
</mirror>
</mirrors>
|
如果我们并没有搭建私服,属于个人开发,那么也可以直接配置使用阿里 云maven 仓库:
1
2
3
4
5
6
|
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<mirrorOf>*</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
|
pom.xml
如果配置了 pom.xml,则覆盖 settings.xml 以 pom.xml 为准。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<repositories>
<repository>
<id>maven-nexus</id>
<name>maven-nexus</name>
<url>http://localhost:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
|
没有私服,可以配置阿里云 maven 仓库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<repositories>
<repository>
<id>maven-aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</snapshots>
</repository>
</repositories>
|
发布依赖
首先修改 setting.xml 文件,指定 releases 和 snapshots server 的用户名和密码:
1
2
3
4
5
6
7
8
9
10
11
12
|
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>wang0804</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>wang0804</password>
</server>
</servers>
|
接着在项目的 pom.xml 文件中加入 distributionManagement 节点:
注意:repository 里的 id 需要和上一步里的 server id 名称保持一致。
1
2
3
4
5
6
7
8
9
10
11
12
|
<distributionManagement>
<!-- 发布到快照版本的仓库,即测试版本仓库 -->
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/repository/maven-snapshots/</url>
</snapshotRepository>
<!-- 发布到发行版本的仓库中,即正式版本仓库 -->
<repository>
<id>releases</id>
<url>http://localhost:8081/repository/maven-releases/</url>
</repository>
</distributionManagement>
|
1
2
|
mvn clean deploy
mvn clean deploy -Dmaven.test.skip=true
|
发布第三方 jar 到本地仓库中
1
|
mvn install:install-file -DgroupId=com.github.shalousun -DartifactId=smart-doc -Dversion=1.7.2 -Dpackaging=jar -Dfile=D:\Downloads\smart-doc-1.7.2.jar -DdownloadSources=true -DdownloadJavadocs=true -Durl=file:"w:/maven/bo-repository/"
|
Gradle 配置
1
2
3
4
5
6
7
8
9
|
repositories {
maven {
url 'http://localhost:8081/repository/maven-public/'
credentials {
username 'admin'
password 'wang0804'
}
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
plugins {
id "java-library"
id 'maven-publish'
}
publishing {
publications {
myLibrary(MavenPublication) {
from components.java
}
}
repositories {
maven {
def releasesRepoUrl = "http://localhost:8081/repository/maven-releases"
def snapshotsRepoUrl = "http://localhost:8081/repository/maven-snapshots"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = 'admin'
password = 'wang0804'
}
}
}
}
|
Blob Stores - Docker Registry
登录 > 点击 ⚙ 图标 > 选择 Repository 下面的 Blob Stores > Create Blob Store
- Type:file
- Name:docker-hub
- Path: 存储目录,自动补全,默认即可
- Enable Soft Quota:限制目录大小,有兴趣可以勾选
创建完成后,再创建一个:
- Type:file
- Name:docker-private
⚙ 界面 选择 Repository 下面的 Repositories > Create repository > docker (hosted)
- Name:docker-private;
- Online:勾选;
- HTTP:勾选,并输入 8083 端口(这个端口随意,后面对上就行);
- HTTPS:不要管,https 我们会在 nginx 上面配;
- Allow anonymous docker pull:看需要了,如果你想拉镜像的时候不需要登录就勾选,我不勾选;
- Enable Docker V1 API:如果你使用的 docker 版本够新的话,可以不勾选,我勾了;
- Blob store:选择之前创建的 docker-private;
- Strlct Content Type Validation:貌似是要验证 push 的镜像的格式,我勾了;
- Deployment policy:是否允许部署和更新镜像啥的,没弄懂,我选了 Allow redeploy;
- Available cleanup policles:镜像的清除策略,因为我们还没创建,因此没得选,就先不选吧;
⚙ 界面 选择 Repository 下面的 Repositories > Create repository > docker (proxy)
- Name:docker-hub;
- Online:勾选;
- remote storage:https://registry-1.docker.io,这是 dockerhub 的地址;
- Docker Index:Use Docker Hub;
- Auto blocking enabled:勾选;
- Maximum component age:缓存时间自己定就好,默认 1440 为一天;
- Maximum metadata age:使用默认值即可;
- Blob store:选择之前创建的 docker-hub;
- Not found cache enabled:镜像没有找到是否缓存,默认缓存,按默认来;
- Not found cache TTL:缓存时长使用默认值即可;
没有提到的选项不用管,然后就创建完毕了。使用它可以从 docker hub 上下载镜像了,下载的方式下面会讲到。它应该还可以配置从 k8s.gcr.io 下载,这里的镜像是 docker hub 中没有的,有兴趣的可以试试。
接下来还得创建一个 repository,这个仓库用来分组,将上面两个仓库合并在一起,通过都一个端口来同时访问它们。
Create repository > docker (group):
- Name:docker-group;
- Online:勾选;
- HTTP:8082;
- Allow anonymous docker pull:我没有勾选,需要认证才能下载镜像;
Enable Docker V1 API:如果你使用的 docker 版本够新的话,可以不勾选,我勾了;
- Blob store:它只是一个组,不存放任何东西,因此使用 default 即可;
Member repositories:将 docker-private 和 docker-hub 放到右边;
创建完毕之后,nexus 会自动启动 8082 和 8083 端口。其中 8082 是用来 pull 私有仓库和 dockerhub 镜像的,8083 用来 push 镜像到私有仓库。现在要做的就是使用 nginx 为这些端口配置 https。
分别通过 2222 和 3333 端口来代理 8082 和 8083,并且在 2222 和 3333 上启用 https
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
|
server {
listen 2222 ssl;
server_name nexus.ynthm.com;
ssl_certificate /opt/nginx/ssl/ynthm.com/fullchain.cert.pem;
ssl_certificate_key /opt/nginx/ssl/ynthm.com/ynthm.com.key.pem;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect http:// https://;
proxy_pass http://127.0.0.1:8082;
client_max_body_size 1024m;
client_body_buffer_size 128k;
}
}
server {
listen 3333 ssl;
server_name nexus.ynthm.com;
ssl_certificate /opt/nginx/ssl/ynthm.com/fullchain.cert.pem;
ssl_certificate_key /opt/nginx/ssl/ynthm.com/ynthm.com.key.pem;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect http:// https://;
proxy_pass http://127.0.0.1:8083;
client_max_body_size 1024m;
client_body_buffer_size 128k;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
nginx -t
nginx -s reload
docker login -u admin nexus.ynthm.com:2222
# 坑 ~/.docker/config.json 删除 "credsStore" : "desktop"
docker pull nexus.ynthm.com:2222/busybox
docker login -u admin nexus.ynthm.com:3333
# 给 busybox 加了个 tag,只是为了测试,没有任何作用。
docker tag nexus.ynthm.com:2222/busybox nexus.ynthm.com:3333/busybox:test
docker push nexus.ynthm.com:3333/busybox:test
|
附录
https://help.sonatype.com/repomanager3/formats/maven-repositories