Cake MSBuild setting properties
Asked Answered
E

1

6

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.

Evyn answered 10/7, 2017 at 15:13 Comment(0)
S
10

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.

Stoa answered 10/7, 2017 at 21:46 Comment(2)
Thanks! That looks like exactly what I was looking for. I couldn't find an example of the .WithTarget() method anywhere. My workaround was creating a "Clean" task that the MSBuild task depended on. This purged the bin folder, forcing a fresh build. I figured this would have about the same result. I did figure out that using .SetMaxCpuCount(0) will use the maximum number of processors available. Thanks again for the help!Evyn
SetMaxCpuCount to 0 will cause to use the number of CPUs available in the Agent too.Vaish

© 2022 - 2024 — McMap. All rights reserved.