Run an MSBuild target only if project is actually built(仅在实际构建项目时运行 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.
如何做到这一点?
无论编译/构建是否实际发生,AfterBuild 和 AfterCompile 都会被调用.
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 目标
基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
