Batch build .net solution using command line

344 views Asked by At

My Visual Studio solution consists of over 40 projects. Each project has two configurations (Debug,Release) and platforms (Win32/x64/Any CPU), probably total combinations of about 200.

I have just done a batch build using Visual Studio by selecting all and the build is successful with the output as below

========== Build: 200 succeeded, 0 failed, 60 up-to-date, 16 skipped ==========
========== Build started at 7:01 PM and took 14:01.354 minutes ==========

Now I am trying to achieve the same thing without Visual Studio through a batch file.

Approach 1:

@echo off
cls
msbuild.exe "solutionpath\solutionfile.sln" 
pause

Approach 2:

@echo off
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Debug /p:Platform="Any CPU"
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Debug /p:Platform="x64"
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Debug /p:Platform="Win32"
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Release /p:Platform="Any CPU"
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Release /p:Platform="x64"
msbuild.exe "solutionpath\solutionfile.sln" /p:Configuration=Release /p:Platform="Win32"
pause

I am getting the build as succeeded, but I am not sure how many projects it has actually built through command line. There is no information on total number of projects built.

  1. Does approach 2 above covers both configurations and all platforms as did by Visual Studio?
  2. Is there any way to get the total number of projects built? I just need to compare with the output from Visual Studio
1

There are 1 answers

0
Tatranskymedved On

Question 1:

Does approach 2 covers both configurations and all platforms as did by visual studio?

No. Usually, when new project is created, there are automatically added Configurations named Debug and Release and there are 3 Platforms Any CPU, x64, Win32. Those are just default values and it often happens that developers modify those depending on their needs - sometimes they delete those platforms, sometimes they add 5 more Configuration types and add various pre-build and post-build actions depending on value that is selected.

If you would like to iterate over all the Configurations and all the Platforms, the only possible way (I'm aware of) is to inspect all *.csproj files and check what Configurations and Platforms they have defined - make a mapping of that for each Project-Platforms-Configurations, and run the MSbuild for each combination available.

However, you usually don't want to perform this - e.g. those defaults value might be there, but you are never using them. If all those applications are Sever ASP.NET core, you will most likely run them on x64 platform only - so that you don't really care about build of those other defaults.

Also approach 1 (without defining the Configuration and Platform) is basically the same as single line from Approach 2. Those values are automatically set to the defaults - so each *.csproj is build just single time in default Configuration/Platform.


Question 2:

Is there anyway to get the total number of projects built? I just need to compare with the output from visual studio

MSbuild in itself does not contain any kind of "counter" for this stuff that you can easily output. What you are seeing is most likely Visual Studio calling the MSbuild and once the run is finished, it parses MSbuild's output and provides you with the summary.


I don't have complete answer for this, but one of many workarounds is to write own application (e.g. in C#) that would be handling those builds for you instead. Similarly to e.g. this answer: https://stackoverflow.com/a/51383006/7167572


If you want to run it straightforward from cmdline, MSbuild can be run with arguments for minimal verbosity that outputs just "build projects". But the output also includes error messages. Argument for that is -v:m or -verbosity:minimal as per docs.

Mentioned that, it works nicely if there are no errors:

C:\AdventOfCode\AdventOfCode> MSBuild.exe .\AdventOfCode.sln -v:m
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  AOC.Common -> C:\AdventOfCode\AdventOfCode\AOC.Common\bin\Debug\netcoreapp3.1\AOC.Common.dll
  AOC.2020 -> C:\AdventOfCode\AdventOfCode\AOC.2020\bin\Debug\netcoreapp3.1\AOC.2020.dll
  AOC.2021 -> C:\AdventOfCode\AdventOfCode\AOC.2021\bin\Debug\netcoreapp3.1\AOC.2021.dll
  AOC.2022 -> C:\AdventOfCode\AdventOfCode\AOC.2022\bin\Debug\netcoreapp3.1\AOC.2022.dll

Which you can pipeline through to cmdline's command find that will count those:

C:\AdventOfCode\AdventOfCode> MSBuild.exe .\AdventOfCode.sln -v:m | find /c "AdventOfCode"
4

There is an issue however, if you would have some errors during the build, the output could look like this:


C:\AdventOfCode\AdventOfCode> MSBuild.exe .\AdventOfCode.sln -v:m
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  AOC.Common -> C:\AdventOfCode\AdventOfCode\AOC.Common\bin\Debug\netcoreapp3.1\AOC.Common.dll
  AOC.2020 -> C:\AdventOfCode\AdventOfCode\AOC.2020\bin\Debug\netcoreapp3.1\AOC.2020.dll
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day18\Day18.cs(59,16): error CS0029: Typ int nejde implicitne prevést na typ bool. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day18\Day18.cs(61,13): error CS1023: Vloženým príkazem nemuže být deklarace ani príkaz s návestím. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day18\Day18.cs(70,22): error CS0103: Název resJustNumbers v aktuálním kontextu neexistuje. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day18\Day18.cs(72,37): error CS0103: Název resJustNumbers v aktuálním kontextu neexistuje. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day18\Day18.cs(73,37): error CS0103: Název resJustNumbers v aktuálním kontextu neexistuje. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day16\Day16.cs(102,21): warning CS0675: Logický bitový operátor or se použil pro operand s rozšíreným podpisem. Zvažte nejprve možnost pretypování na menší nepodepsaný typ. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day16\Day16.cs(103,21): warning CS0675: Logický bitový operátor or se použil pro operand s rozšíreným podpisem. Zvažte nejprve možnost pretypování na menší nepodepsaný typ. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day16\Day16.cs(104,21): warning CS0675: Logický bitový operátor or se použil pro operand s rozšíreným podpisem. Zvažte nejprve možnost pretypování na menší nepodepsaný typ. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]
C:\AdventOfCode\AdventOfCode\AOC.2021\Days\Day16\Day16.cs(105,21): warning CS0675: Logický bitový operátor or se použil pro operand s rozšíreným podpisem. Zvažte nejprve možnost pretypování na menší nepodepsaný typ. [C:\AdventOfCode\AdventOfCode\AOC.2021\AOC.2021.csproj]

And our code would look:

C:\AdventOfCode\AdventOfCode> MSBuild.exe .\AdventOfCode.sln -v:m | find /c "AdventOfCode"
12

... so there must be some additional handling - ideally output the MSbuild to some file + check whether the return code from MSbuild is 0 or 1 (it is 1 when Error occured during build) and following that you decide with your logic ~ what you want to do.