Search code examples
c#visual-studiomsbuildridercakebuild

How to set absolute path to MSBuild.exe in Cake scripts


I want to build my packages (in .NET Framework project) with Rider, using cake script. I want to use the MSBuild Tools under Rider directories instead of the Visual Studio MSBuild Tools directories. Although I couldn't find a way to provide cake an absolute path of the directories. Any ideas?

Task(CleanSolutionTargetName)
  .Does (() =>
    MSBuild(
        solutionFile,
        settings => settings
                    .SetConfiguration(BuildConfiguration)
                    .SetVerbosity (Verbosity.Quiet)
                    .WithProperty("Platform", SolutionPlatform)
                    .WithTarget("Clean")
                    .UseToolVersion(MSBuildToolVersion.VS2022));

        });

I tried to provide an absolute Tools path to the cake script, but nothing worked.


Solution

  • Overriding the path used by MSBuild alias can be done using the MSBuildSettings class either by using the WithToolPath extension method

    MSBuild(
        "./path/to/solution.sln",
        settings => settings
            .WithToolPath("path/to/msbuild.exe")
    );
    

    or using the MSBuildSettings ToolPath property.

    MSBuild(
        "./path/to/solution.sln",
        new MSBuildSettings
        {
            ToolPath = "path/to/msbuild.exe",
        }
    );
    

    You can read more about overriding MSBuild toolpath in the blog post VSWhere and Visual Studio 2017 Support on Cake's website.