question

MichaelBihler-8119 avatar image
0 Votes"
MichaelBihler-8119 asked AllenCai-7044 answered

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

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.

vs-msbuild
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DylanZhu-MSFT avatar image
1 Vote"
DylanZhu-MSFT answered MichaelBihler-8119 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Dylan,

thank you for your answer!

It helped me to find the reason why my custom target has not been executed when I published with Visual Studio:

In my project file I used the target name "AfterPublish". Because this is a predefined msbuild target mine has been overridden.

Regards
Michael

0 Votes 0 ·
Tison-4018 avatar image
0 Votes"
Tison-4018 answered

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 ==========


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AllenCai-7044 avatar image
0 Votes"
AllenCai-7044 answered

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>

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.