Cake MSBuild Targets

1.4k Views Asked by At

I am trying to use Cake's built in MSBuild functionality to only build a specific Target (i.e. Compile). Using the example at: https://cakebuild.net/api/Cake.Common.Tools.MSBuild/MSBuildAliases/C240F0FB

var settings = new MSBuildSettings()
{
    Verbosity = Verbosity.Diagnostic,
    ToolVersion = MSBuildToolVersion.VS2017,
    Configuration = "Release",
    PlatformTarget = PlatformTarget.MSIL
};
settings.WithTarget("Compile");

MSBuild("./src/Cake.sln", settings);

But it seems to build all targets, where as i would like to only build a specific target, as detailed in: https://msdn.microsoft.com/en-us/library/ms171486.aspx

1

There are 1 best solutions below

3
Gary Ewan Park On

As per the documentation here:

https://cakebuild.net/api/Cake.Common.Tools.MSBuild/MSBuildSettingsExtensions/01F8DC03

The WithTarget extension method returns the same instance of the MSBuildSettings with the modifications, it doesn't interact with the current instance. As a result, where you have:

settings.WithTarget("Compile");

Is actually not doing anything. However, if you do this:

var settings = new MSBuildSettings()
{
    Verbosity = Verbosity.Diagnostic,
    ToolVersion = MSBuildToolVersion.VS2017,
    Configuration = "Release",
    PlatformTarget = PlatformTarget.MSIL
};

MSBuild("./src/Cake.sln", settings.WithTarget("Compile");

It should work how you intend it.

To help with this sort of thing, you can run Cake in diagnostic mode, to see exactly what command is being sent to the command line for execution. You can find more about that in this related question:

How to enable diagnostic verbosity for Cake