【问题标题】:How can I get MSBuild to restore any needed NuGet packages如何让 MSBuild 恢复任何需要的 NuGet 包
【发布时间】:2021-04-06 07:15:30
【问题描述】:

我查看了大量关于此的文章,包括 herehere。没有人提供我可以开始工作的解决方案。

我已经打开了在 VisualStudio 中自动执行此操作的所有功能。但是在我们的构建机器上,我们使用 MSBuild 构建 .sln 文件,而不是通过打开 VisualStudio。那么在 MSBuild 中,我怎样才能最好地做到这一点呢?

我们所有的 .sln 文件都适用于 VisualStudio 2019,并在处理 NuGet 时设置为默认值(我相信)。

【问题讨论】:

  • 根据您需要nuget.exe restoremsbuild /t:restore 的实际项目类型。已被广泛讨论。
  • @LexLi 我试过“msbuild myfile.proj /t:restore”,它立即完成,什么也不做。而且我找不到可以从哪里下载 nuget.exe。
  • nuget.org/downloads 你可以通过阅读它的 bin 日志来了解为什么 MSBuild 不能工作,msbuildlog.com
  • @DavidThielen,有关于这个问题的最新消息吗?
  • @PerryQian-MSFT 我最终做了“nuget restore filename.sln”

标签: visual-studio msbuild nuget nuget-package-restore


【解决方案1】:

msbuild -t:Restore 将为具有PackageReference nuget management format 的项目恢复 nuget 包。

你的情况看起来像packages.config nuget management format,你用过。

如果是这样,您应该只使用nuget restore 来通过 msbuild.exe 恢复 nuget 包。

1) 首先从网站下载nuget.exe cli 并进行配置。您可以查看this about it

2)然后使用MSBuild命令行,输入

nuget xxx\xxx.sln restore
msbuild xxx\xxx.sln -t:build

另一个是使用 msbuild -t:restore 和-p:RestorePackagesConfig=true

msbuild xxx\xxx.sln -t:restore,build -p:RestorePackagesConfig=true

【讨论】:

  • 其实是nuget restore xxx\xxx.sln
  • 注意这篇 MS 文章明确解释了不要像你在这里使用 -t:restore,build 而是使用 -t:build -restore - 给出的原因是:Due to the fact that NuGet can restore packages that bring down MSBuild targets and props, the restore and build evaluations are run with different global properties. This means that the following will have an unpredictable and often incorrect behavior. docs.microsoft.com/en-us/nuget/reference/…
猜你喜欢
  • 2021-11-07
  • 2017-11-26
  • 2014-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多