Building .NET Solutions with Nuke Build

Introduction

In the world of .NET development, automating the build process is essential for maintaining a smooth and efficient workflow. One powerful tool that has gained popularity for building .NET solutions through code is Nuke Build. Nuke Build is an open-source, cross-platform build automation tool that allows you to define your build process entirely in C# code. In this article, we'll explore the fundamentals of Nuke Build and provide a step-by-step example of how to create and execute a .NET build script.

Why Use Nuke Build?

Before diving into the technical details, let's briefly discuss why you might choose Nuke Build for your .NET projects.

  1. Code-First Approach: Nuke Build allows you to define your build process in C# code, making it highly readable, maintainable, and flexible. You can leverage your existing knowledge of C# and .NET to create complex build workflows.
  2. Cross-Platform: Nuke Build is compatible with Windows, macOS, and Linux, ensuring your build scripts work seamlessly across different development environments.
  3. Extensibility: Nuke Build supports plugins, allowing you to easily integrate third-party tools and libraries into your build process. This extensibility makes it adaptable to a wide range of project requirements.
  4. Built-in Tasks: Nuke Build includes a wide variety of built-in tasks for common development operations, such as compiling code, running tests, creating NuGet packages, and more.
  5. Parallel Execution: Nuke Build can execute tasks in parallel, taking full advantage of multi-core processors to speed up your build process.

Now, let's dive into a practical example of creating a .NET build script using Nuke Build.

Getting Started with Nuke Build

To start using Nuke Build, you need to set up a new .NET project or use an existing one. Follow these steps to create a simple Nuke Build script:

Step 1. Create a .NET Solution

Create a new .NET solution or use an existing one. You can do this with the following command:

dotnet new sln -n MySolution

Step 2. Add Projects

Add one or more projects to your solution. For this example, let's assume you have a simple console application named "MyApp."

dotnet new console -n MyApp

Add this project to your solution:

dotnet sln add MyApp/MyApp.csproj

Step 3. Install Nuke.Global Tool

To use Nuke Build, you need to install the Nuke.Global .NET tool globally:

dotnet tool install -g Nuke.Global --version 6.0.0

Step 4. Create a Nuke Build Script

Create a new file named build.cs in your solution's root directory. This file will contain your Nuke Build script. Here's a minimal example:

using Nuke.Common;
using Nuke.Common.IO;
using Nuke.Common.Tools.DotNet;
using Nuke.Common.Tools.GitVersion;
using Nuke.Common.Tools.NuGet;
using Nuke.Common.Utilities.Collections;

class Build : NukeBuild
{
    readonly AbsolutePath SourceDirectory = RootDirectory / "MyApp";
    readonly AbsolutePath OutputDirectory = RootDirectory / "output";

    Target Clean => _ => _
        .Executes(() =>
        {
            SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
            EnsureCleanDirectory(OutputDirectory);
        });

    Target Restore => _ => _
        .DependsOn(Clean)
        .Executes(() =>
        {
            DotNetRestore(s => s
                .SetProjectFile(SourceDirectory / "MyApp.csproj"));
        });

    Target Compile => _ => _
        .DependsOn(Restore)
        .Executes(() =>
        {
            DotNetBuild(s => s
                .SetProjectFile(SourceDirectory / "MyApp.csproj")
                .SetConfiguration(Configuration.Release)
                .SetOutput(OutputDirectory / "app"));
        });

    Target Publish => _ => _
        .DependsOn(Compile)
        .Executes(() =>
        {
            DotNetPublish(s => s
                .SetProject(SourceDirectory / "MyApp.csproj")
                .SetOutput(OutputDirectory));
        });

    public static int Main() => Execute<Build>(x => x.Publish);
}

This simple Nuke Build script defines four targets: Clean, Restore, Compile, and Publish. The Clean target removes build artifacts, the Restore target restores dependencies, the Compile target builds the application, and the Publish target publishes the application.

Step 5. Run the Build

To execute the build script, run the following command in your project's root directory:

dotnet run -p build.cs

Nuke Build will execute the defined targets in the correct order.

Conclusion

Nuke Build is a powerful and flexible tool for automating the build process of .NET projects through code. It allows you to create highly customizable build scripts using C#, making it easier to manage and extend your build process. By following the steps outlined in this article, you can start automating your .NET builds with Nuke Build and streamline your development workflow. Happy coding!