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

Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 MSBuild 目标)
本文介绍了仅在实际构建项目时运行 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 目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

MSBuild cannot find a reference(MSBuild 找不到参考)
The reference assemblies for framework .NETCore, Version=v5.0 were not found(未找到框架 .NETCore,Version=v5.0 的参考程序集)
quot;File has a different computed hash than specified in manifestquot; error when signing the EXE(“文件的计算哈希值与清单中指定的不同签署EXE时出错)
Good-practices: How to reuse .csproj and .sln files to create your MSBuild script for CI?(良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 MSBuild 脚本?)
MS-Build BeforeBuild not firing(MS-Build BeforeBuild 未触发)
Using C# 7.1 with MSBuild(将 C# 7.1 与 MSBuild 结合使用)