How to Prevent Coping for a Specific File from Reference Projects

If you prevent coping for appsettings.json files from reference projects, you can do it with following custom targets.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  
  <ItemGroup>
    <ProjectReference Include="..\ReferenceProjectA\ReferenceProjectA.csproj" />
    <ProjectReference Include="..\ReferenceProjectB\ReferenceProjectB.csproj" />
  </ItemGroup>

  <!-- for build (Debug/Release) -->
  <Target Name="RemoveAppsettingsJsonForBuild" BeforeTargets="_CopyOutOfDateSourceItemsToOutputDirectory">
    <ItemGroup>
      <_SourceItemsToCopyToOutputDirectory Remove="@(_SourceItemsToCopyToOutputDirectory->'%(Identity)')" Condition="'%(Filename)%(Extension)' == 'appsettings.json'" />
    </ItemGroup>
  </Target>

  <!-- for publish -->
  <Target Name="RemoveAppsettingsJsonForPublish" AfterTargets="ComputeResolvedFilesToPublishList">
    <ItemGroup>
      <ResolvedFileToPublish Remove="@(ResolvedFileToPublish->'%(Identity)')" Condition="'%(Filename)%(Extension)' == 'appsettings.json'" />
    </ItemGroup>
  </Target>

</Project>