如果msbuild 15可以正确识别还原目标,为什么它不能识别压缩包目标?

时间:2018-11-19 01:37:15

标签: msbuild visual-studio-2017 nuget

所以我可以恢复并构建良好:

C:\xyz\MyApp [master ↑1]> git clean -qdfx
C:\xyz\MyApp [master ↑1]> msbuild /t:restore
Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Build started 11/18/2018 8:24:37 PM.
Project "C:\xyz\MyApp\MyApp.sln" on node 1 (Restore target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Restore:
  Restoring packages for C:\xyz\MyApp\MyApp\MyApp.csproj...
  Restoring packages for C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj...
  Restoring packages for C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj...
  Committing restore...
  Committing restore...
  Committing restore...
  Generating MSBuild file C:\xyz\MyApp\MyAppFileTool\obj\MyAppFileTool.csproj.nuget.g.props.
  Generating MSBuild file C:\xyz\MyApp\MyApp\obj\MyApp.csproj.nuget.g.props.
  Generating MSBuild file C:\xyz\MyApp\MyAppHelper\obj\MyAppHelper.csproj.nuget.g.props.
  Generating MSBuild file C:\xyz\MyApp\MyAppFileTool\obj\MyAppFileTool.csproj.nuget.g.targets.
  Generating MSBuild file C:\xyz\MyApp\MyApp\obj\MyApp.csproj.nuget.g.targets.
  Generating MSBuild file C:\xyz\MyApp\MyAppHelper\obj\MyAppHelper.csproj.nuget.g.targets.
  Writing lock file to disk. Path: C:\xyz\MyApp\MyApp\obj\project.assets.json
  Writing lock file to disk. Path: C:\xyz\MyApp\MyAppFileTool\obj\project.assets.json
  Writing lock file to disk. Path: C:\xyz\MyApp\MyAppHelper\obj\project.assets.json
  Restore completed in 367.2 ms for C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj.
  Restore completed in 367.46 ms for C:\xyz\MyApp\MyApp\MyApp.csproj.
  Restore completed in 367.02 ms for C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj.

  NuGet Config files used:
      C:\Users\myself\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Done Building Project "C:\xyz\MyApp\MyApp.sln" (Restore target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.01
C:\xyz\MyApp [master ↑1]> msbuild /m /v:m
Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  MyAppHelper -> C:\xyz\MyApp\MyAppHelper\bin\Debug\MyAppHelper.dll
  MyApp -> C:\xyz\MyApp\MyApp\bin\Debug\MyApp.exe
  MyAppFileTool -> C:\xyz\MyApp\MyAppFileTool\bin\Debug\MyAppFileTool.exe

因此,我希望打包目标也可以工作,但不能:

C:\xyz\MyApp [master ↑1]> msbuild /t:pack
Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
Build started 11/18/2018 8:24:55 PM.
Project "C:\xyz\MyApp\MyApp.sln" on node 1 (pack target(s)).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Project "C:\xyz\MyApp\MyApp.sln" (1) is building "C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj" (2) on node 1 (pack target(s)).
C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj : error MSB4057: The target "pack" does not exist in the project.
Done Building Project "C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj" (pack target(s)) -- FAILED.

Project "C:\xyz\MyApp\MyApp.sln" (1) is building "C:\xyz\MyApp\MyApp\MyApp.csproj" (3) on node 1 (pack target(s)).
C:\xyz\MyApp\MyApp\MyApp.csproj : error MSB4057: The target "pack" does not exist in the project.
Done Building Project "C:\xyz\MyApp\MyApp\MyApp.csproj" (pack target(s)) -- FAILED.

Project "C:\xyz\MyApp\MyApp.sln" (1) is building "C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj" (4) on node 1 (pack target(s)).
C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj : error MSB4057: The target "pack" does not exist in the project.
Done Building Project "C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj" (pack target(s)) -- FAILED.

Done Building Project "C:\xyz\MyApp\MyApp.sln" (pack target(s)) -- FAILED.


Build FAILED.

"C:\xyz\MyApp\MyApp.sln" (pack target) (1) ->
"C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj" (pack target) (2) ->
  C:\xyz\MyApp\MyAppFileTool\MyAppFileTool.csproj : error MSB4057: The target "pack" does not exist in the project.


"C:\xyz\MyApp\MyApp.sln" (pack target) (1) ->
"C:\xyz\MyApp\MyApp\MyApp.csproj" (pack target) (3) ->
  C:\xyz\MyApp\MyApp\MyApp.csproj : error MSB4057: The target "pack" does not exist in the project.


"C:\xyz\MyApp\MyApp.sln" (pack target) (1) ->
"C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj" (pack target) (4) ->
  C:\xyz\MyApp\MyAppHelper\MyAppHelper.csproj : error MSB4057: The target "pack" does not exist in the project.

    0 Warning(s)
    3 Error(s)

Time Elapsed 00:00:00.27
C:\xyz\MyApp [master ↑1]>

怎么了?

编辑1

我刚刚创建了一个新的.NET Standard项目,并且restorepack都可以正常工作。有问题的解决方案是.NET Framework 4.7.2解决方案,我在Visual Studio 2017的帮助下将其转换为使用PackageReference。我没有手动执行任何操作。

1 个答案:

答案 0 :(得分:0)

我绝不是MSBuild专家,但我相信这比说NuGet与MSBuild集成要复杂得多。我不相信MSBuild 15具有NuGet的内置知识,甚至还没有还原目标,可以通过创建一个空的MSBuild项目文件<Project></Project>来确认。运行msbuild /t:restore,然后看到错误消息说没有目标还原。

因此,还原目标实际上并未内置在msbuild.exe中,但实际上是在项目正在使用的目标文件中定义的。因此,为了定义包目标,您需要导入其他目标文件。

从较不理论,更实际的角度来看,“旧”样式的csproj文件(导入Microsoft.Common.CSharp.targets或类似名称的文件)没有定义压缩包目标,而是新的SDK样式项目。

因此,为了能够从msbuild打包,您需要迁移到SDK样式的项目。有人误以为您需要将.NET Core定位为使用SDK风格的项目,这是不正确的。 SDK风格的项目与.NET Core同时可用,.NET Core项目需要SDK风格的项目,但是您仍然可以定位.NET Framework。