Search code examples
c#visual-studiomsbuildpost-build-eventpost-build

Where do we edit AfterBuild target in Visual Studio


Today, I came to know about AfterBuild target when I was trying to build a GitHub project. I had hard time figuring out a failing command which was getting fired after the build. I was not sure from where the command was getting fired because its post build event was empty.

This was the tag which I commented to get rid of the error:

<Target Name="AfterBuild" DependsOnTargets="_GetSignToolPath">
    <Exec Command="&quot;$(_SignToolPath)&quot; sign /fd sha256 /sha1 $(SigningCertificateThumbprint) /t http://timestamp.verisign.com/scripts/timstamp.dll /v &quot;$(TargetPath)&quot;" />
    <Copy SourceFiles="$(TargetPath)" DestinationFolder="$(MSBuildThisFileDirectory)..\Demo Website\Content" />
  </Target>

To edit post build events we go to project properties of any C# project and then click on Build Events tab. Here we get the option to edit the post-build event:

enter image description here

So similarly is there any UI in Visual Studio to edit AfterBuild targets of a C# project? OR this can be edited only by modifying the project XML after unloading the project. I'm using Visual Studio 2019.


Solution

  • In short:

    There isn't any other UI in VS to edit AfterBuild targets, it can only be edited in project XML.

    Analyzing:

    To my knowledge, there is no more UI for AfterBuild target in Visual Studio.

    Actually, the AfterBuild target is an empty predefined target, it is the same with other targets such as BeforeBuild, CoreBuild and so on. You can find the detailed description here: Override predefined targets.

    The document mentioned:

    By default, the empty targets in the common targets do nothing, but you can override their default behavior by defining the targets you want in a project file that imports the common targets.

    So, normally, there is no UI to edit/modify the AfterBuild target in VS, and if you need to use it, or more accurate, override it, you need to edit MSBuild codes in a project file(.xxproj file).

    Focus on your issue, you mentioned you were not sure from where the command was getting fired because its post build event was empty.

    Oh, just stop and add some other words about post build event. Actually, though post build event runs after Build completed, post build event is set as <PostBuildEvent> XXXXX</PostBuildEvent> in <PropertyGroup>XXXXX</PropertyGroup>, and it is not very related to AfterBuild target.

    But is it possible to consider other methods to find out where the command was getting fired, for example, to check the output information from VS’s output window?

    Output window will give you as much information as you want for the building process, if you have set the MSBuild project build output verbosity to Detailed or Diagnostic in Tools > Options > Projects and Solutions > Build And Run. You can even search and find the related targets/events which you want to see.

    Did a small test and here are some screenshots:

    enter image description here

    enter image description here

    BTW, yes, it is not convenient, indeed, it will be better if it can be set/modified by using UI.