exe启动和结束时如何调用MSBuild目标

exe启动和结束时如何调用MSBuild目标

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

问题描述

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

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)时,无论构建什么内容(以及构建是否成功),我都希望在构建的开始和结束时调用自定义目标. ).

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 Targets替代方案,使我可以在每个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?

推荐答案

要执行解决方案范围内的目标之前和之后,您将创建两个MSBuild项目文件,分别名为之后.< 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)\ SolutionFile \ ImportBefore \或$(MSBuildExtensionsPath)\ $(MSBuildToolsVersion)\ SolutionFile路径中\ ImportAfter.构建这些解决方案后,它将导入这些文件夹中的所有项目文件(如果存在).

To do this on all solutions, you would drop your custom solution-level after targets files into the path $(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportBefore\ or $(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\SolutionFile\ImportAfter. 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.

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

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目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 02:44