生成预发行包Building pre-release packages

每当发布具有新版本号的更新包时,NuGet 将其视为所示的“最新稳定版本”,以 Visual Studio 中的包管理器 UI 为例:Whenever you release an updated package with a new version number, NuGet considers that one as the "latest stable release" as shown, for example in the Package Manager UI within Visual Studio:

显示最新稳定版本的包管理器 UI

稳定版本是指被视为足够可靠,可在生产中使用的版本。A stable release is one that's considered reliable enough to be used in production. 最新稳定版本也指作为包更新安装或在包还原期间安装的版本(受制于重新安装和更新包中所述的约束)。The latest stable release is also the one that will be installed as a package update or during package restore (subject to constraints as described in Reinstalling and updating packages).

为了支持软件的版本生命周期,NuGet 1.6 及更高版本允许分配预发行包,其中的版本号包括语义化版本控制后缀,如 -alpha-beta-rcTo support the software release lifecycle, NuGet 1.6 and later allows for the distribution of pre-release packages, where the version number includes a semantic versioning suffix such as -alpha, -beta, or -rc. 有关详细信息,请参阅包版本控制For more information, see Package versioning.

可以使用以下方法之一来指定此类版本:You can specify such versions using one of the following ways:

在准备好发布稳定版本后,只需删除后缀,此包便会优先于任何预发行版本。When you're ready to release a stable version, just remove the suffix and the package takes precedence over any pre-release versions. 同样,请参阅包版本控制Again, see Package versioning.

安装和更新预发行包Installing and updating pre-release packages

默认情况下,NuGet 在处理包时不会包括预发行版本,但按照下文所述方法可更改此行为:By default, NuGet does not include pre-release versions when working with packages, but you can change this behavior as follows:

  • Visual Studio 中的包管理器 UI :在“管理 NuGet 包”UI 中,选中“包括预发行版”框 :Package Manager UI in Visual Studio: In the Manage NuGet Packages UI, check the Include prerelease box:

    Visual Studio 中的“包括预发行版”复选框

    设置或清除此框将刷新包管理器 UI 和可安装的可用版本的列表。Setting or clearing this box will refresh the Package Manager UI and the list of available versions you can install.

  • 包管理器控制台:将 -IncludePrerelease 开关与 Find-PackageGet-PackageInstall-PackageSync-PackageUpdate-Package 命令配合使用。Package Manager Console: Use the -IncludePrerelease switch with the Find-Package, Get-Package, Install-Package, Sync-Package, and Update-Package commands. 请参阅 PowerShell 参考Refer to the PowerShell Reference.

  • NuGet CLI:将 -prerelease 开关与 installupdatedeletemirror 命令配合使用。NuGet CLI: Use the -prerelease switch with the install, update, delete, and mirror commands. 请参阅 NuGet CLI 参考Refer to the NuGet CLI reference

语义化版本控制Semantic versioning

语义化版本控制或 SemVer 约定介绍如何利用版本号中的字符串传达基础代码的含义。The Semantic Versioning or SemVer convention describes how to utilize strings in version numbers to convey the meaning of the underlying code.

在此约定中,每个版本由三部分组成,即 Major.Minor.Patch,其含义分别是:In this convention, each version has three parts, Major.Minor.Patch, with the following meaning:

  • Major:重大更改Major: Breaking changes
  • Minor:新增功能,但可向后兼容Minor: New features, but backwards compatible
  • Patch:仅可向后兼容的 bug 修复Patch: Backwards compatible bug fixes only

然后,通过在修补程序编号后追加连字符和字符串表示预发行版本。Pre-release versions are then denoted by appending a hyphen and a string after the patch number. 从技术上说,可在连字符后使用任意 字符串,NuGet 都会将包视为预发行版。Technically speaking, you can use any string after the hyphen and NuGet will treat the package as pre-release. 然后,NuGet 在适用的 UI 中显示完整的版本号,供使用者自己解读其含义。NuGet then displays the full version number in the applicable UI, leaving consumers to interpret the meaning for themselves.

出于这种考虑,通常最好遵照如下所示的公认命名约定:With this in mind, it's generally good to follow recognized naming conventions such as the following:

  • -alpha:Alpha 版本,通常用于在制品和试验品-alpha: Alpha release, typically used for work-in-progress and experimentation
  • -beta:Beta 版本,通常指可用于下一计划版本的功能完整的版本,但可能包含已知 bug。-beta: Beta release, typically one that is feature complete for the next planned release, but may contain known bugs.
  • -rc:候选发布,通常可能为最终(稳定)版本,除非出现重大 bug。-rc: Release candidate, typically a release that's potentially final (stable) unless significant bugs emerge.


NuGet 4.3.0+ 支持语义化版本控制 v2.0.0,后者支持采用点表示法的预发布号,如 1.0.1-build.23 中所示。NuGet 4.3.0+ supports Semantic Versioning v2.0.0, which supports pre-release numbers with dot notation, as in 1.0.1-build.23. NuGet 4.3.0 之前的版本不支持点表示法。Dot notation is not supported with NuGet versions before 4.3.0. 在 NuGet 的早期版本中,可以使用 1.0.1-build23 之类的格式,但这始终被视为预发布版本。In earlier versions of NuGet, you could use a form like 1.0.1-build23 but this was always considered a pre-release version.

但是,无论使用何种后缀,NuGet 都会按照反向字母顺序向它们赋予优先级:Whatever suffixes you use, however, NuGet will give them precedence in reverse alphabetical order:


如上所示,没有任何后缀的版本始终优先于预发行版本。As shown, the version without any suffix will always take precedence over pre-release versions.

semver2 不需要前导 0,但它们与旧版本架构一致。Leading 0s are not needed with semver2, but they are with the old version schema. 如果在可能使用两位数(或更多数字)的预发行版本标记中使用数字后缀,请使用前导零,如 beta.01 和 beta.05,以便确保数字增大时,可以正确地进行排序。If you use numerical suffixes with pre-release tags that might use double-digit numbers (or more), use leading zeroes as in beta.01 and beta.05 to ensure that they sort correctly when the numbers get larger. 此建议仅适用于旧版架构。This recommendation only applies to the old version schema.