why the C# pre-build and post-build events are not called if I import a custom target with my own build target in it.

Basu 41 Reputation points
2024-10-18T18:57:01.24+00:00

why the C# pre-build and post-build events are not called if I import a custom target with my own build target in it ?

I have a C# project file, which I modified by importing my own custom target file. Inside the target, I have defined a "Build" target which will be called when I do the build from the Visual Studio IDE or from the MSBUILD command line.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="Build">

<Message Text="Build started" Importance="high"/>

</Target>

</Project>

The "Build" target from the custom target file is getting called if I do the build from the Visual Studio IDE or from the MSBUILD command line. But, the pre-build and post-build events configured inside the project file is not getting called after importing my own custom target file.

It will be great if anybody could suggest what kind of change I have to make in my target file to call the pre-build event before calling the "Build" target from my target file, and the post-build event after the "Build" target.

Developer technologies | Visual Studio | Other
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,446 Reputation points Microsoft External Staff
    2024-10-21T06:30:05.3433333+00:00

    Hello @67883078,

    I think you have got the answer/solution here: Why are the C# pre-build and post-build events not called if I import a custom target with my own build target in it?

    As a summary, the default target, Build target, was overridden in your .targets file. However, the "new" Build target didn’t depend on the pre-build event target and the post-build event target, which made them didn’t start when you were building the project.

    The key is that do not simply override the default target(pick a different custom target name).

    BTW, as we mention "override", one thing to note again is that the order is very important in the project file(.csproj file).

    Feel free to contact us if you have any further concerns:)

    Have a great day!

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.