How can I call my MSBuild Target when msbuild.exe starts and when will it end?

In other words, I want this goal to be called only once after the start and end of the entire build session, and not for each individual project that can be created as part of this session.

Another way to deliver this is when I hit Build (F7) in Visual Studio, I would like to call my custom Target at the very beginning and at the end of the build, regardless of what was built (and whether it was successful).

Is it possible? If not, are there any alternatives to using MSBuild targets that would allow me to call my process at the beginning and end of each Visual Studio build?

+6
source share
1 answer

To accomplish the targets before and after the solution, you must create two MSBuild project files named "after. <SolutionName> .sln.targets" and "before. <SolutionName> .sln.targets" in the same way as your solution.

To do this on all solutions, you will lose your custom solution level after the goal files to the path $ (MSBuildExtensionsPath) \ $ (MSBuildToolsVersion) \ SolutionFile \ ImportBefore \ or $ (MSBuildExtensionsPath) \ $ (MSBuildToolsVersion) \ SolutionFile \ ImportAfter. When these solutions are built, he will import all the project files into these folders, if they exist.

To verify this, open a command prompt and navigate to the folder containing the solution file. Enter "SET MSBuildEmitSolution = 1". Then run msbuild.exe <SolutionName>. You will see that msbuild saved the <SolutionName> .sln.metaproj and <SolutionName> .sln.metaproj.tmp metadata project files in the same way as your solution.

View the <Import /> declarations at the top and bottom of the file, and you will notice a conditional import that will allow you to declare specific decisions before and after goals or global specific decisions before and after goals.

Edit:

It seems like this only applies to the command line or command assembly, but not from Visual Studio.

I added this to my After.Solution.sln.targets file:

<Target Name="Banana" AfterTargets="Build"> <Message Text="*** BEGIN BANANA ***" Importance="high" /> <Message Text=" _ " Importance="high" /> <Message Text="//\ " Importance="high" /> <Message Text="V \ " Importance="high" /> <Message Text=" \ \_ " Importance="high" /> <Message Text=" \,'.`-. " Importance="high" /> <Message Text=" |\ `. `. " Importance="high" /> <Message Text=" ( \ `. `-. _,.-:\" Importance="high" /> <Message Text=" \ \ `. `-._ __..--' ,-';/" Importance="high" /> <Message Text=" \ `. `-. `-..___..---' _.--' ,'/ " Importance="high" /> <Message Text=" `. `. `-._ __..--' ,' / " Importance="high" /> <Message Text=" `. `-_ ``--..'' _.-' ,' " Importance="high" /> <Message Text=" `-_ `-.___ __,--' ,' " Importance="high" /> <Message Text=" `-.__ `----''' __.-' " Importance="high" /> <Message Text=" `--..____..--' " Importance="high" /> <Message Text="*** END BANANA ***" Importance="high" /> </Target> 

From the command line: Bananas!

In the TFS assembly: Bananas!

Visual Studio: no bananas: (

+17
source

Source: https://habr.com/ru/post/949679/


All Articles