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.
thanks for this – worked perfectly in vs 2015
Glad it worked for you. Thanks for letting me know!
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!
Glad it was helpful. Thanks for sharing!
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.
Glad it helped. Thanks for sharing!
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.
You can extend the deployment package and use the RunCommand provider to run a batch command to delete what you need.
https://technet.microsoft.com/en-us/library/ee619740(v=ws.10).aspx
https://www.dotnetcatch.com/2016/05/19/extending-the-webdeploy-manifest/
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.
I had to add this to the actual project file (not a publish profile) to get it working on our build server
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
This MS doc suggests that you actually need both CopyAllFilesToSingleFolderForPackageDependsOn and CopyAllFilesToSingleFolderForMsdeployDependsOn
Ref: https://docs.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/deploying-extra-files
Like so …
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForPackageDependsOn);
CustomCollectFiles;
$(CopyAllFilesToSingleFolderForMsdeployDependsOn);
Thanks for sharing Tony!