I have a code generator tool that I partially own, and now that csproj files can list multiple Target Frameworks in them and building builds all of them, I am trying to figure o
While some targets only run in the inner builds, e.g. when you use BeforeTargets="BeforeBuild"
, the outer build also defines the IsCrossTargetingBuild
variable to indicate that the currently running build is the outer build which dispatches to the inner build and is the preferred way to condition targets.
So you can condition your target like Condition="'$(IsCrossTargetingBuild)' == 'true'"
to make sure the target is only run for the outer build.
The other way is use GenerateNuspec
when you want to change the nuget file.
You will find the Foo output once when you package.
Condition="'$(TargetFrameworks)' == '' OR $(TargetFrameworks.EndsWith($(TargetFramework)))"
On single target framework I only use BeforeTargets="PreBuildEvent"
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<Target Name="GenerateVersionInfo" BeforeTargets="PreBuildEvent">
<Exec Command="your custom command" />
</Target>
</Project>
on multi target frameworks I use BeforeTargets="DispatchToInnerBuilds"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
</PropertyGroup>
<Target Name="GenerateVersionInfo" BeforeTargets="DispatchToInnerBuilds">
<Exec Command="your custom command" />
</Target>
</Project>
So my custom command is only exeuted once before every build. If you use InitialTargets
, the command is executed more often than only once! For example if you save your project!