寻求从Nexus 2批量迁移npm/NuGet包至本地Azure DevOps Server的简便方案
Nexus 2 迁移 npm/NuGet 包至本地 Azure DevOps Server 解决方案
npm 包迁移流程
- 导出包列表:调用 Nexus 2 的 REST API 获取所有 npm 包信息,执行命令:
从返回的 JSON 中提取每个包的名称和版本号。curl -u <nexus-username>:<nexus-password> "http://<nexus-url>/repository/npm-hosted/-/all" > npm-packages.json - 批量下载包:临时切换 npm 源到 Nexus,编写 Node.js 脚本遍历包列表,下载
.tgz包:const fs = require('fs'); const { execSync } = require('child_process'); // 切换 npm 源到 Nexus execSync('npm config set registry http://<nexus-url>/repository/npm-hosted/'); execSync('npm login --registry=http://<nexus-url>/repository/npm-hosted/'); // 输入 Nexus 账号密码 const packages = JSON.parse(fs.readFileSync('npm-packages.json', 'utf8')); packages.forEach(pkg => { const { name, version } = pkg; try { execSync(`npm pack ${name}@${version}`); console.log(`下载成功:${name}@${version}`); } catch (err) { console.error(`下载失败:${name}@${version}`, err.message); } }); - 批量上传至 Azure DevOps:切换 npm 源到 Azure DevOps Server 的 feed,登录后批量发布:
编写脚本遍历本地# 配置 Azure DevOps npm 源 npm config set registry http://<azure-devops-url>/<collection>/_packaging/<feed-name>/npm/registry/ # 登录(使用 Azure DevOps PAT 令牌,用户名可填任意值) npm login --registry=http://<azure-devops-url>/<collection>/_packaging/<feed-name>/npm/registry/.tgz文件执行发布:const fs = require('fs'); const { execSync } = require('child_process'); const tgzFiles = fs.readdirSync('./').filter(file => file.endsWith('.tgz')); tgzFiles.forEach(file => { try { execSync(`npm publish ${file}`); console.log(`发布成功:${file}`); } catch (err) { console.error(`发布失败:${file}`, err.message); } });
NuGet 包迁移流程
- 导出包列表:调用 Nexus 2 API 获取 NuGet 包的下载链接:
解析 XML 提取每个包的curl -u <nexus-username>:<nexus-password> "http://<nexus-url>/service/local/repositories/nuget-hosted/content?depth=-1&baseMimeType=application/zip" > nuget-packages.xmlresourceURI(下载链接)和文件名。 - 批量下载包:编写 PowerShell 脚本批量下载
.nupkg文件:$xml = [xml](Get-Content .\nuget-packages.xml) $packages = $xml.data.repository.content.resource | Where-Object { $_.text.EndsWith('.nupkg') } foreach ($pkg in $packages) { $url = $pkg.@resourceURI $fileName = $pkg.text try { Invoke-WebRequest -Uri $url -OutFile $fileName -Credential (Get-Credential -Message "输入 Nexus 账号密码") Write-Host "下载成功:$fileName" } catch { Write-Error "下载失败:$fileName - $_" } } - 批量上传至 Azure DevOps:配置 NuGet 源并批量推送:
# 添加 Azure DevOps NuGet 源(使用 PAT 令牌作为密码) nuget sources add -Name "AzureDevOpsFeed" -Source "http://<azure-devops-url>/<collection>/_packaging/<feed-name>/nuget/v3/index.json" -Username "any" -Password "<pat-token>" # 遍历本地 nupkg 文件推送 Get-ChildItem -Filter *.nupkg | ForEach-Object { try { nuget push $_.FullName -Source "AzureDevOpsFeed" -ApiKey "azdo" Write-Host "推送成功:$($_.Name)" } catch { Write-Error "推送失败:$($_.Name) - $_" } }
注意事项
- 提前在 Azure DevOps Server 中创建好对应的 npm 和 NuGet feed,并确保迁移账号拥有 Packaging 读写权限。
- 生成 Azure DevOps PAT 令牌时,需勾选
Packaging (Read & Write)权限。 - 迁移前先测试少量包,验证流程正确性后再批量操作。
- Nexus 2 API 请求需携带账号密码认证,避免权限不足导致的访问失败。
内容的提问来源于stack exchange,提问作者Arkady Karasin
相关产品推荐
相关产品推荐

