DotnetCore 项目 IntermediateOutputPath 在解决方案目录中留下了一些文件

DotnetCore project IntermediateOutputPath leaves some files in the solution directory

为了保持存储库干净并与输出和构建文件分开,我们更改了项目文件中的相应路径。

对于 Net Framework 项目,指定 IntermediateOutputPath 会将 obj 目录重定向到相应的文件夹。

对于 Net Core 项目 (3.0) 使用此 属性 是不够的。尽管 Debug、Release 文件夹确实被重定向了,但 obj 文件夹仍然被创建并且它包含一些文件 - 例如 project.assets.json,.csproj.nuget.cache,.csproj.nuget.dgspec.json,.csproj.nuget.g.props,.csproj.nuget.g.targets。 使用 BaseIntermediateOutputPath - 也无济于事。

只是想知道是否有人可以建议如何移动整个 obj 目录? 谢谢

Martin 建议的解决方案适用于 Net Core 项目

<Project>
  <PropertyGroup>
    <BuildDIrectory>C:\Temp\Build$(Configuration)</BuildDIrectory>
    <RelativePath>some arelative path which depends on location of corresponding project withing the solution</RelativePath>

    <BaseIntermediateOutputPath>$(BuildDIrectory)\obj$(RelativePath)$(AssemblyName)\</BaseIntermediateOutputPath>
    <OutputPath>$(BuildDIrectory)\out$(RelativePath)$(AssemblyName)\</OutputPath>
    <DocumentationFile>$(BuildDIrectory)\Documentation$(RelativePath)$(AssemblyName).xml</DocumentationFile>
  </PropertyGroup>

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

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

  ...
  </Project>

BaseIntermediateOutputPath也可以,但需要尽早设置才能生效。

最简单的方法是将其添加到 Directory.Build.props 文件中:

<Project>
  <PropertyGroup>
    <BaseIntermediateOutputPath>$(MSBuildThisFileDirectory)..\shared-obj$(MSBuildProjectName)\</BaseIntermediateOutputPath>
  </PropertyGroup>
</Project>

如果您想直接在csproj 文件中指定它,则不能使用<Project Sdk=" 表示法,因为需要在应用部分SDK 之前设置属性。但是,它在使用显式 SDK 导入和正确排序时有效:

<Project>

  <PropertyGroup>
    <BaseIntermediateOutputPath>..\shared-obj\myprojA\</BaseIntermediateOutputPath>
  </PropertyGroup>

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <OutputType>Exe</OutputType>
  </PropertyGroup>

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

</Project>