简体   繁体   English

NuGet pack 命令如何生成包版本?

[英]How does the NuGet pack command generate the package version?

NuGet version: 4.5.1.4879 NuGet 版本:4.5.1.4879

When using "project-driven" NuGet package building (ie, passing the .csproj file to the nuget pack command), I thought I understood how NuGet will determine the version to use in the generated package.在使用“项目驱动”的 NuGet 包构建(即,将 .csproj 文件传递​​给nuget pack命令)时,我想我了解 NuGet 将如何确定要在生成的包中使用的版本。 Specifically, that it will use AssemblyInformationalVersion if it exists in the AssemblyInfo, or AssemblyVersion if AssemblyInformationalVersion is not present.具体而言,它将使用AssemblyInformationalVersion如果它存在于集信息,或AssemblyVersion如果AssemblyInformationalVersion不存在。

That seems to work as expected in some of my assemblies (I have a simple 1:1 relationship between assemblies and packages - 1 assembly produces 1 package of the same name), but not in others.这似乎在我的某些程序集中按预期工作(我在程序集和程序包之间有一个简单的 1:1 关系 - 1 个程序集产生 1 个同名程序包),但在其他程序集中则不然。

In a problematic assembly, the AssemblyInfo includes this:在有问题的程序集中,AssemblyInfo 包括以下内容:

[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]

In the .csproj file I also have this, among other stuff (note the <Version> tag):在 .csproj 文件中,我也有这个,除其他外(注意<Version>标签):

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{E233E8E2-EB9E-461F-80C2-63F9AFCC425D}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>XYZ.Core.Messaging</RootNamespace>
    <AssemblyName>XYZ.Core.Messaging</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>

    <Version>2.0.0</Version>

    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
    <TargetFrameworkProfile />
  </PropertyGroup>

BTW, I have no .nuspec file.顺便说一句,我没有 .nuspec 文件。

Despite those settings, when I try to build the package, using this command:尽管有这些设置,当我尝试构建包时,使用以下命令:

nuget pack XYZ.Core.Messaging.csproj  -OutputDirectory "c:\build\nuget-local" -Suffix "local"

It builds the package as XYZ.Core.Messaging.1.0.0-local.nupkg .它将包构建为XYZ.Core.Messaging.1.0.0-local.nupkg

For some other packages I build with a similar command (just a different .csproj file), with the same AssemblyInfo contents, those build the package with version 2.0.0 correctly.对于我使用类似命令(只是一个不同的 .csproj 文件)构建的其他一些包,具有相同的 AssemblyInfo 内容,这些包正确构建了版本 2.0.0 的包。

So where could NuGet be getting the 1.0.0 version from?那么 NuGet 可以从哪里获得 1.0.0 版本呢?

Ensure that you pack the correct configuration.确保您打包了正确的配置。 You might pack a Release configuration while only setting the Version element in the .csproj section related to the Debug configuration, or visa versa.您可以打包 Release 配置,同时仅在与 Debug 配置相关的 .csproj 部分中设置Version元素,反之亦然。

With assembly attributes ( AssemblyVersion , etc.) - note that they are embedded inside the compiled assembly, so for them to take effect you need to build the project, and do it in a correct configuration.使用程序集属性( AssemblyVersion等) - 请注意,它们嵌入在已编译的程序集中,因此要使它们生效,您需要构建项目,并在正确的配置中进行。 Often it happens that you test your code in the Debug configuration, then change version and build without switching to Release, and then pack the Release configuration.通常情况下,您在 Debug 配置中测试代码,然后更改版本和构建而不切换到 Release,然后打包 Release 配置。

To avoid all those problems it's good to always build when packing, and always pack an explicit configuration:为了避免所有这些问题,最好在打包时始终构建,并始终打包显式配置:

nuget pack XYZ.Core.Messaging.csproj  -Build -Properties Configuration=Release ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM