Question

shiftbit on Sun, 27 Oct 2019 12:48:36


Ok, so this is driving me nuts I suspect I will need to know more of msbuild internals.

Have a .net core 3.0 console app and I want to use a publish profile to simply publish it to a folder - EXCEPT for a specific file.

This is a demo project just to test publishing.  The app has 3 appsettings json files.  Im trying to remove one of them BEFORE the files are copied over to the destination.  Or another way to say it is to publish everything EXCEPT this one file (or another if I choose). 

I admit to not being adept at msbuild and I am trying to customize a publish profile, which as I understand it and the csproj file are both simply msbuild scripts, correct?

The 3 json files, appsettings.json, appsettings.Local.json and appsettings.Test.json should be part of any build, but I want to selectively build 2 or more different publish profiles that will EXCLUDE one or more of these json files from the publishdir.

The problem:  All 3 files are getting into the publishDir. 

Questions:

(1) How can I selectively exclude file(s) from being published to the destination
(2) When one creates a publish profile in VS 2019, has the namespace not actually changed since 2003, or has it successively with Visual Studio versions, but the namespace is still the same?
(3) In this example, https://docs.microsoft.com/en-us/visualstudio/msbuild/removedir-task?view=vs-2019 a newer namespace is referenced?  Why is the default publish profile seemingly using an outdated schema?
(4) Where is <ExcludeFilesFromDeployment> in the MSBuild docs? 
(5) Concerning the sequence of events - how does invoking a publish trigger a build first, then publishing the contents? 
(6) Will this only work with web projects?

The csproj file.

<Project Sdk="Microsoft.NET.Sdk">

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="3.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
  </ItemGroup>

  <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>
</Project>

Publish profile

<?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>true</DeleteExistingFiles>
    <ExcludeFilesFromDeployment>appsettings.Test.json</ExcludeFilesFromDeployment>    
  </PropertyGroup>
</Project>

This also does not work as expected, as appsettings.Test.Json still makes it over the the PublishDir

<?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>true</DeleteExistingFiles>
    <!--<ExcludeFilesFromDeployment>appsettings.Test.json</ExcludeFilesFromDeployment>-->    
  </PropertyGroup>

  <Target Name="DeleteFiles">
    <Delete Files="appsettings.Test.json" />
  </Target>
</Project>




Replies

Perry Qian-MSFT on Mon, 28 Oct 2019 09:13:01


Hi shiftbit,

Welcome to the MSDN forum.

l have tried this issue in my side and l found that it worked in  an ASP.Net Web application rather than a Net Core Project.

So the first pubxml file is wrong and you want to use a custom target which will be automatically executed after you specify the target publish to delete this file in your publish folder, you can refer to this:

<?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>true</DeleteExistingFiles>
    <!--<ExcludeFilesFromDeployment>appsettings.Test.json</ExcludeFilesFromDeployment>-->    
  </PropertyGroup>

  <Target Name="DeleteFiles" AfterTargets="Publish">
    <Delete Files="$(PublishDir)appsettings.Test.json" />
  </Target>
</Project>

2.Or if you did not use this fie, you can change its build action to None and change Do not Copy or delete every <CopyToOutputDirectory>Always</CopyToOutputDirectory> in csproj file.

(1) How can I selectively exclude file(s) from being published to the destination

>>You can follow the first way l list to create several custom targets and then give instructions to each target specific file to be removed, depending on the situation.

Remember to remove the AfterTarget instruction and then you can use use the command line -t:xxxx to execute it.

<Target Name="DeleteFiles">
    <Delete Files="appsettings.Test.json,appsettings.json" />
  </Target>



msbuild xxx.csproj -t:Build,Publish -p:publishprofile="xxx.pubxml" -t:DeleteFiles

(2) When one creates a publish profile in VS 2019, has the namespace not actually changed since 2003, or has it successively with Visual Studio versions, but the namespace is still the same?

>>It is still in use today.

3) In this example, https://docs.microsoft.com/en-us/visualstudio/msbuild/removedir-task?view=vs-2019 a newer namespace is referenced?  Why is the default publish profile seemingly using an outdated schema?

>>It still in use today.

(4) Where is <ExcludeFilesFromDeployment> in the MSBuild docs? 

>>You can refer to this article:https://devblogs.microsoft.com/aspnet/web-deployment-excluding-files-and-folders-via-the-web-applications-project-file/

(5) Concerning the sequence of events - how does invoking a publish trigger a build first, then publishing the contents? 

>>you can use this command line:

msbuild xxx.csproj -t:build,publish -p:DeployOnBuild=true -p:PublishProfile="xxx.pubxml"


(6) Will this only work with web projects?

>> Yes, it is used in Publishing Web Application.

Best Regards,

Perry


MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com


Perry Qian-MSFT on Tue, 29 Oct 2019 11:18:09


Hi friend,

Did your issue still persist? If you have any other concern about his, please feel free to let us know. Maybe you cannot use the ExcludeFilesFromDeployment node to pubish your console app. If my answer is helpful for you, please do not forget to mark as answer so that it will help other community members search and handle similar issues.

Thanks for your understanding.

Best Regards,

Perry