Skip to main content

Build Anatomy

A build project is a regular .NET console application. However, unlike regular console applications, NUKE chooses to name the main class Build instead of Program. This establishes a convention and allows easier navigation in your solution. The Build class must inherit from the NukeBuild base class and define a Main method to invoke the build execution and define any number of default targets:

Build.cs
class Build : NukeBuild
{
public static int Main() => Execute<Build>(x => x.Compile);

// Target definitions
}
info

You will learn how to write target definitions in the next chapter.

Base Properties​

With the NukeBuild base class you gain access to a lot of properties that provide information about the build environment and build status:

NukeBuild.cs
abstract class NukeBuild
{
static Host Host { get; }
static bool IsLocalBuild { get; }
static bool IsServerBuild { get; }

static AbsolutePath RootDirectory { get; }
static AbsolutePath TemporaryDirectory { get; }

static AbsolutePath BuildAssemblyFile { get; }
static AbsolutePath BuildAssemblyDirectory { get; }
static AbsolutePath BuildProjectFile { get; }
static AbsolutePath BuildProjectDirectory { get; }
}
tip

With the Host property you can determine the running environment, for instance with Host is TeamCity. Make sure to explore other implementations of the Host base class through your IDE.


Since Host, IsLocalBuild, and IsServerBuild are static properties, you can conveniently use them in static conditions to skip targets (including their dependencies) in local or server builds.

info

Learn more about the AbsolutePath class and how it's used for path construction.

Build Events​

For implementing cross-cutting concerns, like telemetry and similar, you can hook into various build events:

NukeBuild.cs
abstract class NukeBuild
{
virtual void OnBuildCreated();
virtual void OnBuildInitialized();
virtual void OnBuildFinished();

virtual void OnTargetRunning(string target);
virtual void OnTargetSkipped(string target);
virtual void OnTargetFailed(string target);
virtual void OnTargetSucceeded(string target);
}