multi-target project: run a command once after all builds are done
Asked Answered
F

0

2

I have a class library in an SDK style project. It targets several frameworks, e.g.:

<TargetFrameworks>net48;netcoreapp3.1;net5.0-windows;net6.0-windows</TargetFrameworks>

I have a target that I want to run after the builds, e.g.

<Target Name="WriteText" AfterTargets="AfterBuild">
    <Exec Command="echo $(TargetFramework)"/> <!-- print out target framework -->
</Target>

If I run dotnet build, I can see from the console that the command is run after each build. But I want it to be run just once after all builds are done.

How can I make it be run just once after all builds are done?

I know I can work with conditions, so I could put a condition like

Condition="'$(TargetFramework)' == 'net6.0-windows'"

into my target to let it run after a certain framework build. But I need it to run after the LAST build, and which one is the last build? Is it

  • the last one in my <TargetFrameworks> list? (No.)
  • always a certain one in my <TargetFrameworks> list? (Seems so on my computer, at the moment. But can I be sure of it? What about other computers or future MSBuild versions?)

What I think I need is a way to manipulate a variable during builds, and then put a condition on that variable. Like this:

  • There is an int variable finishedBuilds that is 0 before any build is done.
  • There is a target that adds 1 to finishedBuilds after build. It has no condition, so it is called after each build.
  • The condition for my desired command is then Condition="'$(finishedBuilds)' == '4'".

Is that possible, and if yes, how?

Fazio answered 7/2, 2022 at 8:31 Comment(2)
Martin Ullrich answered my question in the comment section below this answer: use AfterTargets="DispatchtoInnerBuilds".Fazio
This does not work. The output shows that my multi-target-post-build runs immediately after the first target is finished. Yes it runs only once but too early. Do you have a solution to that?Antin

© 2022 - 2024 — McMap. All rights reserved.