【问题标题】:MSBuild restore target - MSB4057: The target "restore" does not exist in the projectMSBuild 还原目标 - MSB4057:项目中不存在目标“还原”
【发布时间】:2018-04-29 00:57:27
【问题描述】:

我们的主要产品组合中有 20 多个解决方案(超过 880 个项目),并且我们有一组运行良好的复杂构建脚本,但我们正在尝试从 msbuild 管道中自动恢复 nuget 包。目前,这是通过手动调用 nuget 来恢复包来完成的。

根据https://docs.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 文件中缺少什么。

【问题讨论】:

    标签: c# msbuild nuget-package-restore .net-4.6.2 msbuild-target


    【解决方案1】:

    msbuild 集成的 NuGet 功能在 NuGet 4.0+ 和 MSBuild 15 中可用,这意味着仅在 VS 2017 中可用。不支持 VS 2015 的还原目标。

    VS 2017 / NuGet 4 中的集成还原仅适用于使用新的 PackageReference 引用 NuGet 包样式的项目。它不适用于使用 packages.config 的项目。这种引用包的新方式是 ASP.NET Core(在 .NET Framework / .NET Core 上)、.NET Core 和 .NET Standard 项目的默认选项。通过在 NuGet 属性 (Tools->Options->NuGet) 中选择第一次安装之前的样式,可以选择所有其他项目类型。

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

    【讨论】:

    • 所以要确认,使用 VS2015 获得恢复功能的唯一方法是通过 nuget restore 手动运行它?对不起,如果你觉得这很明显,但我还是有点模糊,所以想确保我是坚实的。再次感谢!
    • 完全正确。 (考虑到文档的当前状态,我完全理解您的困惑)
    • +1 因为 /restore。如此多的变化和如此多的选择......不得不为我的项目尝试单一组合以在服务器中构建并按照我​​想要的方式创建包。
    • 推荐msbuild -t:build -restore,见Restoring and building with one MSBuild command
    • 请注意,如果使用 packages.config 文件指定包,则需要 msbuild -restore -p:RestorePackagesConfig=true。 (如上一条评论链接的文档页面中所述。)如果需要,可以将这些参数添加到名为“Directory.Build.rsp”的文件中 - 请参阅:docs.microsoft.com/en-us/visualstudio/msbuild/…
    猜你喜欢
    • 1970-01-01
    • 2018-07-31
    • 2022-08-11
    • 2017-11-14
    • 2015-11-11
    • 2016-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多