After Publish event in Visual Studio

前端 未结 5 1981
情书的邮戳
情书的邮戳 2020-12-05 13:41

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 in

相关标签:
5条回答
  • 2020-12-05 14:09

    Adding my 2 cents

    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.

    0 讨论(0)
  • 2020-12-05 14:10

    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

    0 讨论(0)
  • 2020-12-05 14:16

    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.

    0 讨论(0)
  • 2020-12-05 14:19

    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.

    0 讨论(0)
  • 2020-12-05 14:20

    My answer for another question might be of some use.

    AfterPublish script doesn't run when I publish a web app

    Particularly, using the path to the 'published files', you can pass in a parameter via $(WebProjectOutputDir)\$(WPPAllFilesInSingleFolder). This would be something like c:\{path to *.csproj}\obj\{build configuration}\Package\PackageTmp.

    0 讨论(0)
提交回复
热议问题