Running Targets

To run a target, use the RunTarget method. The RunTarget method should be placed at the end of the script.

Task("Default")
    .Does(() =>
{
    Information("Hello World!");
});

RunTarget("Default");

Passing a target to the script

All arguments passed to Cake will also be accessible from the Cake script. You can access the arguments by using the argument DSL.

var target = Argument("target", "Build");

Task("Build")
    .Does(() =>
{
});

Task("Publish")
    .IsDependentOn("Build")
    .Does(() =>
{
});

RunTarget(target);

With this Cake script, you can run a specific target by passing the --target argument to Cake. Thus, we can run the "Publish" target by calling:

dotnet cake --target=Publish

On Windows:
./build.ps1 --target=Publish

On macOS & Linux:
build.sh --target=Publish

On Windows:
./build.ps1 --target=Publish

On macOS & Linux using Mono:
build.sh --target=Publish

On Windows:
./build.ps1 --target=Publish

On macOS & Linux:
build.sh --target=Publish

The --exclusive parameter causes RunTarget to run only the specified target and no dependencies. This command runs the Publish target without running the Build target:

dotnet cake --target=Publish --exclusive

On Windows:
./build.ps1 --target=Publish --exclusive

On macOS & Linux:
build.sh --target=Publish --exclusive

On Windows:
./build.ps1 --target=Publish --exclusive

On macOS & Linux using Mono:
./build.ps1 --target=Publish --exclusive

On Windows:
./build.ps1 --target=Publish --exclusive

On macOS & Linux:
build.sh --target=Publish --exclusive