msbuild task that works from publish profile to remove specific file

Viewed 1523

Have 3 json files in my project. For a specific publish profile, using a FileSystem publish and I'm trying to find a way to have a specific file NOT be published to the target folder. This is for a .net core console app.

This is my publish profile, but the file Im trying to restrict from being published is still published to the target folder.

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PublishProtocol>FileSystem</PublishProtocol>
    <Configuration>Debug</Configuration>
    <Platform>Any CPU</Platform>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PublishDir>D:\projects\test_publish</PublishDir>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>false</SelfContained>
    <DeleteExistingFiles>false</DeleteExistingFiles>
    <ExcludeFilesFromDeployment>appsettings.Test.json</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>

Note: project file includes these files so they will be in the build output. Might this prevent the appsettings.Test.json from being deleted and hence still get copied over to the publish dir?

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Local.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Test.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>
2 Answers

The work-around solution I have come up with is similar to another project I have. It involves taping into the build system and add some extra bits into the csproj file.

So the csproj file would now look like this. It eliminates the file you dont need, and you only need to add a corresponding build config entry and this uses the VS build system to determine which of these json file(s) get output.

  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Local.json" Condition=" '$(Configuration)' == 'Debug' " >
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Test.json" Condition=" '$(Configuration)' == 'Test' " >
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

This is my publish profile, but the file Im trying to restrict from being published is still published to the target folder.

I test it in my local machine and reproduce same issue. Also I find if I use this element in web application like asp.net, it always works. So I assume this element is not supported for publishing .net core console application.

What role does that file play in your project?

1.If your application don't need it in build output directory and publish directory, you can set the Copy to Output Directory to Do not copy.

And then clean your publish folder and publish again to check if it helps.

enter image description here

2.And if you have specific reason that you need the file in build, but need to exclude it from publish folder, trying adding this script into your xx.csproj(Delete Task):

  <Target Name="CustomTarget" AfterTargets="_PublishNoBuildAlternative">
    <Delete Files="$(PublishDir)\appsettings.Test.json"/>
    <!--<Delete Files="$(PublishDir)\appsettings.xxx.json"/>-->
  </Target>

You can add MSbuild Conditions like Condition="$(Configuration)=='xxx'" to the targets to make it more flexible.

Related