Visual studio 2017 Excludefoldersfromdeployment but include some subfolder

915 views Asked by At

I was wondering if it's possible to include some sub-folder on publication, of an excluded folder. Let's assume i have this structure:

/Folder1/
/Folder1/FileX
/Folder1/FileY
/Folder1/SubFolder1/
/Folder1/SubFolder2/

What i want is to exclude the whole Folder1 content, but include only a specific set of sub-folder (in my example SubFolder1).

The Folder1 folder is excluded with the ExcludeFoldersFromDeployment in the .pubxml:

<ExcludeFoldersFromDeployment>Folder1</ExcludeFoldersFromDeployment>
2

There are 2 answers

0
C.J. On

You haven't told us what this .pubxml file is, so there is a limit to what we can help you with.

But in general:

The construct that deals with files and folders in MSBuild is Items. You want an Item here, not an MSBuild property.

So you could easily use an item to point to a specific sub folder in your build environment like this:

<ItemGroup>
  <!-- This will grab all files in SubFolder1 but not recursively -->
  <DeployThese Include="/Folder1/SubFolder1/*.*" />
</ItemGroup>

You can then do anything you want with that Item. You could copy the files in it somewhere else, or anything else with them.

The files are accessed later by using @(DeployThese)

0
LoLance On

I was wondering if it's possible to include some sub-folder on publication, of an excluded folder.

Yes, it’s possible.

Note: One point we should know, as you’ve used “ExcludeFoldersFromDeployment” element in .pubxml to exclude the entire Folder1 directory. Some deploy actions in .xxproj file may be overwritten or affected by it.

So, to achieve your goal we have to follow two steps: 1. Copy the SubFolder to a new folder (A new folder in $(ProjectDir) can be better) 2. Add the content of SubFolder to publish, and choose the structure you want

Here’s a workaround:

1: Add a PreBuildEvent prorerty in .csproj file.

  <PropertyGroup>
    <PreBuildEvent>xcopy "$(ProjectDir)/Folder1/SubFolder1" "$(ProjectDir)/NewFolder" /E /Y /I</PreBuildEvent>
  </PropertyGroup>

2: Add following markup to .csproj file, it helps publish extra files to publish folder in Asp.net:

<PropertyGroup>
    <PipelineCollectFilesPhaseDependsOn>
      CustomCollectFiles;
      $(PipelineCollectFilesPhaseDependsOn);
    </PipelineCollectFilesPhaseDependsOn>
  </PropertyGroup>

  <Target Name="CustomCollectFiles">
    <ItemGroup>
      <_CustomFiles Include="NewFolder\**\*" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>MyStructureUnderPublishFolder\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

For this markup, we only need to change two paths to meet our needs.

First:The CustomFiles refers to the folder whose content will be published.

Second:The MyStructureUnderPublishFolder refers to the structure you want under publish folder. If you want a Folder1 which only has a SubFolder in it after deployment, change it to Folder1/SubFolder1, or change it to SubFolder1 if you want a simple Subfolder1 under Publish folder.

More information about adding extra files to publish see here.