Setting Up A New Cake .NET Tool Project

This is a guide to get started with Cake using the Cake .NET Tool and to show you how Cake works.

Choose your runner

Choose the runner suitable for your scenario. See Runners for other possibilities how to run Cake builds.

For this tutorial we use the Cake .NET Tool on .NET Core 3.1 and newer.

Make sure to have a tool manifest available in your repository or create one using the following command:

dotnet new tool-manifest

Install Cake as a local tool using the dotnet tool command (you can replace 5.0.0 with a different version of Cake you want to use):

dotnet tool install Cake.Tool --version 5.0.0

See Bootstrapping Cake .NET Tool for details about the bootstrapping process.

Create initial build script

Create a file called build.cake with the following content:

var target = Argument("target", "Test");
var configuration = Argument("configuration", "Release");

//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////

Task("Clean")
    .WithCriteria(c => HasArgument("rebuild"))
    .Does(() =>
{
    CleanDirectory($"./src/Example/bin/{configuration}");
});

Task("Build")
    .IsDependentOn("Clean")
    .Does(() =>
{
    DotNetBuild("./src/Example.sln", new DotNetBuildSettings
    {
        Configuration = configuration,
    });
});

Task("Test")
    .IsDependentOn("Build")
    .Does(() =>
{
    DotNetTest("./src/Example.sln", new DotNetTestSettings
    {
        Configuration = configuration,
        NoBuild = true,
    });
});

//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////

RunTarget(target);

Run build script

Run the build script using the .NET CLI:

dotnet cake