All Articles

Cake build - Specify MSBuild version to be used for build

Today when I started on a setup for my project, I started seeing multiple errors on the build (which made no sense :| ). After some research, I found out that I needed the MSBuild 2019 version for this to work. Although I installed the Visual Studio Build Tools 2019 the build was still failing, as the build was using the MSBuild from my Visual Studio 2022.

The easiest solution was to uninstall Visual Studio 2022 and install Visual Studio 2019. But, I wanted a better way. I couldn’t lose VS 2022 so easily.

Error Description

The error message was as below:

MSBuild: error MSB4057: The target does not exist in the project

The error was only with Cake build, the build was successful on Visual Studio.

Solution for the error

I had to find the line in the script where MSBuild was being called. It looked somewhat like below.

Task("Build")
  .Does(() =>
{
    NuGetRestore(solution);
    MSBuild(solution, 
        (new MSBuildSettings {
            Verbosity = Verbosity.Minimal,
            Configuration = "Debug",
            PlatformTarget = PlatformTarget.MSIL,
            MaxCpuCount = 8,
            NodeReuse = true
        })
        .WithRestore()
        .WithTarget("Build"));
});

After going through the Cake build documentation, and a few many trials. I found a working solution.

Task("Build")
  .Does(() =>
{
    NuGetRestore(solution);
    MSBuild(solution, 
        (new MSBuildSettings {
            Verbosity = Verbosity.Minimal,
            Configuration = "Debug",
            PlatformTarget = PlatformTarget.MSIL,
            MaxCpuCount = 8,
            NodeReuse = true,
          //Add below line to specify MS Build Version
			ToolVersion = MSBuildToolVersion.VS2019
        })
        .WithRestore()
        .WithTarget("Build"));
});

Use this link to find out more options for the MSBuild version - Cake Build - MSBuildToolVersion.

Hoping this will be useful to some of you.

Happy Sitecoring!