Include extra files in WebDeploy Package

There are many examples on the web which provide example MSBuild script to add extra files (not referenced by the project) into a WebDeploy package.  Chief among them is Sayed Hashimi’s blog post detailing how it all works.  By injecting into the WebDeploy packaging targets you can add additional files into the files to be packaged.

<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForPackageDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
  <ItemGroup>
    <_CustomFiles Include="..\Extra Files\**\*" />
    <FilesForPackagingFromProject  Include="%(_CustomFiles.Identity)">
      <DestinationRelativePath>Extra Files\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

These all appear to be older posts however.  I have no doubt this worked when Sayed and others blogged it, but this doesn’t appear to work with the latest VS bits.

The Solution

The parent target which eventually triggers the CopyAllFilesToSingleFolderForPackageDependsOn target does not appear to be used any longer.  I found where it was referenced back in the VS2010 release of these target but it doesn’t look like its been used since then.  The new (very similar) target that appears to have replaced it is the CopyAllFilesToSingleFolderForMSDeployDependsOn target. Simply changing “Package” to “MSDeploy” should fix this issue.

<PropertyGroup>
  <CopyAllFilesToSingleFolderForMSDeployDependsOn>
    CustomCollectFiles;
    $(CopyAllFilesToSingleFolderForMSDeployDependsOn);
  </CopyAllFilesToSingleFolderForMSDeployDependsOn>
</PropertyGroup>
<Target Name="CustomCollectFiles">
  <ItemGroup>
    <_CustomFiles Include="..\Extra Files\**\*" />
    <FilesForPackagingFromProject  Include="%(_CustomFiles.Identity)">
      <DestinationRelativePath>Extra Files\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

Obviously, the same change applies to removing files from a package.

If this solves your issue or you have further questions, please comment below or continue the conversation on Twitter/Reddit.

13 thoughts on “Include extra files in WebDeploy Package”

  1. Thanks for posting this – very subtle and trivial, but caused something to break which would have been rather hard to have diagnosed without this pointer. I knew the issue would reside in the csproj – but where!

  2. Was looking for a way to copy files from another location into the publish package and found this. This was awesome, used in VS2013 and works great.

  3. Do you by any chance know how to delete folder on target prior to publish? I think RemoveDir works only on package files, not on destination. I have one folder that I copy using above method but it only works first time – next time it ignores it instead of updating it. I am using checksum method, not date (for determining which files should be updated).
    And where can I find schema syntax for MsDeploy? There are obviously many tags that can be used in pubxml file, but I couldn’t find solid reference.

  4. So, while this does work when using Visual Studio and publishing from a workstation, but on a build server, it doesn’t. The build agent seems to ignore both of those parameters and therefore nothing gets deployed that’s not included in the project.

  5. Pingback: Customizing Publish Profiles to AzureCheckout multiple git repos into same Jenkins workspaceDeploy Azure Website via MSBuild & WebDeploy, but with which credentials?Publish Azure Application with MSBuildVisual Studio 2013 Azure Publish profile same Az

Leave a Reply