AfterPublish target not working
Asked Answered
U

5

37

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>
Usn answered 24/8, 2010 at 23:3 Comment(0)
T
38

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.
Timbered answered 24/7, 2011 at 3:59 Comment(3)
Hi, Do you know if there is a way to make it work with FTP publish?Modulator
I would like to make this work for File System publish.. any ideia how? Thanks.Eyeleteer
@MauricioRamalho: I suggest you ask a new question (create a new post) with specifics. (I personally won't be of much help anymore, as I haven't worked with these things in years, but I'm sure someone will be able to help you.)Timbered
E
29

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.

Entophyte answered 2/10, 2014 at 11:10 Comment(4)
This is the only target that fires for me in VS2015 when performing a file system publish of a web application project. Neither MSDeployPublish nor AfterPublish work.Instar
If you want thepath published to, use $(PublishUrl)Phelgon
Yes ! That's the answer I was looking for, for so long !Scend
According to this MSDN forum link, it appears the only publish target available to web projects is GatherAllFilesToPublish.Dendritic
C
8

This seems to work in Visual Studio 2019

<Target Name="MyCustomTarget" AfterTargets="Publish">
  <Copy SourceFiles="C:\A.txt" DestinationFiles="C:\B.txt" />
</Target>
Coburn answered 2/7, 2020 at 16:8 Comment(0)
B
2
  • You must define override the target at the end of your file, after <Import ... />
  • Launch MSBuild with detailed verbosity to see why your target is ignored :

    msbuild project.csproj /t:Target_to_Launch /v:d
    

AfterPublish is called after Publish target, but Publish is not the target called when you publish a web application. Publish is the target for publishing ClickOnce application.

You'll have to find the target used when you call Publish in Visual Studio, it could be Package, WebPublish...

Bevan answered 25/8, 2010 at 4:45 Comment(1)
Here are some highlights from the output: Overriding target "AfterPublish" in project "c:\...\Microsoft.Common.targets" with target "AfterPublish" from project "C:\...\Ara.Web.csproj". Target "_DeploymentUnpublishable" in file "c:\...\Microsoft.Common.targets" from project "C:\...\Ara.Web.csproj": Task "Message" Skipping unpublishable project. I can publish it from within Visual Studio, but the AfterPublish task is not working.Usn
I
-1

I'm a little lazy right now to figure out the mess of targets to find the right one for a file-based publish (which you might be interested in). What you can do in the meantime is defining an AfterBuild target in the *.pubxml file.

<Target Name="AfterBuild">

...

I recommend also turning off the property "DeleteExistingFiles" because if you copy files into the directories being published, it does a clean somewhere during the publishing process.

<DeleteExistingFiles>False</DeleteExistingFiles>
Induplicate answered 29/3, 2013 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.