如何在 msbuild.exe 启动和结束时调用我的 MSBuild 目标 [英] How can I invoke my MSBuild Target when msbuild.exe starts and when it ends

查看:18
本文介绍了如何在 msbuild.exe 启动和结束时调用我的 MSBuild 目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说,我希望在整个构建会话的开始和结束时只调用一次此目标,而不是针对可能作为该会话一部分构建的每个单独项目.

In other words, I want this Target to be invoked just once a the start and end of the whole build session, not for each individual project that might be built as part of that session.

另一种说法是,当在 Visual Studio 中点击 Build (F7) 时,我想在构建的最开始和结束时调用我的自定义 Target,而不管构建了什么(以及它是否成功).

Another way to put is it that when hitting Build (F7) in Visual Studio I would like to invoke my custom Target at the very beginning and end of the build, regardless of what was built (and whether or not it was successful).

这可能吗?如果没有,是否有使用 MSBuild 目标的替代方法,可以让我在每次 Visual Studio 构建的开始和结束时调用我的进程?

Is this possible? If not, are there alternatives to using MSBuild Targets that would allow me to call my process at the start and end of every Visual Studio build?

推荐答案

要执行解决方案范围的 Before 和 After 目标,您将创建两个名为after.<SolutionName>.sln.targets"和之前.<SolutionName>.sln.targets" 在与您的解决方案相同的文件夹中.

To execute a solution-wide Before and After targets, you would create two MSBuild project files named "after.<SolutionName>.sln.targets" and "before.<SolutionName>.sln.targets" in the same folder as your solution.

要对所有解决方案执行此操作,您需要将目标文件之后的自定义解决方案级别放到路径 $(MSBuildExtensionsPath)$(MSBuildToolsVersion)SolutionFileImportBefore 或 $(MSBuildExtensionsPath)$(MSBuildToolsVersion)SolutionFile导入后.构建这些解决方案后,它将导入这些文件夹中的所有项目文件(如果存在).

To do this on all solutions, you would drop your custom solution-level after targets files into the path $(MSBuildExtensionsPath)$(MSBuildToolsVersion)SolutionFileImportBefore or $(MSBuildExtensionsPath)$(MSBuildToolsVersion)SolutionFileImportAfter. When those solutions are built, it will import all project files in these folders if they exist.

要验证上述内容,请打开命令提示符并导航到包含您的解决方案文件的文件夹.键入SET MSBuildEmitSolution=1".然后运行 ​​msbuild.exe <SolutionName>.您将看到 msbuild 已将元数据项目文件 <SolutionName>.sln.metaproj 和 <SolutionName>.sln.metaproj.tmp 保存在与您的解决方案相同的路径中.

To verify the above, open a command prompt and navigate to the folder containing your solution file. Type "SET MSBuildEmitSolution=1". Then run msbuild.exe <SolutionName>. You will see that msbuild has saved the metadata project files <SolutionName>.sln.metaproj and <SolutionName>.sln.metaproj.tmp in the same path as your solution.

查看<导入/>文件顶部和底部的声明,您会注意到条件导入允许您在目标之前和之后声明特定于解决方案,或在目标之前和之后声明特定于全局解决方案.

View the <Import /> declarations at the top and bottom of the file and you'll notice the conditional imports that allow you to declare solution-specific before and after targets, or global solution-specific before and after targets.

这似乎只适用于命令行或团队构建,但不适用于 Visual Studio.

It appears that this would only be applicable to command line or team builds, but not from within Visual Studio.

我将此添加到我的 After.Solution.sln.targets 文件中:

I added this to my After.Solution.sln.targets file:

<Target Name="Banana" AfterTargets="Build">
    <Message Text="*** BEGIN BANANA ***" Importance="high" />
    <Message Text=" _                                          " Importance="high" />
    <Message Text="//                                         " Importance="high" />
    <Message Text="V                                          " Importance="high" />
    <Message Text="   \_                                      " Importance="high" />
    <Message Text="  ,'.`-.                                   " Importance="high" />
    <Message Text="   | `. `.                                 " Importance="high" />
    <Message Text="   (   `. `-.                        _,.-:" Importance="high" />
    <Message Text="        `.  `-._             __..--' ,-';/" Importance="high" />
    <Message Text="      `.   `-.   `-..___..---'   _.--' ,'/ " Importance="high" />
    <Message Text="      `. `.    `-._        __..--'    ,' /  " Importance="high" />
    <Message Text="        `. `-_     ``--..''       _.-' ,'   " Importance="high" />
    <Message Text="          `-_ `-.___        __,--'   ,'     " Importance="high" />
    <Message Text="             `-.__  `----'''    __.-'       " Importance="high" />
    <Message Text="                  `--..____..--'            " Importance="high" />
    <Message Text="*** END BANANA ***"  Importance="high" />
</Target>

从命令行:香蕉!

在 TFS 构建中:香蕉!

On a TFS build: Bananas!

Visual Studio:没有香蕉 :(

Visual Studio: No bananas :(

这篇关于如何在 msbuild.exe 启动和结束时调用我的 MSBuild 目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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