LinuxCommandLibrary

msbuild

Build software projects using MSBuild project files

TLDR

Build the first project file in the current directory

$ msbuild
copy

Build a specific project file
$ msbuild [path/to/project_file]
copy

Specify one or more semicolon-separated targets to build
$ msbuild [path/to/project_file] /target:[targets]
copy

Specify one or more semicolon-separated properties
$ msbuild [path/to/project_file] /property:[name=value]
copy

Specify the build tools version to use
$ msbuild [path/to/project_file] /toolsversion:[version]
copy

Display detailed information at the end of the log about how the project was configured
$ msbuild [path/to/project_file] /detailedsummary
copy

Display help
$ msbuild /help
copy

SYNOPSIS

msbuild [project-or-solution-file] [options]

Examples:
msbuild MyProject.csproj
msbuild MySolution.sln -t:Clean;Build
msbuild -restore ./src/App
msbuild MyProject.csproj -p:Configuration=Release -v:minimal

PARAMETERS

-t, --target
    Specifies one or more build targets to execute, separated by semicolons. Common targets include Build, Clean, Rebuild, and Publish.

-p, --property
    Sets or overrides project properties. Use the format -p:PropertyName=Value. Can be specified multiple times.

-restore, --restore
    Restores NuGet packages required by the project or solution before building. This is often implicitly handled by dotnet build.

-nologo, --nologo
    Suppresses the display of the startup banner and copyright message.

-v, --verbosity
    Sets the verbosity level of the build output. Possible values include q (quiet), m (minimal), n (normal), d (detailed), and diag (diagnostic).

-force, --force
    Forces a build even if the project is up-to-date, bypassing incremental build checks.

-no-incremental
    Disables incremental build for all targets.

-m, --maxcpucount
    Specifies the maximum number of concurrent processes to use when building. 0 uses all available CPU cores.

DESCRIPTION

MSBuild, also known as the Microsoft Build Engine, is a free and open-source build platform for .NET applications. It provides an XML-based schema for project files that define how the build process should be handled, including tasks like compiling code, packaging applications, and deploying solutions. On Linux, msbuild is a crucial component of the .NET SDK, enabling developers to build, test, and package their .NET Core and .NET 5+ applications directly from the command line. It processes project files (e.g., .csproj, .fsproj) and solution files (.sln), understanding the dependencies and build instructions specified within them. While primarily used for C# and F# projects, its extensibility allows for a wide range of custom build scenarios. Its cross-platform availability ensures that .NET development workflows are consistent across different operating systems.

CAVEATS

MSBuild on Linux requires the .NET SDK to be installed and correctly configured. While highly capable, it primarily supports modern .NET Core and .NET 5+ projects. Some legacy .NET Framework project types or complex build configurations that heavily rely on Windows-specific features or third-party tools might not fully translate or function as expected in a Linux environment. Developers often use the higher-level dotnet CLI commands (e.g., dotnet build) which internally invoke MSBuild, providing a more streamlined experience.

PROJECT FILES AND TARGETS

MSBuild operates by parsing XML-based project files (e.g., .csproj, .vbproj) or solution files (.sln). These files contain properties, items, and most importantly, targets and tasks that define the build steps. A target is a named set of tasks that perform a specific build operation (e.g., Build, Clean, Publish). Developers can define custom targets and extend the build process using standard or custom tasks, providing immense flexibility for complex build scenarios. The project files themselves are typically generated by the .NET SDK or Visual Studio and are human-readable, allowing for direct modification.

INTEGRATION WITH .NET CLI

While msbuild can be invoked directly, most .NET developers on Linux interact with it indirectly through the dotnet command-line interface. Commands like dotnet build, dotnet publish, and dotnet restore are high-level wrappers that invoke MSBuild internally with appropriate default arguments and targets. This abstraction simplifies common development workflows, making it easier to manage .NET projects without needing to understand the intricate details of MSBuild syntax for everyday tasks. For advanced or highly customized build scenarios, directly using msbuild provides greater control.

HISTORY

MSBuild originated as the Microsoft Build Engine, an integral part of Visual Studio and the .NET Framework on Windows. Its primary purpose was to provide a robust, extensible XML-based platform for building software products. With the advent of .NET Core in 2016, Microsoft began open-sourcing significant portions of the .NET ecosystem, including MSBuild itself. This made it possible for MSBuild to run natively on Linux and macOS, becoming a cornerstone of cross-platform .NET development and enabling consistent build pipelines across different operating systems. Its evolution has focused on aligning with the .NET SDK and supporting modern project formats.

SEE ALSO

dotnet(1), make(1), cmake(1)

Copied to clipboard