MSBuild nuget RestoreOutputPath 如何让它工作?

标签 msbuild nuget .net-core .net-core-2.0

新的 msbuild csproj 格式已经集成了 nuget 命令。可以使用 <RestoreOutputPath>obj\profile7</RestoreOutputPath> 更改恢复项目 Assets 的默认路径。项目文件中的命令。

但如果我添加 <RestoreOutputPath>obj\profile7</RestoreOutputPath>到 csproj 文件后续命令

dotnet restore myproj.sln
dotnet build myproj.sln

产生构建错误

obj\project.assets.json' not found. Run a NuGet package restore to generate this file.

如何告诉 MSBuild 从此 obj\Profile7 获取 nuget Assets 构建命令中的路径?

最佳答案

还原输出路径需要与MSBuildProjectExtensionsPath相同这样 nuget 生成的 props 和 targets 文件将被通用的 props 和 targets 导入。以及BaseIntermediateOutputPath将默认构成到 ProjectAssetsFile 的路径.

至少对于 NuGet 导入,重要的是 MSBuildProjectExtensionsPathBaseIntermediateOutputPath在导入 SDK props 文件之前设置。

解决所有这些问题的最简单方法是设置 BaseIntermediateOutputPath在项目的早期,所有组件都将其值作为默认基本路径 - 这实际上是重定向 obj到别的地方。

这与 <Project SDK="..."> 冲突语法,因为无法在 SDK 的 props 文件之前设置属性。要解决此问题,可以像这样更改项目:

<Project>
  <!-- This needs to be set before Sdk.props -->
  <PropertyGroup>
    <BaseIntermediateOutputPath>obj\SomeSubDir\</BaseIntermediateOutputPath>
  </PropertyGroup>

  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <!-- other content -->

  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

另一种方法是创建一个 Directory.Build.props足够早地自动导入的文件,但这会将值应用于目录中的所有项目,并剥夺了为每个项目指定值的能力。

关于MSBuild nuget RestoreOutputPath 如何让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45575280/

相关文章:

Nuget Update-Package 无法识别已安装的包 --> 更新失败

c# - 将 'Xamarin.Forms' 添加到...未将对象引用设置为对象的实例。添加 nuget 包时出现错误

c# - 使用 Microsoft 登录时出现 HttpListenerException

.net-core - dotnet 控制台应用程序,使用通用主机、HostedService、Windows 任务计划程序保持运行状态

c# - 无法解析对 Windows 运行时类型“Windows.Foundation.Metadata.PlatformAttribute”的依赖

c# - 引用 Microsoft.Build 和 System.IO.Compression 时出现 MissingMethodException

.net - 什么时候copy-local应该设置为true,什么时候不应该设置?

iis - 无法从自托管的 nuget 服务器下载包(404 错误)

c# - Identity Core 与 Entity Framework 6

c# - Microsoft.Build.Engine 错误(默认目标): Target GetFrameworkPaths: Could not locate the . NET Framework SDK