How to make an MSBuild Target that only runs once instead of once, before Targets that run once per framework in the TargetFrameworks tag?

后端 未结 4 1873
梦如初夏
梦如初夏 2021-02-11 07:16

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

相关标签:
4条回答
  • 2021-02-11 07:56

    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.

    0 讨论(0)
  • 2021-02-11 08:05

    The other way is use GenerateNuspec when you want to change the nuget file.

    You will find the Foo output once when you package.

    0 讨论(0)
  • 2021-02-11 08:10
    Condition="'$(TargetFrameworks)' == '' OR $(TargetFrameworks.EndsWith($(TargetFramework)))" 
    
    0 讨论(0)
  • 2021-02-11 08:15

    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!

    0 讨论(0)
提交回复
热议问题