MsBuild PostBuild targets
Asked Answered
P

1

6

I have added an AfterBuild target to a Visual Studio project, which is part of a solution containing multiple Visual Studio projects.

Example Solution setup

Example.sln

  • ExampleProj.csproj

  • ExampleProj.Test.csproj

Example of Target:

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

I would like this target to execute only after all of the Visual Studio projects in the solution have been built.

Is there a way to achieve this.

Note: I need the behaviour to be the same when using dotnet build as well as the build command in Visual Studio.

Penultimate answered 7/8, 2018 at 20:7 Comment(3)
What issue are you having with this?Yalta
The target runs tests (amongst other things) and I find that the target sometimes runs before the Test project has built it's assemblies, and so the tests fall. Note: I am only adding the target to the main project so that certain commands only occur once. I am trying to avoid having to add other targets to other projects as then the order of execution of targets across the projects becomes difficult to manage.Penultimate
I find the best way to reference projects among each other in a solution is to use project references. That way the projects themselves determine the build order, and not the solution file itself. If I were you, I would attach that post build target on the project that is always built last (If you have one).Rayon
J
17

I would like this target to execute only after all of the Visual Studio projects in the solution have been built

According to the document MSBuild Extending The Solution Build, you could create a MSBuild project files named after.<SolutionName>.sln.targets in the same folder as your solution.

As test, I added this to my After.Solution.sln.targets file (Use a banana instead of SomeApplication.exe), and set this file in the same folder as my solution file .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>

Then I build it with dotnet build command:

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

enter image description here

This target execute after all of the Visual Studio projects in the solution have been built.

Alternatively, you could also create separate empty project, referencing subset of all the projects and adding this target to the empty project.

Hope this helps.

Julesjuley answered 8/8, 2018 at 3:17 Comment(4)
Thank you for your response.Penultimate
@Ibz, You are always welcome. SO forum seems to have problems, photos can not be loaded correctly. So I share the test result by onedrive here:1drv.ms/u/s!Ai1sp_yvodHf2VjaF3gem2iYWd1XJulesjuley
While the above works in the command line, I am finding that the after.<SolutionName>.sln.targets appears to be ignored by Visual Studio 2017 and the banana does not show in the output. Please help.Penultimate
@Ibz, Yes, It seems this target only triggered by command line. In the referenced document, I found that If you are building solution files from the command then you might be interested in the technique.. So, if you still want to the banana shows in the Visual Studio output window, you can try to use the second method in my answer: create separate empty project, referencing subset of all the projects and adding this target to the empty project.Julesjuley

© 2022 - 2024 — McMap. All rights reserved.