原文链接:使用 dotnet CLI 来打包和发布 .NET Core nuget package

如何使用 visual studio 2015/2017 打包和发布 Nuget package, 微软在这里有介绍:

Create and publish a package

对于只安装了 vs code 和 .net core sdk 的同学,可以参照本文利用 dotnet CLI 来打包和发布 .NET Core nuget package。

打包 Nuget Package

可以使用 dotnet pack 命令来打包已经完成的 .net core library,进入Project所在目录,运行 dotnet pack 命令,会产生如下效果:

  1. 根据 .csproj 中定义的属性生成 .nuspec 文件,默认在 obj 路径下
  2. 根据 .nuspec 文件,打包成 Nuget package,默认在 bin\debug 路径下

.nuspec文件定义了 Nuget package 需要的一些属性,比如 id,version等,内如如下:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Supperxin.SendCloud</id>
<version>1.0.0</version>
<authors>Supperxin.SendCloud</authors>
<owners>Supperxin.SendCloud</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Package Description</description>
<dependencies>
<group targetFramework=".NETStandard1.4">
<dependency id="NETStandard.Library" version="1.6.1" exclude="Build,Analyzers" />
<dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
<files>
<file src="xxx\Supperxin.SendCloud\src\Supperxin.SendCloud\bin\Debug\netstandard1.4\Supperxin.SendCloud.dll" target="lib\netstandard1.4\Supperxin.SendCloud.dll" />
</files>
</package>

这些属性是根据 .csproj 中的属性自动生成的,对应关系如下表:

Attribute/NuSpec Value MSBuild Property Default Notes
Id PackageId AssemblyName $(AssemblyName) from msbuild
Version PackageVersion Version New $(Version) property from msbuild, is semver compatible. Could be “1.0.0”, “1.0.0-beta”, or “1.0.0-beta-00345”.
Authors Authors username of the current user will be the default value
Title Title empty
Owners N/A Not present in NuSpec
Description Description "Package Description"
Copyright Copyright empty
RequireLicenseAcceptance PackageRequireLicenseAcceptance false
LicenseUrl PackageLicenseUrl empty
ProjectUrl PackageProjectUrl empty
IconUrl PackageIconUrl empty
Tags PackageTags empty
ReleaseNotes PackageReleaseNotes empty
RepositoryUrl RepositoryUrl empty
RepositoryType RepositoryType empty
PackageType <PackageType>DotNetCliTool, 1.0.0.0;Dependency, 2.0.0.0</PackageType>

维护 Nuget package 属性

打开 .csproj 文件,按照上表的对应关系,我们做如下修改:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<PackageId>supperxin.test</PackageId>
<PackageVersion>1.0.1</PackageVersion>
<Authors>Supperxin</Authors>
<Title>Test for Nuget package</Title>
<Description>This is a test nuget package.</Description>
<PackageIconUrl>http://cdn.supperxin.com/images/upload/2017/7/7c120726-c8ca-499f-a974-e896e308bfa0.jpg</PackageIconUrl>
<PackageProjectUrl>https://github.com/xiaoxin01/Supperxin.SendCloud</PackageProjectUrl>
<RepositoryUrl>https://github.com/xiaoxin01/Supperxin.SendCloud</RepositoryUrl>
</PropertyGroup> </Project>

修改完成之后再次打包:dotnet pack,发现新生成的 nuspec 文件已经相应的更改:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>Supperxin.SendCloud</id>
<version>1.0.1</version>
<title>Send mail package for SendCloud</title>
<authors>Supperxin</authors>
<owners>Supperxin</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<projectUrl>https://github.com/xiaoxin01/Supperxin.SendCloud</projectUrl>
<iconUrl>http://cdn.supperxin.com/images/upload/2017/7/7c120726-c8ca-499f-a974-e896e308bfa0.jpg</iconUrl>
<description>This is a package for send mail using sendcloud service</description>
<repository url="https://github.com/xiaoxin01/Supperxin.SendCloud" />
<dependencies>
<group targetFramework=".NETStandard1.4">
<dependency id="NETStandard.Library" version="1.6.1" exclude="Build,Analyzers" />
<dependency id="Newtonsoft.Json" version="9.0.1" exclude="Build,Analyzers" />
</group>
</dependencies>
</metadata>
<files>
<file src="xxx\Supperxin.SendCloud\src\Supperxin.SendCloud\bin\Debug\netstandard1.4\Supperxin.SendCloud.dll" target="lib\netstandard1.4\Supperxin.SendCloud.dll" />
</files>
</package>

发布 Nuget package

发布 Nuget package的命令格式如下:

dotnet nuget push [xxxx.nupkg] -k [api key] -s https://www.nuget.org/api/v2/package

api key 可以在 Nuget 网站上注册账号之后获得:

图片:

发布完成之后,可以在网站上看到 Package 的信息:

注意,Package需要等 Nuget 完成索引之后才能被其他人使用,

调用 Nuget package

索引完成之后,就可以通过Nuget Package Manager搜索和下载了,完成之后,Package的应用会自动添加到 csproj 文件中,dotnet restore之后就可以使用了。

参考:

使用 dotnet CLI 来打包和发布 .NET Core nuget package的更多相关文章

  1. 流程自动化RPA,Power Automate Desktop系列 - DotNet Core打包并发布Nuget Package

    一.背景 DotNet Core通常基于Nuget来实现包管理,如果你想要把自己的实现共享给其他人,通常我们需要把本地项目打包好,然后发布到对应的Nuget Server上,以便于其他人可以查找.安装 ...

  2. 使用 dotnet cli 命令上传 nuget 程序包

    前言 前面写了一篇文章介绍了如何将自己的程序集打包成nuget package并上传到nuget.org,传送门.全部是通过网页端来进行操作的,现在介绍一种比较方便快捷的方法就是用dotnet cli ...

  3. 使用dotnet Cli向nuget发布包

    长话短说, 今天分享如何在nuget.org创建并发布.NET Standard package. 前置 安装勾选.NET Core开发套件的Visual Studio; 安装dotnet Cli 从 ...

  4. 使用 DotNet CLI 创建自定义的 WPF 项目模板

    描述 当我们安装完 DotNetCore 3.0 版本的 SDK 后,我们就可以创建基于 DotNetCore 的 WPF 项目模板,通过如下 CLI 可以方便快捷的创建并运行我们的项目: dotne ...

  5. dotnet CLI工具是如何运行你的代码的

    原文连接:https://mattwarren.org/2016/07/04/How-the-dotnet-CLI-tooling-runs-your-code/作者 Matt Warren.授权翻译 ...

  6. dotnet cli

    前言 dotnet cli (Command-Line Interface) .net 源代码和二进制文件管理工具.需要安装 .NET Core SDK. 终端执行 dotnet --info 可以打 ...

  7. dotnet cli 5.0 新特性——dotnet tool search

    dotnet cli 5.0 新特性--dotnet tool search Intro .NET 5.0 SDK 的发布,给 dotnet cli 引入了一个新的特性,dotnet tool sea ...

  8. 在Linux上编译dotnet cli的源代码生成.NET Core SDK的安装包

    .NET 的开源,有了更多的DIY乐趣.这篇博文记录一下在新安装的 Linux Ubuntu 14.04 上通过自己动手编译 dotnet cli 的源代码生成 .net core sdk 的 deb ...

  9. 尝试在mac上用dotnet cli运行asp.net core示例程序

    自从知道微软用dotnet cli取代dnx之后,一直在等dotnet cli支持asp.net core... 昨天看到这篇新闻(ASP.NET Core 1.0 Hello World)后,才知道 ...

随机推荐

  1. Docker自学纪实(一)Docker介绍

    先简单了解一下,做个记录,以便不时之需. Docker简介:Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依 ...

  2. Python数据分析实战视频教程【小蚊子数据分析实战课程】

    点击了解更多Python课程>>> Python数据分析实战视频教程[小蚊子数据分析实战课程] [课程概述] Python数据分析实战' 适用人群:适合需提升竞争力.提升工作效率.喜 ...

  3. python入门:从安装python开始

    python简介: Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种面向对象的解释型计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明 ...

  4. uvm transaction modeling

    1.what is transaction? network transactions tcp/ip wifi 3g/4g bus transactions amba-ahb/apb/axi pci/ ...

  5. POJ:1751-Highways(Kruskal和Prim)

    Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6078 Accepted: 1650 Special Judg ...

  6. /dev/sda is apparently in use by the system; will not make a filesystem here!解决方法

    /dev/sda  is apparently in use by the system; will not make a filesystem here! 翻译:系统显然在使用,不会在这里做文件系统 ...

  7. visual studio 2019安装秘钥

    美国时间4.2微软发布了最新版本的visual studio 2019 现在贴出visual studio2019的秘钥,有需要的请自取: Visual Studio 2019 Enterprise( ...

  8. BZOJ 2721: [Violet 5]樱花

    (X-N)(Y-N)=N^2 #include<cstdio> using namespace std; const int mod=1e9+7; int n,cnt,isprime[10 ...

  9. JSON Undefined 问题

    在IE6和IE7浏览器下或在IE8-IE10浏览器文档模式为IE7及以下时,控制台会报错:JSON is undefined. 这种错误在IE6和IE7浏览器下出现很正常,因为JSON在IE8+浏览器 ...

  10. (英文排版测试)Lorem Ipsum

    Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis tincidunt consequat pretiu ...