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

╄→гoц情女王★ 提交于 2019-12-04 19:12:26

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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!