AfterPublish target not working

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

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> 

回答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.


回答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.



回答3:

  • 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...



回答4:

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> 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!