VS2012 AfterPublish for Web Application
-
Tuesday, August 21, 2012 6:51 AM
We are using the One Click Publish feature in VS2012 and would like to execute some commands after the publish process has finished.
We have added the following element to our .pubxml file but this does not get fired:
<Target Name="AfterPublish" AfterTargets="MSDeployPublish">
What is the correct way of doing this in VS2012?
All Replies
-
Thursday, August 23, 2012 3:55 AMModerator
Jason,
Do you use VS 2012 RC or RTM? If it's VS 2012 RC, you need the July Update for VS 2012. One Click Publish feature has a known issue here.
Refer to below article to apply the July update: http://blogs.msdn.com/b/webdev/archive/2012/07/12/visual-studio-2012-rc-web-tooling-extensions-update.aspx
Forrest Guo | MSDN Community Support | Feedback to manager
- Proposed As Answer by Forrest GuoMicrosoft Employee, Moderator Thursday, September 27, 2012 9:52 AM
- Unproposed As Answer by jason_hill Thursday, September 27, 2012 10:08 PM
-
Thursday, August 23, 2012 5:29 AM
Hi Forrest,
I am using RTM so it sounds like this should work right? Is it just a case of adding the target into the .pubxml?
Thanks,
Jason
-
Thursday, August 23, 2012 7:57 AMModerator
I'm afraid you misplaced the script. In order to execute a task after publish, we should add the target to project file, but not the ".pubxml", in below script, I write a few words to build output logging:
<Target Name="AfterPublish" AfterTargets="MSDeployPublish" > <Message Importance="normal" Text="Project Published, Forrest" ></Message> </Target>You can replace the <Message> task with your task.
Please report back if you could get it working.
thanks.
Forrest Guo | MSDN Community Support | Feedback to manager
- Proposed As Answer by Forrest GuoMicrosoft Employee, Moderator Thursday, August 23, 2012 7:57 AM
- Unproposed As Answer by jason_hill Thursday, September 27, 2012 10:08 PM
-
Thursday, August 23, 2012 8:41 AMI added the code to the project file but it still isn't firing. If I add the same code to the AfterBuild target then it does get called but I only want it to get executed at Publish. Is MSDeployPublish the correct AfterTargets value? I am using the File System Deployment in case that makes any difference.
-
Thursday, August 23, 2012 9:35 AMModerator
Sorry, what I did successfully was in VS 2010. I could not get the target executed in VS 2012 either. Please wait some time, I try to consult the issue.
thanks.
Forrest Guo | MSDN Community Support | Feedback to manager
-
Monday, August 27, 2012 12:07 AM
Hi Forrest,
Just wondering if you had any luck with this issue?
Currently, we have to either manually run the post-publish steps or open the solution in VS2010 to run the old WDP build instead.
Thanks,
Jason
-
Monday, August 27, 2012 1:45 AMModerator
Jason,
I have not gotten a solution yet, once I get, I'll update the thread soon.
To work out the problem, customize the AfterPublish target should help. I didn't test this though. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets Please well backup this file before you customizing. Refer to: http://blogs.msdn.com/b/mwade/archive/2007/11/30/how-to-archive-your-source-files-when-publishing.aspx
thanks.
Forrest Guo | MSDN Community Support | Feedback to manager
-
Monday, September 24, 2012 4:25 AM
Hi Forrest,
Any progress on this?
The post you linked to mentiones using the AfterPublish target but this is the one we have been trying to use but that isn't firing.
Thanks,
Jason
-
Wednesday, September 26, 2012 2:32 AMModerator
Jason,
I get it working, in both Visual Studio 2012, and the msbuild commandline. Last time I said it didn't work in VS 2012, that's because the build logging level is set to minimal. Once I set it to normal, it shows up the custom task get executed.
Sample pubxml attached below, please let me know if you couldn't get it working.
<?xml version="1.0" encoding="utf-8"?> <!-- This file is used by the publish/package process of your Web project. You can customize the behavior of this process by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>MSDeploy</WebPublishMethod> <SiteUrlToLaunchAfterPublish /> <MSDeployServiceURL>http://localhost</MSDeployServiceURL> <DeployIisAppPath>Default Web Site/MyApp</DeployIisAppPath> <RemoteSitePhysicalPath /> <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer> <MSDeployPublishMethod>InProc</MSDeployPublishMethod> <UserName /> <_SavePWD>False</_SavePWD> <PublishDatabaseSettings> <Objects xmlns="" /> </PublishDatabaseSettings> </PropertyGroup> <Target Name="AfterPublish" AfterTargets="MSDeployPublish" > <Message Importance="normal" Text="Project Published, Forrest" ></Message> </Target> </Project>
Forrest Guo | MSDN Community Support | Feedback to manager
- Edited by Forrest GuoMicrosoft Employee, Moderator Wednesday, September 26, 2012 2:33 AM
-
Wednesday, September 26, 2012 5:09 AM
In a previous post, you said the code needed to go in the project file but the above post mentions placing this in the pubxml file now (which is where I initially had it).
I have tried inserting this code in both the project file and the pubxml but it does not fire in either place.
In your sample above, you are using the WebPublishMethod of MSDeploy but I am using FileSystem deployment so I have FileSystem in that element instead. Maybe it needs to be a different AfterTargets value? I have tried AfterTargets="FileSystemPublish" but that doesn't work.
-
Wednesday, October 10, 2012 6:44 AMModerator
Jason,
Sorry for late reply. In previous, I was not aware of you publish to FileSystem. In this case, if you want to execute a custom task, you can override the PipelineCollectFilesPhaseDependsOn property. Below is a sample from MSBuild program manager:
<?xml version="1.0" encoding="utf-8"?> <!-- This file is used by the publish/package process of your Web project. You can customize the behavior of this process by editing this MSBuild file. In order to learn more about this please visit http://go.microsoft.com/fwlink/?LinkID=208121. --> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <WebPublishMethod>FileSystem</WebPublishMethod> <SiteUrlToLaunchAfterPublish /> <publishUrl>C:\temp\publish\02</publishUrl> <DeleteExistingFiles>False</DeleteExistingFiles> </PropertyGroup> <PropertyGroup> <PipelineCollectFilesPhaseDependsOn> CustomCollectFiles; $(PipelineCollectFilesPhaseDependsOn); </PipelineCollectFilesPhaseDependsOn> </PropertyGroup> <Target Name="CustomCollectFiles"> <Message Text="Inside of CustomCollectFiles" Importance="high"/> <ItemGroup> <_CustomFilesddd Include="$(MSBuildThisFileDirectory)..\..\..\additional files\**\*" /> <FilesForPackagingFromProject Include="%(_CustomFilesddd.Identity)"> <DestinationRelativePath>additional files\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath> </FilesForPackagingFromProject> </ItemGroup> </Target> </Project>A complete sample project that demo this can be found here: https://github.com/sayedihashimi/publish-samples/tree/master/FileSysIncludeOtherFiles
You will want to create a new publish profile and replicate the custom code above. The **.pubxml.user is automatically generated.
Did you get it working?
Forrest Guo | MSDN Community Support | Feedback to manager
-
Friday, October 12, 2012 4:20 AM
Hi Forrest,
I did mention that I was using File System publish in a previous post as I thought that might be part of the problem.
The sample you provided seems to be for implementing a custom task prior to the files being published to the target location. As per my original post, I need to implement a custom task after the publish has completed though. Is there a different phase that I can hook into?
Thanks,
Jason
-
Tuesday, October 16, 2012 2:36 AM
The official response I have received on this is:
"We currently do not support executing custom targets after publish from VS for the file system protocol. If you publish from the command line the target will be executed however."
As a workaround, we have switched to using Web Deploy Package and then setting the PackageAsSingleFile to false in the pubxml file. We can then hook into the following event to run the appropriate custom tasks at the end of the process:
<Target Name="CustomAfterPackage" AfterTargets="Package">
</Target>It seems that you still have to use a .zip name for the package location which creates a folder of that name, even though we have specified that we don't want to package it as a single file.
Quite deep within that destination folder hierarchy is the uncompressed website folder that we can manually work with and then compress using 7-Zip (which results in a 60% reduction in the compressed package file versus the built-in compression mechanism).
It would be nice to see support for custom tasks after publishing in VS using the File System protocol too.
- Proposed As Answer by Forrest GuoMicrosoft Employee, Moderator Wednesday, October 17, 2012 12:48 PM
- Marked As Answer by jason_hill Wednesday, October 17, 2012 10:59 PM
-
Monday, November 26, 2012 3:42 PM
Try this. It works for FileSystem publish as well as the ZIP package.
<Target Name="CustomAction" AfterTargets="CopyAllFilesToSingleFolderForPackage">
</Target>
I have this target in the *.pubxml file.
You also have access to the Deploy directory via: $(_MSDeployDirPath_FullPath) variable
Rolek
-
Tuesday, December 18, 2012 3:35 AM
Hi Rolek,
I can confirm that this method works great.
Thanks,
Jason

