0

I created a C# console application with Visual Studio 2022 that just prints a hello world on the console for testing purposes. Now I am trying to build the project using MSBuild directly on the command line.

As far as I understood, Visual Studio uses MSBuild as well, so at first I tried to just execute:

"c:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"  LeoBuildToolsTest.sln

which reports an error regarding wrong XML namespace. After I fixed that, there's a problem regarding missing targets, which is obvious, as there are none:

<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>
</Project>

I was able to get the project build by MSBuild but only with my manually written testproject.proj file. So I want to ask whether there is a way to automatically generate a MSBuild proj file by VS that be used directly as input? Or do I always have to write it by myself?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mrleop
  • 33
  • 5
  • 1
    MSBuild 14? Visual Studio 2022? Something doesn't add up here. MSBuild 14 is from the era of Visual Studio 2015, a time predating the SDK-style project files used today... –  Dec 18 '22 at 21:44
  • 1
    What version of MSBuild are you using? If you use the msbuild executable that comes with the Visual Studio that generates the solution or project, everything should work as expected, Make sure to provide the full path of the file, not only its name – NineBerry Dec 18 '22 at 21:45

1 Answers1

1

To locate the proper msbuild.exe, you could use:

for /f "usebackq tokens=1* delims=: " %%i in (`vswhere -latest -requires Microsoft.Component.MSBuild`) do (
  if /i "%%i"=="installationPath" set InstallDir=%%j
)

::  https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-command-line-reference?view=vs-2019
echo InstallDir=%InstallDir%
if exist "%InstallDir%\MSBuild\Current\Bin\MSBuild.exe" (
  "%InstallDir%\MSBuild\Current\Bin\MSBuild.exe" -nologo -verbosity:normal yourSolution.sln -property:Configuration=Release
) else (
echo.
echo MSBuild not found!
echo expected in "%InstallDir%\MSBuild\Current\Bin\MSBuild.exe"
echo.
)

Path to vswhere.exe on my system is

"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"
Axel Kemper
  • 10,544
  • 2
  • 31
  • 54