仅在实际构建项目时运行 MSBuild 目标 [英] Run an MSBuild target only if project is actually built

查看:21
本文介绍了仅在实际构建项目时运行 MSBuild 目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个构建 */.sln 文件的 MSBuild 文件(构建所有存在的 .sln 文件).

I have an MSBuild file that builds */.sln files (builds all .sln files that exist).

构建使用 Build 目标,因此如果没有对输入文件进行任何更改,则不应再次构建任何项目.

The build uses the Build target, so if no changes were made to input files, no project should be built again.

我想执行一些自定义目标只有在项目实际上再次构建时.

I would like to execute some custom target only if a project actually gets built again.

如何做到这一点?

无论编译/构建是否实际发生,AfterBuildAfterCompile 都会被调用.

Both AfterBuild and AfterCompile are always called, no matter if the compile/build actually takes place.

推荐答案

例如,基本上你想要与 PostBuildEvent 相同的行为,所以我查找了 Microsoft.Common.Targets 做到了(这个文件 always 提供了关于如何使用 msbuild 的一个很好的见解).这是解决方案:

Basically you want the same behaviour as the PostBuildEvent for instance, so I looked up how Microsoft.Common.Targets does it (this file always provides a nice insight in to how msbuild is supposed to be used). Here's the solution:

<PropertyGroup>
  <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
</PropertyGroup>
<Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
<Target Name="RunWhenBuild" AfterTargets="CoreBuild"       
        Condition="'$(_AssemblyTimestampBeforeCompile)'!='$(_AssemblyTimestampAfterCompile)'">
  <Message Importance="high" Text="RunWhenBuild!!"/>
</Target>

这就是发生的事情:当有一个名为 RunPostBuildEvent 且值为 OnOutputUpdated 的属性时,CoreBuild 目标的依赖关系最终会记录下来构建前后输出文件的时间戳.如果它们相等,则不会生成输出.所以剩下的就是让你的目标在 CoreBuild 之后运行并检查这些时间戳.

And this is what goes on: when there is a property named RunPostBuildEvent with a value of OnOutputUpdated the CoreBuild target's dependncies will eventually record the timestamp of the output file before and after the build. And if they are equal the output was not build. So all that is left is getting your target to run after CoreBuild and checking on these timestamps.

这篇关于仅在实际构建项目时运行 MSBuild 目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆