前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MSBuild 在编写编译任务的时候判断当前是否在 Visual Studio 中编译

MSBuild 在编写编译任务的时候判断当前是否在 Visual Studio 中编译

作者头像
walterlv
发布2023-10-22 10:22:31
2170
发布2023-10-22 10:22:31
举报
文章被收录于专栏:walterlv - 吕毅的博客

我们这里说的编译任务是 MSBuild 的 Target。虽然只有少部分,但确实有一些情况需要判断是否在 Visual Studio 中编译的时候才需要执行的编译任务,典型的如某些仅为设计器准备的代码。


本文需要理解的前置知识是:

而使用 Visual Studio 编译的时候,会自动帮我们设置 BuildingInsideVisualStudio 的值为 True,所以实际上我们可以使用这个值进行判断。

我们可以在 Microsoft.NET.Sdk 中找到不少使用此属性的编译任务。

比如为了 IO 性能考虑的硬连接,在 Visual Studio 中即便打开也不会使用:

1 2 3 4 5 6 7 8 9 10 11 12

<!-- ============================================================ CopyFilesToOutputDirectory Copy all build outputs, satellites and other necessary files to the final directory. ============================================================ --> <PropertyGroup> <!-- By default we're not using Hard or Symbolic Links to copy to the output directory, and never when building in VS --> <CreateHardLinksForCopyAdditionalFilesIfPossible Condition="'$(BuildingInsideVisualStudio)' == 'true' or '$(CreateHardLinksForCopyAdditionalFilesIfPossible)' == ''">false</CreateHardLinksForCopyAdditionalFilesIfPossible> <CreateSymbolicLinksForCopyAdditionalFilesIfPossible Condition="'$(BuildingInsideVisualStudio)' == 'true' or '$(CreateSymbolicLinksForCopyAdditionalFilesIfPossible)' == ''">false</CreateSymbolicLinksForCopyAdditionalFilesIfPossible> </PropertyGroup>

另外 Visual Studio 接管了一部分引用项目的清理工作,所以编译任务里面也将其过滤掉了。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

<!-- ============================================================ CleanReferencedProjects Call Clean target on all Referenced Projects. ============================================================ --> <Target Name="CleanReferencedProjects" DependsOnTargets="PrepareProjectReferences"> <!-- When building the project directly from the command-line, clean those referenced projects that exist on disk. For IDE builds and command-line .SLN builds, the solution build manager takes care of this. --> <MSBuild Projects="@(_MSBuildProjectReferenceExistent)" Targets="Clean" Properties="%(_MSBuildProjectReferenceExistent.SetConfiguration); %(_MSBuildProjectReferenceExistent.SetPlatform); %(_MSBuildProjectReferenceExistent.SetTargetFramework)" BuildInParallel="$(BuildInParallel)" Condition="'$(BuildingInsideVisualStudio)' != 'true' and '$(BuildProjectReferences)' == 'true' and '@(_MSBuildProjectReferenceExistent)' != ''" ContinueOnError="$(ContinueOnError)" RemoveProperties="%(_MSBuildProjectReferenceExistent.GlobalPropertiesToRemove)"/> </Target>

关于如何探索 Microsoft.NET.Sdk 可以阅读我的另一篇博客:

本文会经常更新,请阅读原文: https://blog.walterlv.com/post/determine-building-in-visual-studio-during-building.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名 吕毅 (包含链接: https://blog.walterlv.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请 与我联系 ([email protected])

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-06-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档