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

201 views Asked by At

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.

2

There are 2 answers

2
devlead On BEST ANSWER

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.

1
dododo On

I did something similar for nuget.exe:

        public const string NugetBinaryName = "nuget.exe";
        var nugetExePath = ..path..;
        cakeContext.Tools.RegisterFile(nugetExePath);

after this, the whole cake run uses the provided path when it's working with nuget.exe in internal steps. To get the saved data, you can use:

        cakeContext.Tools.Resolve(NugetBinaryName);