如何为 projectReference 设置特定于构建的版本号,以便 NuGet 包依赖项列出正确的版本

How do I set the build-specific version number for a projectReference so that the NuGet package dependency lists the correct version

在 Visual Studio 2017 年使用新的 CSProj 格式,我编写了一个目标文件来处理名为 CreateLocalPackages 的新配置,它让构建在特定的本地文件夹中创建一个 NuGet 包,并且它是否自动将其版本号增加到比该文件夹中任何解决方案包的最高版本号高一个。

我的解决方案有几个项目,每个项目构建一个 NuGet 包,其中一些依赖于解决方案中的其他项目。

例如,本地包目录中的这些文件:

BaseLibrary.2.1.0.1-betalocal.nupkg
BaseLibrary.2.1.0.1-betalocal.symbols.nupkg
BaseLibrary.Specialized.2.1.0.1-betalocal.nupkg
BaseLibrary.Specialized.2.1.0.1-betalocal.symbols.nupkg

它会选择在下次 CreateLocalPackages 配置干净地创建时创建这些包:

BaseLibrary.2.1.0.2-betalocal.nupkg
BaseLibrary.2.1.0.2-betalocal.symbols.nupkg
BaseLibrary.Specialized.2.1.0.2-betalocal.nupkg
BaseLibrary.Specialized.2.1.0.2-betalocal.symbols.nupkg

除了 Visual Studio 似乎在我可以连接到 MSBuild 目标并将 $(Version) 更改为 [=15 之前创建 BaseLibrary\BaseLibrary.Specialized\obj\project.assets.json 文件这一事实外=],导致 BaseLibrary.Specialized.2.1.0.2-betalocal.nupkg 改为仅引用此依赖项:BaseLibrary.2.1.0-betalocal。 Visual Studio 似乎在调用 MSBuild 构建项目之前执行的 NuGet 还原步骤中生成 project.assets.json

我已经尝试 运行 我的版本号逻辑 BeforeTargets="CollectPackageReferences;_ComputeTargetFrameworkItems" 以便在 Restore 目标链中尽早调用,但它看起来不像 Visual Studio调用 MSBuild /t:Restore 以恢复 NuGet 包引用(并生成 project.assets.json)。不知道是不是有自己的逻辑,还是调用了别的工具

我希望能够告诉 VS 重建并让它找出新版本,构建和打包所有内容,并列出正确的依赖项版本。

How do I set the build-specific version number for a projectReference so that the NuGet package dependency lists the correct version

如果我理解你是对的,你想在 VS/MSBuild Restore 目标之前调用版本号逻辑目标 CreateLocalPackages

您可以添加在目标还原之前运行的 MSBuild 目标或 _GenerateRestoreProjectSpec 通过遵循 MSBuild .*csproj 文件中的代码片段:

  <Target Name="VersionNumberLogicBeforeRestore"
          BeforeTargets="Restore">
    <Message Text="Version Number Logic Before Restore!" Importance="high" />
  </Target>

  <Target Name="VersionNumberLogicBeforeRestore"
          BeforeTargets="_GenerateRestoreProjectSpec">
    <Message Text="Version Number Logic Before Restore!" Importance="high" />
  </Target>

但是,这只适用于 dotnet cli,不适用于 Visual Studio。

dotnet restore "xx.csproj"

此外,NuGet 恢复更改将成为 15.4 的一部分,如果它仍然存在的话。否则是 15.5。

有关详细信息,请参阅 Add support for pre restore MSBuild target that Visual Studio triggers

所以,此时,我们必须使用dotnet cli调用Restore目标之前的版本号逻辑目标,您也可以使用dotnet cli构建项目。