如何在msbuild.exe启动和结束时调用我的MSBuild目标

Ben*_*Ben 9 msbuild visual-studio visual-studio-2012

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

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

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

Nic*_*eus 22

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

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

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

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

编辑:

看起来这只适用于命令行或团队构建,但不适用于Visual Studio内部.

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

<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>
Run Code Online (Sandbox Code Playgroud)

从命令行:香蕉!

在TFS构建:香蕉!

Visual Studio:没有香蕉:(

  • 哎呀...根据本文,这些扩展点在 Visual Studio 中不可用:( http://connect.microsoft.com/VisualStudio/feedback/details/782934/visual-studio-not-executing-before-或解决方案文件目标之后 (2认同)