Search code examples
c#makefilemsbuildcakebuild

Cake MSBuild setting properties


I have a batch file that I am trying to replicate with Cake (C# Make). It makes a call to MSBuild with a few properties. Here is the line from the batch;

"%MSBuildPath%msbuild.exe" ..\public\projectToBeBuilt.sln /t:Rebuild /p:Configuration=RELEASE;platform=%platform% /maxcpucount:%cpucount% /v:%verboselevel%

These are the properties I need to set. I think its something like this;

MSBuild(@"..\public\projectToBeBuilt.sln", s=> s.SetConfiguration("Release")
    .UseToolVersion(MSBuildToolVersion.Default)
    .WithProperty("Verbosity", Verbosity)
    .WithProperty("MaxCpuCount", cpuCount)
    .WithProperty("Platform", "x64")
    .WithProperty("OutDir", buildDir));

I'm having trouble making this work. I think it may be something to do with how I'm designating the cpu count. I also cant find any way to set it to Rebuild, the way the batch does it.


Solution

  • What kind of error are you getting?

    To rebuild like you do in your batch example you would set target using WithTarget like this

    .WithTarget("Rebuild")
    

    Regarding CPU count I have no issue if I set like this

    .SetMaxCpuCount(System.Environment.ProcessorCount)
    

    Setting platform would look something like this

    .SetPlatformTarget(PlatformTarget.x64)
    

    Setting verbosity would be

    .SetVerbosity(Verbosity)
    

    So a complete command could look like

    MSBuild(solution, settings =>
        settings.SetConfiguration("Release")
            .UseToolVersion(MSBuildToolVersion.Default)
            .WithTarget("Rebuild")
            .SetMaxCpuCount(cpuCount)
            .SetPlatformTarget(PlatformTarget.x64)
            .SetVerbosity(Verbosity)
            .WithProperty("OutDir", buildDir)
            );
    

    The fluent API methods for the MSBuild settings are documented here.