Excluding a folder from deployment and stopping additional file removal

Solution 1:

The following is my CustomProfile.pubxml file that I use to leave my .well-known folder for LetsEncrypt, as well as other folders alone. Add the items below in bold to exclude processing files on the server, such as user generated content. This has only been tested on Visual Studio 2017, with Server 2016.

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. 
You can customize the behavior of this process by editing this MSBuild file.
In order to learn more about this please visit 
https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developermsbuild/2003">

<PropertyGroup>
  <WebPublishMethod>MSDeploy</WebPublishMethod>
</PropertyGroup>

<PropertyGroup>
  <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
  <LastUsedPlatform>Any CPU</LastUsedPlatform>
  <SiteUrlToLaunchAfterPublish>https://www.vinceworks.com</SiteUrlToLaunchAfterPublish>
  <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
  <ExcludeApp_Data>True</ExcludeApp_Data>
  <MSDeployServiceURL>https://www.vinceworks.com</MSDeployServiceURL>
  <DeployIisAppPath>VinceWorks</DeployIisAppPath>
  <RemoteSitePhysicalPath />
  <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
  <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
  <EnableMSDeployBackup>True</EnableMSDeployBackup>
  <UserName>Vince</UserName>
  <_SavePWD>True</_SavePWD>
  <PrecompileBeforePublish>True</PrecompileBeforePublish>
  <EnableUpdateable>True</EnableUpdateable>
  <DebugSymbols>False</DebugSymbols>
  <WDPMergeOption>DonotMerge</WDPMergeOption>
</PropertyGroup>

<ItemGroup>
  <MsDeploySkipRules Include="CustomSkipFolder">
    <ObjectName>dirPath</ObjectName>
    <AbsolutePath>VinceWorks\\\.well-known</AbsolutePath><!--Regular Expression here-->
  </MsDeploySkipRules>
</ItemGroup>

<ItemGroup>
  <MsDeploySkipRules Include="CustomSkipFolder">
    <ObjectName>dirPath</ObjectName>
    <AbsolutePath>VinceWorks\\Media</AbsolutePath>
  </MsDeploySkipRules>
</ItemGroup>

<ItemGroup>
  <MsDeploySkipRules Include="CustomSkipFolder">
    <ObjectName>dirPath</ObjectName>
    <AbsolutePath>\\Views</AbsolutePath>
  </MsDeploySkipRules>
</ItemGroup>

</Project>

Solution 2:

Something like this will do it:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AfterAddIisSettingAndFileContentsToSourceManifest>AddCustomSkipRules</AfterAddIisSettingAndFileContentsToSourceManifest>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <LastUsedBuildConfiguration>Local</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <MSDeployServiceURL>localhost</MSDeployServiceURL>
    <DeployIisAppPath>AppPath</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>False</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>InProc</MSDeployPublishMethod>
    <EnableMSDeployBackup>False</EnableMSDeployBackup>
    <UserName />
    <_SavePWD>False</_SavePWD>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
  </PropertyGroup>

  <PropertyGroup>
    <UseMsDeployExe>true</UseMsDeployExe>
  </PropertyGroup>

  <Target Name="AddCustomSkipRules">
    <Message Text="Adding Custom Skip Rules" />
    <ItemGroup>
      <MsDeploySkipRules Include="SkipFilesFolder">
        <SkipAction>Delete</SkipAction>
        <ObjectName>filePath</ObjectName>
        <AbsolutePath>YourFolderNameHere</AbsolutePath>
      </MsDeploySkipRules>
    </ItemGroup>
  </Target>

</Project>

I have a detailed post here:

Using MsDeploy publish profile .pubxml to create an empty folder structure on IIS and skip deleting it with MsDeploySkipRules