首页 > 解决方案 > 如果使用条件目标框架,Nuget 会为错误的目标框架恢复包

问题描述

我有一个解决方案,它通过选择相应的配置来构建 .NetFramework 3.5 或 .NetFramework 4.5.2 库。我正在尝试将其迁移到 .NET5,但我也想保留构建 .NetFramework 库的可能性。因为该解决方案包含一些不支持多目标的 CLI/C++ 项目,所以我决定使用条件 TargetFrameworks:

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Net5' Or '$(Configuration)' == 'Release_Net5' ">
    <TargetFramework>net5.0-windows</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug_Net4' Or '$(Configuration)' == 'Release_Net4' ">
    <TargetFramework>net452</TargetFramework>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' == 'Debug' Or '$(Configuration)' == 'Release' ">
    <TargetFramework>net35</TargetFramework>

该解决方案由 Visual Studio 成功构建。但是,如果我尝试将 MSBuild 用于构建前言,Nuget restore请致电:

"nuget.exe" restore <mySolutionPath> -Verbosity detailed
msbuild <mySolutionPath> /p:Configuration=Debug_Net4;Platform=x86

我收到错误:

C:\Program Files\dotnet\sdk\5.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1005: Assets file '<projectpath>\obj\project.assets.json' doesn'
t have a target for 'net452'. Ensure that restore has run and that you have included 'net452' in the TargetFrameworks for your project.

根据nuget restore输出和资产文件内容,仅对 .NetFramework 3.5 进行了恢复。有什么方法可以指定 targetFrameworknuget restore吗?

标签: .net-coremsbuildnugetnuget-package-restore

解决方案


推荐阅读