Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Publish event in Visual Studio

I am trying to invoke simple task after publish event. When I say "publish", I mean publish in Visual Studio, right click on project and pressing "Publish...". I have included (Imported) targets file in project file which works fine because I have already tested it on Build event. I have found at http://msdn.microsoft.com/en-us/library/ms366724.aspx that there is AfterPublish event which should do what I need, but it doesn't. I am not sure if this is a same event which should trigger on Publish in Visual Studio, someone please clarify this. My question is how to trigger any kind of task from targets file on Publish in Visual Studio?

I have tried this in targets file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">     <Target Name="AfterPublish">         <Message Label="Test"></Message>                 <Warning Label="Test"></Warning>     </Target>    </Project> 

I am using Visual Studio 2010.

EDIT:

I am actually looking for any way to execute certain action on Publish in Visual Studio. I was also thinking of adding Build Events, but I have no idea how to determine whether it is Publish in progress or not.

EDIT: @Alexey Shcherbak thank you for your fast reply. I am getting this in my MSBuild output:

12/10/2012 12:29:40 AM:        Done executing task "CallTarget". 12/10/2012 12:29:40 AM:        Done building target "PipelinePreDeployCopyAllFilesToOneFolder" in project "PublishTestApp.csproj". 12/10/2012 12:29:40 AM:Done building project "PublishTestApp.csproj". Deleting existing files... Publishing folder /... Publishing folder Account... Publishing folder bin... Publishing folder Scripts... Publishing folder Styles... ========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ========== ========== Publish: 1 succeeded, 0 failed, 0 skipped ========== 

So my task will executes right after PipelinePreDeployCopyAllFilesToOneFolder but before actual coping of the files and I don't consider Publish being done yet at that point. Of course, I did actually test this, so in MSBuild task I was executing simple read from text file that is suppose to be copied in the Publish folder, and it failed.

like image 923
Andrija Avatar asked Dec 08 '12 20:12

Andrija


People also ask

What does Visual Studio Publish do?

Publishing creates the set of files that are needed to run your application. To deploy the files, copy them to the target machine.

How do I Publish Microsoft Visual Studio?

Right-click on the project (not the solution) in Solution Explorer and select Publish. In the Publish tab, select Publish. Visual Studio writes the files that comprise your application to the local file system. The Publish tab now shows a single profile, FolderProfile.

Where are Visual Studio Publish profiles stored?

The default location for $(MSBuildSDKsPath) (with Visual Studio 2019 Enterprise) is the %programfiles(x86)%\Microsoft Visual Studio\2019\Enterprise\MSBuild\Sdks folder. Microsoft.


2 Answers

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

like image 163
Alexey Shcherbak Avatar answered Sep 28 '22 02:09

Alexey Shcherbak


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.

like image 24
Alex from Jitbit Avatar answered Sep 28 '22 01:09

Alex from Jitbit