代码之家  ›  专栏  ›  技术社区  ›  jcolebrand

MSBuild restore target-MSB4057:项目中不存在目标“restore”

  •  16
  • jcolebrand  · 技术社区  · 8 年前

    我们的主要产品组合中有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文件中遗漏了什么。

    1 回复  |  直到 8 年前
        1
  •  19
  •   Martin Ullrich    8 年前

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

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

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

    推荐文章