为了加强代码的安全性和企业团队开发的高效性,搭建私有的package 包管理服务器是颇有必要的,搭建私有的类库管理服务有如下优势:javascript
使用BaGet 搭建本地私有nuget 服务器php
BaGet 是GitHub上开源的一个项目,是一个轻量级NuGet和符号服务器,查看详情。html
具体实现过程以下:java
{
...
"Mirror": {
"Enabled": true,
"PackageSource": "https://api.nuget.org/v3/index.json"
},
...
}
须要API Key 的能够配置本身的API Key,我是搭建简单的测试版本,全部就没有配置git
"ApiKey": "oy2bgb2qfcnbd3hpge2a2qp3t5ud7khezf7zbyqhurg64u"
3.启动BaGet服务github
启动服务的前提是要安装.NetCore SDKweb
在BaGet文件目录下执行:shell
dotnet BaGet.dll
打开浏览器,运行http://localhost:5000 便可看到nuget 服务已启动
json
为了测试本地nuget 服务器是否可用,在vs中新建一个测试项目,如下简单的代码。api
namespace Easten.Core
{
public class Handler
{
public string Name { get; set; }
public bool IsLock { get; set; }
public void GetValue(int abc,out object value) {
value=abc * 10.3;
}
}
}
使用nuget 命令打包可执行文件
1.使用nuget 命令以前须要安装nuget.exe,点击安装,安装完成以后在系统变量中进行配置才可经过命令调用。
2.验证nuget 有无安装和配置成功,可打开cmd 或者powershell输入 nuget
C:\Windows\System32>nuget NuGet Version: 5.0.2.5988
3.将须要打包的测试项目编译成功后,在项目.csproj目录中打开cmd 或者powershell 并执行:nuget spec
PS F:\我的\开发\技术\Nuget\NugetTest\Easten.Core> nuget spec 已成功建立“Easten.Core.nuspec”。
4.用文本编辑器将上述命令执行完成的.nuspec 文件进行编辑。
<?xml version="1.0"?>
<package >
<metadata>
<id>Dongteng</id>
<version>1.0.0</version>
<title>ceshiceshi</title>
<authors>Dongteng</authors>
<owners>$author$</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>dongteng test</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2019</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>
根据实际的需求修改,通常修改id、version、authors、description等
5.修改完以上信息后执行命令:nuget pack,进行打包.正常结果以下
PS F:\我的\开发\技术\Nuget\NugetTest\Easten.Core> nuget pack
正在尝试从“Easten.Core.csproj”生成程序包。
MSBuild auto-detection: using msbuild version '16.200.19.32702' from 'D:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\bin'.
正在打包“F:\我的\开发\技术\Nuget\NugetTest\Easten.Core\bin\Debug”中的文件。
正在对元数据使用“Easten.Core.nuspec”。
Successfully created package 'F:\我的\开发\技术\Nuget\NugetTest\Easten.Core\Dongteng.1.0.0.nupkg'.
1.运行命令行,将包文件推送到本地nuget服务器中,执行命令:(若是设置了Key,则须要在包名以前添加对应的ApiKey)
dotnet nuget push -s http://localhost:5000/v3/index.json Dongteng.1.0.0.nupkg
2.提示推送成功以后在浏览器中刷新服务,查看推进的内容。
3.在vs 中新建项目,打开nuget 程序包管理控制台,在配置程序包源,指向:http://localhost:5000/v3/index.json
4.在控制台中执行包安装,即安装完成。
PM> Install-Package Dongteng -Version 1.0.0
5.编写测试代码,执行调用
class Program
{
static void Main(string[] args) {
var a = new Easten.Core.Handler();
a.GetValue(222, out var ss);
}
}
经过以上步骤便可完成基础的本地Nuget 服务的搭建及package 包的发布于安装,比较简单。
知识拓展 ,使用NuGet Package Explorer 打包工具对须要发布的程序包执行打包处理
具体操做可参考文档:
1.https://blog.csdn.net/u011523479/article/details/82012340
2.http://www.javashuo.com/article/p-disrzfox-bs.html