AfterPublish target not working

World's simplest task (see below) is not being executed after I publish my web application project. Any idea why?

<Target Name="AfterPublish">
  <Copy SourceFiles="C:\A.txt" DestinationFiles="C:\B.txt" />
</Target>

Solution 1:

Note: The following applies to VS2010 and publishing web-application projects with the "Web Deploy" publish method selected in the 'Build/Publish {projectname}' dialog.

Julien Hoarau's correct in that "Publish" is NOT the name of the msbuild target invoked in the above case; the actual target name is "MSDeployPublish".

Therefore, you have to define a "Target" element whose "AfterTarget" attribute's value is set to "MSDeployPublish" - the "Name" attribute's value does not matter (as long as it's unique among target names).

Here's how to do it:

  • Open the project file (e.g. *.csproj) in a text/XML editor and, just before the closing </Project> tag, add a <Target Name="CustomPostPublishAction" AfterTargets="MSDeployPublish"> element; pick a name of your choice for "CustomPostPublishAction".
  • Add a so-called Task child element that performs the desired action; for instance, to add a command to be passed to cmd.exe, use an <Exec Command="..." /> element.

Example:

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish" >
    <Exec Command="echo Post-PUBLISH event: Active configuration is: $(ConfigurationName)" />
</Target>

Note:

  • In command strings, use XML entity(?) references in place of characters that would break XML parsing, e.g. "&gt" in place of "<".
  • For documentation of the <Target> element in general, see http://msdn.microsoft.com/en-us/library/t50z2hka.aspx
  • Task-elements reference here: http://msdn.microsoft.com/en-us/library/7z253716.aspx
  • In general, if you need to determine the name of the msbuild.exe target that is actually invoked by Visual Studio 2010, do the following:
    • Go to Tools/Options..., Project and Solutions/Build and Run, select 'Detailed' (or, for even more information, 'Diagnostic') from the dropdown list labeled 'MSBuild project build output verbosity.
    • After running the build/publish action, e.g. Build/Publish, examine the Output window for the last occurrence of the string "Done building target" to determine the top-level target that was invoked.

Solution 2:

Visual Studio 2013. Publish Web application to file system.

  <Target Name="Moose" AfterTargets="GatherAllFilesToPublish" >
    <Message Importance="high" Text="***Moooooooooooooooose***$(WPPAllFilesInSingleFolder)***$(TargetDir)" />
  </Target>

Note: Make sure that build logging is set to at least to Detailed. Look for it under Tools -> Options -> Projects and Solutinos -> Build and Run -> MSBuild output verbosity. Diagnostic is also fine if you want to investigate which build target was last run before actual publish.

Solution 3:

This seems to work in Visual Studio 2019

<Target Name="MyCustomTarget" AfterTargets="Publish">
  <Copy SourceFiles="C:\A.txt" DestinationFiles="C:\B.txt" />
</Target>