VS2017:无法使用MSBuild还原NuGet软件包 [英] Vs2017: NuGet packages not restored with MSBuild
问题描述
- 我从VS2017构建并下载了软件包和项目 构建.
- 然后我核对packages文件夹.
- 然后我使用MSBuild构建项目失败,因为软件包没有 存在,它们还没有恢复.
- I build from VS2017 and the packages are downloaded and the projects build.
- Then I nuke the packages folder.
- Then I build using MSBuild the projects fail because the packages don't exist, they haven't been restored.
我在
上有下载缺少的软件包"
I have "download missing packages" on
项目文件原本是在VS 2013中创建的
The project files would have been originally created in VS 2013
使用MSBuild任务来构建解决方案的build.proj.
The build.proj using the MSBuild task to build the solution.
<MSBuild Projects ="$(root)\src\MySolution.sln" ContinueOnError ="false" Properties="Configuration=$(Configuration)">
<Output ItemName="OutputFiles" TaskParameter="TargetOutputs"/>
</MSBuild>
我的Google-fu让我失败了,所有想法都将得到很好的认可.
My google-fu has failed me, any and all ideas will be greatfully received.
推荐答案
packages.config
的项目需要通过NuGet而不是MSBuild进行还原.通过VS请求生成时,Visual Studio会执行此操作.如果需要从命令行执行此操作,则需要使用命令行nuget.exe
实用程序来还原软件包.
packages.config
based projects need to be restored via NuGet, not MSBuild. Visual Studio does this when requesting a build via VS. If you need to do it from command line, you need to use the command line nuget.exe
utility to restore the packages.
只有PackageReference
引用NuGet程序包的方式集成到了MSBuild中.您可以通过从项目中卸载所有NuGet软件包并更改默认的软件包管理格式或选择在首次安装软件包时允许选择格式"复选框来切换到此格式.然后,您可以重新添加软件包(仅需要顶级软件包),并确保它不会生成packages.config
文件.
Only the PackageReference
way of referencing NuGet packages is integrated into MSBuild. You can switch to this format by uninstalling all NuGet packages from the project, and changing the default package management format or select the "Allow format selection on first package install" checkbox. Then you can re-add the packages (only top-level packages are needed) and make sure it does not generate a packages.config
file.
这里有几个问题:
-
还原后,MSBuild必须重新评估项目文件.在CI脚本中使用
<MSBuild>
任务时,仅当将一组不同的属性传递给任务时,才会发生这种情况.这是因为还原会在obj\
目录中生成自动导入的文件,这些文件会在评估过程中进行检查.
After a restore, MSBuild has to re-evaluate the project file. When using the
<MSBuild>
task inside a CI script, this only happens when a different set of properties are passed to the task. This is because the Restore generates auto-imported files in theobj\
directory that are checked for during the evaluation.
如果还原不会生成,而是更改这些文件,则这还不够,因此必须使用干净的xml缓存重新评估项目.对于<MSBuild>
任务,这是不可能的. MSBuild 15.5引入了/restore
选项来执行Restore
目标,然后清除这些缓存(之前没有API)并按要求执行其余构建.在15.5之前,需要分别调用msbuild.exe
两次,以确保增量restore +构建产生正确的输出.
Should the restore not generate, but alter these files, this is not enough and the project has to be re-evaluated with clean xml caches. This is not possible with the <MSBuild>
task. MSBuild 15.5 has introduces a /restore
option to execute a Restore
target, then clean these caches (for which there was no API before) and execute the rest of the build as requested. Before 15.5, two separate invocations of msbuild.exe
were needed to ensure that incremental restore+builds produce the correct output.
如果要将CI构建项目与PackageReference
一起使用,最安全的方法是创建具有还原和构建目标的build.proj
:
If you want to use a CI build project with PackageReference
, the safest way is to create a build.proj
that has a restore and a build target:
<Project>
<Target Name="Restore">
<MSBuild Projects="$(root)\src\MySolution.sln" Targets="Restore" />
</Target>
<Target Name="Build">
<MSBuild Projects="$(root)\src\MySolution.sln" Targets="Build" />
</Target>
</Project>
并使用
msbuild /restore /t:Build build.proj
这篇关于VS2017:无法使用MSBuild还原NuGet软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!