Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MsBuild PostBuild targets

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.

like image 831
Ibz Avatar asked Aug 07 '18 20:08

Ibz


People also ask

What are the common targets of MSBuild?

Common. targets is: BeforeRebuild , Clean , Build , and then AfterRebuild . Tasks that are inserted in one of these targets run before or after the core clean functionality is invoked. Tasks that are inserted in one of these targets run before or after the core publish functionality is invoked.

How do I run MSBuild target?

To build a specific target of a specific project in a solution. At the command line, type MSBuild.exe <SolutionName>. sln , where <SolutionName> corresponds to the file name of the solution that contains the target that you want to execute.

Will MSBuild compile without any target?

If there are no initial targets, default targets, or command-line targets, then MSBuild runs the first target it encounters in the project file or any imported project files.

Where is Microsoft common CurrentVersion targets?

NET projects are defined in the Microsoft. Common. CurrentVersion. targets file which can be found in the MSBuild bin directory.


1 Answers

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.

like image 84
Leo Liu-MSFT Avatar answered Oct 20 '22 16:10

Leo Liu-MSFT