MsBuild的后置构建目标

6

我已经为一个Visual Studio项目添加了一个AfterBuild目标,该项目是包含多个Visual Studio项目的解决方案的一部分。

示例解决方案设置

Example.sln

  • ExampleProj.csproj

  • ExampleProj.Test.csproj

目标示例:

  <Target Name="PostBuildScript" AfterTargets="AfterBuild" Condition="'$(Configuration)' == 'Release'">
    <PropertyGroup>
      <BuildCommand>"SomeApplication.exe")</BuildCommand>
    </PropertyGroup>
    <Exec Command="$(BuildCommand)" ConsoleToMSBuild="true" LogStandardErrorAsError="true" WorkingDirectory="$(ProjectDir)" />
  </Target>

我希望当解决方案中的所有Visual Studio项目构建完成后,这个目标才会执行。有没有办法实现这一点?
注意:我需要在使用"dotnet build"以及在Visual Studio中使用构建命令时保持相同的行为。

你对这个有什么问题? - Dan Wilson
目标运行测试(以及其他内容),我发现有时目标在测试项目构建其程序集之前运行,因此测试失败。注意:我只是将目标添加到主项目中,以便某些命令仅发生一次。我试图避免添加其他目标到其他项目中,因为这样跨项目执行目标的顺序变得难以管理。 - Ibz
我发现在解决方案中相互引用项目的最佳方式是使用项目引用。这样,项目本身确定了构建顺序,而不是解决方案文件本身。如果我是你,我会将该后置生成目标附加到始终最后构建的项目上(如果有的话)。 - C.J.
1个回答

17
我希望只有在解决方案中的所有Visual Studio项目都编译完成后,才执行此目标。
根据文档MSBuild 扩展解决方案生成,您可以在与解决方案相同的文件夹中创建一个名为after.<SolutionName>.sln.targets 的 MSBuild 项目文件。
作为测试,我将以下内容添加到我的 After.Solution.sln.targets 文件中(使用香蕉代替 SomeApplication.exe),并将此文件设置在与我的解决方案文件.sln相同的文件夹中:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <Target Name="PostBuildScript" AfterTargets="Build" Condition="'$(Configuration)' == 'Release'">
    <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>

</Project>

然后我使用dotnet build命令来构建它:

dotnet build "xxxx\TestSolutionTarget.sln" --configuration Release --verbosity n

在此输入图片描述

在所有 Visual Studio 解决方案项目构建完成之后,此目标将被执行。

或者,您也可以创建一个空项目,引用部分项目并将此目标添加到该空项目中。

希望这可以帮助到您。


1
感谢您的回复。 - Ibz
1
@Ibz,欢迎您随时光临。SO论坛似乎存在问题,无法正确加载照片。因此,我在这里通过OneDrive分享测试结果:https://1drv.ms/u/s!Ai1sp_yvodHf2VjaF3gem2iYWd1X - Leo Liu
1
尽管以上命令行可以工作,但我发现after.<SolutionName>.sln.targets似乎被Visual Studio 2017忽略了,而且输出中没有出现"banana",请帮忙。 - Ibz
1
@Ibz,是的,看起来这个目标只能通过命令行触发。在参考文档中,我发现如果你正在从命令行构建解决方案文件,那么你可能会对这种技术感兴趣。所以,如果你仍然想在Visual Studio输出窗口中显示香蕉,你可以尝试使用我的答案中的第二种方法:创建一个单独的空项目,引用所有项目的子集并将此目标添加到空项目中 - Leo Liu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接