简体   繁体   English

仅在实际构建项目时运行MSBuild目标

[英]Run an MSBuild target only if project is actually built

I have an MSBuild file that builds * / .sln files (builds all .sln files that exist). 我有一个MSBuild文件,用于构建* / .sln文件(构建所有存在的.sln文件)。

The build uses the Build target, so if no changes were made to input files, no project should be built again. 构建使用Build目标,因此如果没有对输入文件进行任何更改,则不应再次构建任何项目。

I would like to execute some custom target only if a project actually gets built again. 我想只在项目实际上再次构建时才执行一些自定义目标。

How can this be done? 如何才能做到这一点?

Both AfterBuild and AfterCompile are always called, no matter if the compile/build actually takes place. 无论编译/构建是否实际发生,都始终调用AfterBuildAfterCompile

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). 基本上你需要和PostBuildEvent一样的行为,所以我查看了Microsoft.Common.Targets是如何做到的(这个文件总能提供一个很好的洞察如何使用msbuild)。 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>

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. 这就是:当存在一个名为RunPostBuildEvent的属性,其值为OnOutputUpdatedCoreBuild目标的依赖关系将最终记录构建之前和之后的输出文件的时间戳。 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. 所以剩下的就是让你的目标在CoreBuild之后CoreBuild并检查这些时间戳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM