MSBuild还原目标 - MSB4057:项目中不存在“还原”目标

17
我们的主要产品组合中有超过20个解决方案(超过880个项目),我们拥有一套复杂的构建脚本,它们运行良好,但我们正在尝试在msbuild管道内自动恢复nuget程序包。目前,这是通过手动调用nuget来恢复包完成的。
根据https://learn.microsoft.com/en-us/nuget/schema/msbuild-targets,我应该能够运行此命令并运行还原:
# works
& $script:msBuildPath $solutionName /t:build /v:q /clp:ErrorsOnly /nologo /p:...
# doesn't works
& $script:msBuildPath $solutionName /t:restore,build /v:q /clp:ErrorsOnly /nologo /p:...

然而,当我添加 restore 到上面的代码时,它会抛出一个错误。
MSB4057: The target "restore" does not exist in the project

我应该从哪里开始研究以理解为什么它找不到这个目标? 我们主要使用的是VS 2015和.NET 4.6.2,因此目前无法使用任何特定于VS2017的选项。


如果我省略/v:q/clp:ErrorsOnly标志,我会得到这个(稍微经过处理的解决方案/项目名称和路径)
PS Z:\git\company> build c
building Common\Common.sln
Build started 11/15/2017 11:08:54 AM.
     1>Project "Z:\git\company\Common\Common.sln" on node 1 (restore;build target(s)).
     1>ValidateSolutionConfiguration:
         Building solution configuration "DEBUG|Any CPU".
     1>Z:\git\company\Common\Common.sln.metaproj : error MSB4057: The target "restore" does not exist in the project. [Z:\git\company\Common\Common.sln]
     1>Done Building Project "Z:\git\company\Common\Common.sln" (restore;build target(s)) -- FAILED.

Build FAILED.

       "Z:\git\company\Common\Common.sln" (restore;build target) (1) ->
         Z:\git\company\Common\Common.sln.metaproj : error MSB4057: The target "restore" does not exist in the project. [Z:\git\company\Common\Common.sln]

    0 Warning(s)
    1 Error(s)

Time Elapsed 00:00:00.03
runBuildPackage :  ... failed
At Z:\git\company\psincludes\buildFunctions.ps1:11 char:5
+     runBuildPackage "Common\Common.sln"
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,runBuildPackage


msbuild failed
At Z:\git\company\psincludes\buildInternals.ps1:63 char:21
+                     throw "msbuild failed";
+                     ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (msbuild failed:String) [], RuntimeException
    + FullyQualifiedErrorId : msbuild failed

我知道这其中有些是我们内部工具的问题,只是我觉得没有必要遮掩这一点。

根据“互联网”的说法,这应该只是构建过程中的“免费”部分,我不确定我们在这里的csproj文件中缺少了什么。

1个回答

21

在使用MSBuild 15和VS 2017中,NuGet 4.0+提供了msbuild集成的功能。然而,对于使用VS 2015的用户来说,并不支持还原目标。

仅当项目使用新的PackageReference引用NuGet包时,VS 2017 / NuGet 4中的集成还原才能正常工作。如果项目使用packages.config,则无法使用该功能。这种新的包引用方式是ASP.NET Core(包括.NET Framework / .NET Core)、.NET Core和.NET Standard项目的默认选项。对于所有其他项目类型,可以在NuGet属性中选择样式以进行选择(Tools->Options->NuGet)。

请注意,调用/t:Restore,Build不是调用此目标的好方法,因为还原可能会生成或更改文件,然后msbuild就无法重新加载。MSBuild 15.5(即将推出的更新版本)引入了一个/restore选项,它将调用还原目标,然后清除之前无法清除的所有缓存,并按请求执行正常构建。在15.5之前,最好分别调用两个msbuild。


1
所以确认一下,使用VS2015恢复功能的唯一方法是通过手动运行nuget restore吗?如果你觉得这很明显,我很抱歉,但我还是有点困惑,所以想确保我没问题。再次感谢! - jcolebrand
3
明白了。(考虑到当前文档的状态,我完全理解你的困惑) - Martin Ullrich
因为有/restore,所以我加了+1。有这么多的更改和选项...我不得不尝试每一种组合才能让我的项目在服务器上构建并创建我想要的包。 - Lucas Locatelli
3
推荐使用 msbuild -t:build -restore 命令,参见 Restoring and building with one MSBuild command - Dave Anderson
请注意,如果使用packages.config文件指定包,则需要使用msbuild -restore -p:RestorePackagesConfig=true。 (如前面评论中链接的文档页面中所述。)如果需要,这些参数可以添加到名为“Directory.Build.rsp”的文件中-请参见:https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-response-files - Otto G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接