= nexus
== nexus 私服工作原理
image::9999-appendix/nexus/002.png[,100%]

== 配置 maven 仓库
阿里云提供了大量的仓库镜像，可参见: https://developer.aliyun.com/mvn/guide
[%header,cols="3,1,1,5"]
|===
| Name                                | Type    | Format  | Remote Repository URL
| proxy-maven-aliyun-public           | proxy   | maven2  | https://maven.aliyun.com/repository/public/
| proxy-maven-aliyun-google           | proxy   | maven2  | https://maven.aliyun.com/repository/google/
| proxy-maven-aliyun-gradle-plugin    | proxy   | maven2  | https://maven.aliyun.com/repository/gradle-plugin/
| proxy-maven-aliyun-spring           | proxy   | maven2  | https://maven.aliyun.com/repository/spring/
| proxy-maven-aliyun-spring-plugin    | proxy   | maven2  | https://maven.aliyun.com/repository/spring-plugin/
| proxy-maven-aliyun-grails-core      | proxy   | maven2  | https://maven.aliyun.com/repository/grails-core/
| proxy-maven-aliyun-apache-snapshots | proxy   | maven2  | https://maven.aliyun.com/repository/apache-snapshots/
|===

TIP:: 可通过以下脚本自动创建上述代理仓库，避免手工处理的繁琐。

下载: link:resources/files/9999-appendix/docker/nexus/repository/repository.zip[repository.zip] 文件,
假设文件下载到: ~/Downloads 文件夹下.

[source,bash]
----
# 进入下载文件夹
cd ~/Downloads/

# 解压 repository.zip
unzip repository.zip

# 进入到解压后的文件夹
cd repository

# 构建 maven 仓库, 假设 nexus 仓库地址为: localhost:8000
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-apache-snapshots.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-google.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-gradle-plugin.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-grails-core.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-public.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-spring.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/maven/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./maven/aliyun-spring-plugin.json

# 更新 maven-public
curl -X PUT http://124.222.99.204:8000/service/rest/v1/repositories/maven/group/maven-public -H "Content-Type: application/json" --user "admin:admin" --data @./maven/group-public.json

# 构建 npm 仓库
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/npm/hosted -H "Content-Type: application/json" --user "admin:admin" --data @./npm/npm-releases.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/npm/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./npm/npmjs.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/npm/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./npm/npmmirror.json
curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/npm/proxy -H "Content-Type: application/json" --user "admin:admin" --data @./npm/taobao.json

curl -X POST http://124.222.99.204:8000/service/rest/v1/repositories/npm/group -H "Content-Type: application/json" --user "admin:admin" --data @./npm/npm-public.json
----

== 配置客户端 gradle
[source,groovy]
----
repositories {
    maven {
        allowInsecureProtocol = true
        url "http://nexus.sc.io:8000/repository/maven-public/"           <1>
    }
}

publishing{
    repositories {
        maven {
            allowInsecureProtocol = true
            url = "http://nexus.sc.io:8000/repository/maven-releases/"   <2>
            credentials {
                username = "admin"                                          <3>
                password = "admin"                                          <4>
            }
        }
    }
}
----
<1> jar 包下载的仓库 url
<2> 发布 jar 包时的仓库 url
<3> 发布 jar 包时的登录用户名
<4> 发布 jar 包时的登录用户密码

== 配置 npm 仓库
[%header,cols="3,1,1,5"]
|===
| Name                          | Type    | Format  | Remote Repository URL
| npm-public                    | group   | npm     |
| npm-releases                  | hosted  | npm     |
| npm-snapshots                 | hosted  | npm     |
| proxy-npm-npmmirror           | proxy   | npm     | https://registry.npmmirror.com/
|===

== 配置 npm 客户端
=== 当前用户的 npm 配置
当前用户的 npm 配置文件位于 ~/.npmrc 文件，通过配置该文件实现对所有项目生效。文件内容如下:
[source,bash]
----
# registry URL
registry=http://nexus.sc.io:8000/repository/npm-public/

# pnpm 的存储目录
store-dir=/Users/wangshaoping/.pnpm-store
----

=== 单个项目的 npm 配置
在每个单独的 node 项目文件下,新建一个名为 .npmrc 的文件, 该文件的配置可以覆盖当前用户的 npm 配置。文件内容如下:

[source,bash]
----
# npm 仓库地址, 在 npm install 时使用
registry=http://nexus.sc.io:8000/repository/npm-public/

# 登录 npm 仓库的用户认证信息, 在 npm publish 时使用
//nexus.sc.io:8000/repository/npm-releases/:_authToken=NpmToken.a0dfe976-168d-342e-a382-7081d6c73edd
#--------------------------------------------- ---------- ---------------------------------------------
#仓库地址 URL                                   认证 key    认证令牌
----

上述令牌可通过以下命令获取:
[source,bash]
----
# 获取用户名和密码为 admin/admin 的令牌, 如果是其他用户和密码, 请调整响应部分
curl -X PUT -H "Content-Type:application/json" \
  -d '{"_id":"org.couchdb.user:admin","name":"admin","password":"admin"}' \
  http://124.222.99.204:8000/repository/npm-releases/-/user/org.couchdb.user:admin

# 输出结果
{
  "rev":"_we_dont_use_revs_any_more",
  "id":"org.couchdb.user:undefined",
  "ok":"true",
  "token":"NpmToken.a0dfe976-168d-342e-a382-7081d6c73edd"
}
----

如果在执行 npm publish 发生认证错误时，再次执行 curl 并获得 token,将其更新到当前项目目录下的 .npmrc 文件中即可。


== 常见问题解决方法
=== Connect to sonatype-download.global.ssl.fastly.net:443
[source,bash]
----
# 如果在运行 nexus 期间出现以下错误:
org.apache.http.conn.ConnectTimeoutException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/128.242.245.93] failed: connect timed out
----
image::9999-appendix/tools/nexus/001.png[,80%]

=== npm publish 失败
[source,bash]
----
# 当执行 npm publish 出现以下错误:
npm ERR! code E401
npm ERR! Unable to authenticate, need: BASIC realm="Sonatype Nexus Repository Manager"
----
image::9999-appendix/tools/nexus/003.png[,80%]