参考链接:

背景:

随着公司前端团队的扩大,需要拆分出公司基础模块供其它项目使用,使用Nexus3搭建公司npm私服。

搭建步骤:

1. 创建npm库:

logo
logo
logo
logo

2. 权限配置:

  • 激活 npm Bearer Token Realm
    logo
  • 创建开发权限组对hosted npm私服库读写权限
    logo
    logo
  • 创建dev帐户并加入到开发权限组
    logo
    logo
1
PS: 匿名用户可以下载私服npm包,只有dev组内的帐户才能publish包到hosted私服

使用:

1.配置nexus私服

  • 查看本机私服配置:
    1
    $ npm config get registry
  • 设置本机配置到group私服:
    1
    2
    $ npm config set registry http://${ip}:8081/repository/${npm_group}/
    (写入到本机.npmrc文件)
  • 此时,项目执行npm install即从nexus上面进行下载包

2.配置publish帐户

有👇两种方式

  • Authentication Using Realm and Login
    1
    2
    3
    4
    5
    6
    7
    8
    9
     $ npm login --registry=http://${ip}:8081/repository/${npm_hosted}/


    Username: dev (各位根据上面创建的帐户自行替换)
    Password: ${dev_pass} (各位根据上面创建的密码自行替换)
    Email: (this IS public) XXX@XXX.com (各位根据上面创建的邮箱自行替换)

    Logged in as dev on http://${ip}:8081/repository/${npm_hosted}/.
    (写入到本机.npmrc文件)
  • Authentication Using Basic Auth
    1
    2
    3
    4
    5
    6
    7
    8
    9
     $ echo -n 'dev:${dev_pass}' | openssl base64  (dev帐户密码base64编码)


    本机.npmrc文件里面增加👇行

    email=XXX@XXX.com
    always-auth=true
    _auth=${base64编码后的值}

3.推送npm包到nexus

有👇两种方式

  • 命令行 + 发布路径

    1
    $ npm publish –registry http://${ip}:8081/repository/${npm_hosted}
  • package.json配置发布路径(推荐)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    项目package.json增加👇配置:

    "publishConfig" : {
    "registry" : "http://${ip}:8081/repository/${npm_hosted}/"
    },


    执行👇命令即可
    $ npm publish

Comment and share

参考链接:

背景:

已经搭建Nexus3.6.0,需要迁移原有 两个 nexus2上的jar包 到nexus3统一管理。

分析:

  • Nexus2.x 服务器直接存储jar包
  • Nexus3.x 服务器存储的是二进制文件
  • 所以不能通过 copy原Nexus2.x服务器上的 repositories目录 到nexus3.X,刷新index方式来迁移。

迁移导入方法:

  1. 官方Upgrading
    具体实施可以参考上一篇blog。
    这种方法有两个小问题:
    - 需要原nexus版本为2.14.x版本
    - 每个nexus2导入都会在nexus3上面都会创建一个仓库,不适合有多个nexus2统一迁移到一个nexus3

  2. mvn deploy命令上传
    如果原nexus2待迁移的jar包并不多,可以使用这种方式。
    1. 只上传jar,自动生成pom.xml (独立jar,pom不需要依赖其它)
    mvn deploy:deploy-file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar -DrepositoryId=nexus -Durl=http://$ip:8081/repository/$repository_name -Dfile=$path/XX.jar

    2. 上传jar 和 pom.xml (pom里面有依赖)
    mvn deploy:deploy-file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -DgeneratePom=false -Dpackaging=jar -DrepositoryId=nexus -Durl=http://$ip:8081/repository/$repository_name -DpomFile=$path/pom.xml -Dfile=$path/XX.jar
    需要注意:
    - 命令里面-DrepositoryId=nexus对应的是本机mvn settings.xml文件配置
    - 写脚本封装上面命令实现批量迁移

  3. http协议上传
    直接使用http put 文件到/repository/$repo-id/$path-of-file
    curl -v -u admin:admin123 –upload-file pom.xml http://$ip:8081/repository/maven-releases/org/foo/1.0/foo-1.0.pom
    我们需要上传的文件:jar和pom.xml

    创建 mavenimport.sh 脚本如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash

# Get command line params
while getopts ":r:u:p:" opt; do
case $opt in
r) REPO_URL="$OPTARG"
;;
u) USERNAME="$OPTARG"
;;
p) PASSWORD="$OPTARG"
;;
esac
done

find . -type f -not -path '*/\.*' -not -path '*/\^archetype\-catalog\.xml*' -not -path '*/\^maven\-metadata\-local*\.xml' -not -path '*/\^maven\-metadata\-deployment*\.xml' -exec curl -u $USERNAME:$PASSWORD -X PUT -v -T {} $REPO_URL{} \;
登陆到Nexus2.x服务器执行
    cd $releases_dir
    cp $path/mavenimport.sh .
    sh mavenimport.sh -u admin -p admin123 -r http://$ip:8081/repository/$release_repository_name

    cd $snapshots_dir
    cp $path/mavenimport.sh .
    sh mavenimport.sh -u admin -p admin123 -r http://$ip:8081/repository/$snapshots_repository_name

需要注意:
    - 上面命令里面 -u admin -p admin123 请更换成nexus管理员帐户密码 (nexus默认管理员/密码 admin/admin123)
    - 上面命令里面 $releases_dir 是指Nexus2.x服务器上存储releases仓库路径。
    - 上面命令里面 $snapshots_dir 是指Nexus2.x服务器上存储snapshots仓库路径。
    - 如果不切换到上面目录里面,那么上传的jar包 group_id会有问题。

Comment and share

背景:

公司之前使用nexus2.13.0版本的nexus,管理毕竟混乱。现搭建nexus3.6.0版本进行规范统一管理。

上篇记录nexus3搭建配置过程,本篇记录迁移nexus2.13.0版本库到nexus3.6.0版本过程。

迁移思路方法:

迁移范围:

仓库 类型 是否迁移
proxy 代理远端仓库 已经在nexus3.6.0版本配置增加,不需要迁移
hosted-3rd 本地内部仓库-第三方 之前没有使用,不需要迁移
hosted-Snapshots 本地内部仓库-快照版本 之前使用比较混乱沟通确认舍弃,不需要迁移
hosted-Releases 本地内部仓库-稳定版本 需要迁移

迁移方法:

如果原有hosted-Releases存放的jar包不多的话,可以考虑下载所有jar到本地,然后使用命令deploy到nexus3.6.0。

现状是原有库里面的jar包很多,groupId很多而且比较不规范(目录层级很复杂)。

参考链接:

升级方法:官方文档

根据官方升级文档说明nexus2.14.1(或者之后版本)才能直升nexus3.x。因为原有nexus版本为2.13.0,所有需要先升级到2.14.x后,再迁移到nexus3.6.0版本。

升级路径为:
nexus2.x – nexus2.14.x – nexus3.x

迁移步骤:

  1. 搭建nexus2.14.5
    • 官网下载 All platforms - Nexus Repository Manager OSS 2.x - bundle.zip
    • 解压缩、安装运行
    • ps:windows遇到的坑,需要使用管理员权限进行cmd安装
      logo
      logo
      logo
  2. 迁移原有nexus2.13.0仓库到nexus2.14.5
    • 仓库存放目录:sonatype-work\nexus\storage
    • 拷贝原nexus2.13.0的sonatype-work\nexus\storage\releases目录下的依赖文件
    • 覆盖nexus2.14.5的releases目录文件
    • 重启nexus2.14.5并刷新索引
      logo
      logo
      logo
  3. 迁移nexus2.14.5仓库到nexus3.6.0
    • 在nexus2.14.5上面配置Upgrade Agent
      logo
      logo
    • 在nexus3.6.0上面执行Upgrade
      logo
      logo
      logo
      logo
      logo
      logo
      logo
      logo
      logo
      logo

后续

使用这种方法,原有的release仓库迁移成功。
logo

但是同时原有nexus的Privileges、Roles、Users也会过来,需要后续进行调整。

当然此方法也可以用来全量升级nexus2.x到nexus3.x。

Comment and share

Nexus3搭建配置

in SCM

参考链接:

PS:

  • nexus分为nexus-repository-oss(免费版)和nexus-repository-pro(商业版)
  • 主要使用版本:2.x和3.x
  • 版本区别:官方文档

JAVA项目依赖管理方法:

  1. 所有依赖jar包都上传提交到代码库里面:
    • 优点:直接获取代码库代码就可以进行编译打包,不需要依赖外部网络(独立个人小项目)
    • 缺点:
      • 依赖包体积很大,导致代码获取推送太耗时和占用带宽;
      • 基础框架依赖包每个代码库都要存放一份(空间浪费);
      • 当前依赖包版本不明确。
  2. 依赖包编译过程中下载,不上传到代码库:
    • 优点:
      • 代码库干净,体积小;
      • 项目显性配置依赖包版本和引用阶段清晰明了。

私服的优点:

  1. 统一服务器代理外部依赖(节省外网带宽);
  2. 项目下载依赖通过内部网络的进行(更快);
  3. 托管内部项目依赖(协作更方便);
  4. 托管第三方依赖(例如:合作伙伴的依赖包)
    logo

搭建环境版本:

  • OS: Windows Server 2012 R2
  • JDK: 1.8.0_71
  • nexus-repository-oss 3.6.0-02

搭建步骤:

  1. 安装JDK
  2. 官网下载:nexus-3.6.0-02-win64.zip,解压缩
  3. 配置windows服务:官方文档
    1
    $install-dir/bin/nexus.exe /install <optional-service-name>
  4. 启动服务:
    1
    nexus.exe /start <optional-service-name>
  5. 网页端登陆配置Nexus
    • http://$ip:8081
    • 用户名/密码:admin/admin123

后台配置:

  1. data目录修改
    1
    2
    3
    4
    5
    $install-dir/bin/nexus.vmoptions

    -Dkaraf.data=../sonatype-work/nexus3
    -Djava.io.tmpdir=../sonatype-work/nexus3/tmp
    -XX:LogFile=../sonatype-work/nexus3/log/jvm.log
  2. 服务端口号修改:
    1
    2
    3
    4
    $data-dir/etc/nexus.properties

    # Jetty section
    application-port=8081
  3. jvm性能调优:
    1
    2
    3
    4
    5
    $install-dir/bin/nexus.vmoptions

    -Xms4G
    -Xmx4G
    -XX:MaxDirectMemorySize=4014M
    以上修改都需要重启服务生效。

前台配置:

使用管理员admin账号登陆

配置邮箱

logo

配置LDAP

logo
logo
logo

配置权限

logo
logo
logo

配置repositories

repositories分为三个种类:

  1. proxy 代理远端仓库
  2. hosted 本地内部仓库
  3. group 组合仓库
    logo

Maven依赖私服配置

修改~/.m2/settings.xml

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
38
39
40
41
42
43
44
<settings>
<servers>
<server>
<id>nexus</id>
<username>用户名</username>
<password>密码</password>
</server>
</servers>
<mirrors>
<mirror>
<!--This sends everything else to /public -->
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<url>http://$ip:8081/repository/maven-public/</url>
</mirror>
</mirrors>
<profiles>
<profile>
<id>nexus</id>
<!--Enable snapshots for the built in central repo to direct -->
<!--all requests to nexus via the mirror -->
<repositories>
<repository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>http://central</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<!--make the profile active all the time -->
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

内部项目上传jar包:

修改项目pom.xml文件增加

1
2
3
4
5
6
7
8
9
10
11
12
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases</name>
<url>http://$ip:8081/repository/maven-releases</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot</name>
<url>http://$ip:8081/repository/maven-snapshots</url>
</snapshotRepository>
</distributionManagement>

然后执行:

1
mvn clean deploy

上传第三方jar包:

nexus3不能使用web端来上传第三方jar包,只能使用命令行:

1
2
mvn deploy:deploy-file -DgroupId=$groupId -DartifactId=$artifactId -Dversion=$version -Dpackaging=jar -Dfile=$path/XX.jar -Durl=http://$ip:8081/repository/$hosted_3rd/ -DrepositoryId=nexus

Comment and share

  • page 1 of 1
Author's picture

Weilong

    Write something about work-life:

PM


Shenzhen