Solution 1:

The Publish context menu isn't running "Publish" target (if we are speaking about publishing website, not publishing ClickOnce package).

If you are using VS2010 - context menu will run "PipelinePreDeployCopyAllFilesToOneFolder" target, and in VS2012 (keep this in mind if you are going to switch) it will run "MSDeployPublish" target.

I suppose you should read this question and answer. Jez and I provided pretty comprehensive answer on how to hook to Before\After publish target.

In short - for MSBuild version>=4.0 you could use this approach

<Target Name="Mytarget" AfterTargets="PipelinePreDeployCopyAllFilesToOneFolder" >
    <Message Label="Test"></Message>        
    <Warning Label="Test"></Warning>
</Target>

@Edit1: use CopyAllFilesToSingleFolderForPackage instead of PipelinePreDeployCopyAllFilesToOneFolder - the files should be copied after this target. If you need to fire your target only when it launched in VS context - check the link I posted and add some more conditions - to check for Visual studio launch like this Condition="'$(BuildingInsideVisualStudio)'=='true' AND '$(VisualStudioVersion)'=='10.0'" If you add more context like what kind of target do you want to launch after publishing etc. - it could add more context and help others to understand the issue

Solution 2:

UPDATE: seems like in VS 2019 and .NET 5 you can now use Publish target.

<Target Name="Test" AfterTargets="Publish">
    <Exec Command="blablabla" />
</Target>

Here's my old answer that also works:


MS has confirmed, that when publishing to file system they don't have any target to launch after that.

"We currently do not support executing custom targets after publish from VS for the file system protocol."

Quoted from this SO question

So what we ended up doing is:

  1. use AfterTargets="CopyAllFilesToSingleFolderForPackage" (runs just before the files are copied to the publish location)
  2. which executes a bat-file
  3. the bat-file starts with a timeout 10 command (waits 10 seconds) - for the lack of a better way.

IMPORTANT: This bat file has to be executed asynchronously so the publish process continues after it's been launched, please refer to this SO answer on how to do that.

Solution 3:

I tried every other answer but they just didn't work. In my case, I'm publishing a VSTO Add-In with VS2015, so maybe there's something different going on, but here are two options that did work:

<Target Name="AfterPublish">
  <Warning Text="publish warning 1" />
</Target>
<Target Name="CustomTarget" AfterTargets="AfterPublish">
  <Warning Text="publish warning 2" />
</Target>

Part of the reason I (and assumedly others) had a hard time with this is that the Message task doesn't seem to do anything. I was expecting text in the Output view, but nothing was showing up. There were no Message items in the Error List either, but by using Warning instead, they did show up in the Error List. Unfortunately I don't know enough about MSBuild to say why Message isn't behaving as expected.

Edit: See this excellent answer about how to see exactly which targets are being executed.

Solution 4:

I just wrote a post on how I achieved this in Visual Studio 2013 here: http://www.alexdresko.com/2015/02/28/taking-visual-studio-build-and-publish-events-to-the-next-level/

Essentially, this is the magic:

<Target Name="Mytarget" AfterTargets="MSDeployPublish" >
    <Message Text="The name of the publish profile is $(DestinationAppRoot)"/>   
    .... Here's where you do something awesome... 
</Target>

I encourage you to read the whole post for more information.