Is there any msbuild target that is executed after publishing a .NET core desktop app (WPF)?

Michael Bihler 21 Reputation points
2020-08-29T10:56:47.303+00:00

I need to execute a command after publishing a .NET core WPF app in Visual Studio.

I tried following custom target in .csproj and .pubxml, but it doesn't work:

   <Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
         <Exec Command="ECHO custom action has been called" />
   </Target>

I also tried AfterTargets="Publish" with no success.

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,796 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dylan Zhu-MSFT 6,421 Reputation points
    2020-08-31T08:09:46.637+00:00

    Hi MichaelBihler,

    You could set AfterTargets=Publish in csproj file, then open developer command prompt for visual studio 2019, and run this command:

    msbuild project.csproj -target:publish  
    

    The result should be like this:
    21494-annotation-2020-08-31-102623928.jpg

    By the way, it could also work with VS on my side. Maybe you could create a new wpf application to check if it could work.
    21410-annotation-2020-08-31-1026239228.jpg

    Best Regards,
    Dylan

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Tison 1 Reputation point
    2021-08-20T18:21:43.12+00:00

    Not working for Visual Studio 2019 Version 16.11.0 Preview 4.0

    <Target Name="CustomActionsBeforePublish" BeforeTargets="BeforePublish">
    <Message Text="$(PackageName) BeforePublish" Importance="high" />
    </Target>

    <Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
    <Message Text="$(PackageName) AfterPublish" Importance="high" />
    </Target>

    <Target Name="PostPublish" AfterTargets="Publish">
    <Message Importance="high" Text="FINISH to Publish $(PackageName)" />
    <Exec Command="ECHO PUBLISH to server" />
    </Target>

    Result:

    ------ Build started: Project: Mgft.Core.Cache, Configuration: Release Any CPU ------
    FINISH to Build bin\Release\net5.0\Mgft.Core.Cache.1.0.0.0.nupkg
    Done building project "Mgft.Core.Cache.csproj".
    Successfully created package ..bin\Release\Mgft.Core.Cache.1.0.0.nupkg'.
    Publish started: Project: Mgft.Core.Cache, Configuration: Release Any CPU ------
    Successfully created package ...Mgft.Core.Cache.1.0.0.nupkg'.
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    ========== Publish: 1 succeeded, 0 failed, 0 skipped ==========

    0 comments No comments

  2. Allen Cai 1 Reputation point
    2022-05-06T07:03:57.5+00:00

    Whether you execute the publish from the command line or click publish from the Visual Studio,
    it mainly depends on whether your project's output type is Library or WinExe/Exe.

    If it is WinExe or Exe you need to focus on the Publish target,
    if it is Library, you need to focus on the GenerateNuspec target.

    Finally, if you need to do something after the execution is published, use the AfterTargets property.

    If you don't care about the output type and want to write a more generic release target,
    you can merge the two targets together as follows:

    <Target Name="CustomActionsAfterPublish" AfterTargets="GenerateNuspec;Publish">
      <Message Text="ECHO custom action has been called" Importance="high" />
    </Target>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.