dotnet build command - .NET CLI (2023)

  • Article
  • 6 minutes to read

This article applies to: ✔️ .NET Core 3.1 SDK and later versions

Name

dotnet build - Builds a project and all of its dependencies.

Synopsis

dotnet build [<PROJECT>|<SOLUTION>] [-a|--arch <ARCHITECTURE>] [-c|--configuration <CONFIGURATION>] [-f|--framework <FRAMEWORK>] [--force] [--interactive] [--no-dependencies] [--no-incremental] [--no-restore] [--nologo] [--no-self-contained] [--os <OS>] [-o|--output <OUTPUT_DIRECTORY>] [-r|--runtime <RUNTIME_IDENTIFIER>] [--self-contained [true|false]] [--source <SOURCE>] [--use-current-runtime, --ucr [true|false]] [-v|--verbosity <LEVEL>] [--version-suffix <VERSION_SUFFIX>]dotnet build -h|--help

Description

The dotnet build command builds the project and its dependencies into a set of binaries. The binaries include the project's code in Intermediate Language (IL) files with a .dll extension. Depending on the project type and settings, other files may be included, such as:

  • An executable that can be used to run the application, if the project type is an executable targeting .NET Core 3.0 or later.
  • Symbol files used for debugging with a .pdb extension.
  • A .deps.json file, which lists the dependencies of the application or library.
  • A .runtimeconfig.json file, which specifies the shared runtime and its version for an application.
  • Other libraries that the project depends on (via project references or NuGet package references).

For executable projects targeting versions earlier than .NET Core 3.0, library dependencies from NuGet are typically NOT copied to the output folder. They're resolved from the NuGet global packages folder at run time. With that in mind, the product of dotnet build isn't ready to be transferred to another machine to run. To create a version of the application that can be deployed, you need to publish it (for example, with the dotnet publish command). For more information, see .NET Application Deployment.

For executable projects targeting .NET Core 3.0 and later, library dependencies are copied to the output folder. This means that if there isn't any other publish-specific logic (such as Web projects have), the build output should be deployable.

Implicit restore

Building requires the project.assets.json file, which lists the dependencies of your application. The file is created when dotnet restore is executed. Without the assets file in place, the tooling can't resolve reference assemblies, which results in errors.

You don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack. To disable implicit restore, use the --no-restore option.

The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

For information about how to manage NuGet feeds, see the dotnet restore documentation.

This command supports the dotnet restore options when passed in the long form (for example, --source). Short form options, such as -s, are not supported.

Executable or library output

Whether the project is executable or not is determined by the <OutputType> property in the project file. The following example shows a project that produces executable code:

(Video) Intro To The .NET CLI - How To Use It, Why We Need It, And More

<PropertyGroup> <OutputType>Exe</OutputType></PropertyGroup>

To produce a library, omit the <OutputType> property or change its value to Library. The IL DLL for a library doesn't contain entry points and can't be executed.

MSBuild

dotnet build uses MSBuild to build the project, so it supports both parallel and incremental builds. For more information, see Incremental Builds.

In addition to its options, the dotnet build command accepts MSBuild options, such as -p for setting properties or -l to define a logger. For more information about these options, see the MSBuild Command-Line Reference. Or you can also use the dotnet msbuild command.

Note

When dotnet build is run automatically by dotnet run, arguments like -property:property=value aren't respected.

Running dotnet build is equivalent to running dotnet msbuild -restore; however, the default verbosity of the output is different.

Workload manifest downloads

When you run this command, it initiates an asynchronous background download of advertising manifests for workloads. If the download is still running when this command finishes, the download is stopped. For more information, see Advertising manifests.

Arguments

PROJECT | SOLUTION

The project or solution file to build. If a project or solution file isn't specified, MSBuild searches the current working directory for a file that has a file extension that ends in either proj or sln and uses that file.

Options

  • -a|--arch <ARCHITECTURE>

    Specifies the target architecture. This is a shorthand syntax for setting the Runtime Identifier (RID), where the provided value is combined with the default RID. For example, on a win-x64 machine, specifying --arch x86 sets the RID to win-x86. If you use this option, don't use the -r|--runtime option. Available since .NET 6 Preview 7.

  • -c|--configuration <CONFIGURATION>

    (Video) Learn Command Line Interface (CLI) for Build, Run and Publish in .NET Core | Infologs

    Defines the build configuration. The default for most projects is Debug, but you can override the build configuration settings in your project.

  • -f|--framework <FRAMEWORK>

    Compiles for a specific framework. The framework must be defined in the project file. Examples: net7.0, net462.

  • --force

    Forces all dependencies to be resolved even if the last restore was successful. Specifying this flag is the same as deleting the project.assets.json file.

  • -?|-h|--help

    Prints out a description of how to use the command.

  • --interactive

    Allows the command to stop and wait for user input or action. For example, to complete authentication. Available since .NET Core 3.0 SDK.

  • --no-dependencies

    Ignores project-to-project (P2P) references and only builds the specified root project.

  • --no-incremental

    Marks the build as unsafe for incremental build. This flag turns off incremental compilation and forces a clean rebuild of the project's dependency graph.

    (Video) How to create your own .NET CLI tools to make your life easier

  • --no-restore

    Doesn't execute an implicit restore during build.

  • --nologo

    Doesn't display the startup banner or the copyright message.

  • --no-self-contained

    Publishes the application as a framework dependent application. A compatible .NET runtime must be installed on the target machine to run the application. Available since .NET 6 SDK.

  • -o|--output <OUTPUT_DIRECTORY>

    Directory in which to place the built binaries. If not specified, the default path is ./bin/<configuration>/<framework>/. For projects with multiple target frameworks (via the TargetFrameworks property), you also need to define --framework when you specify this option.

  • --os <OS>

    Specifies the target operating system (OS). This is a shorthand syntax for setting the Runtime Identifier (RID), where the provided value is combined with the default RID. For example, on a win-x64 machine, specifying --os linux sets the RID to linux-x64. If you use this option, don't use the -r|--runtime option. Available since .NET 6.

  • -r|--runtime <RUNTIME_IDENTIFIER>

    Specifies the target runtime. For a list of Runtime Identifiers (RIDs), see the RID catalog. If you use this option with .NET 6 SDK, use --self-contained or --no-self-contained also. If not specified, the default is to build for the current OS and architecture.

  • --self-contained [true|false]

    (Video) Create new Solution & Projects using Dotnet CLI

    Publishes the .NET runtime with the application so the runtime doesn't need to be installed on the target machine. The default is true if a runtime identifier is specified. Available since .NET 6 SDK.

  • --source <SOURCE>

    The URI of the NuGet package source to use during the restore operation.

-v|--verbosity <LEVEL>

Sets the verbosity level of the command. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]. The default is minimal. By default, MSBuild displays warnings and errors at all verbosity levels. To exclude warnings, use /property:WarningLevel=0. For more information, see LoggerVerbosity and WarningLevel.

  • --use-current-runtime, --ucr [true|false]

    Sets the RuntimeIdentifier to a platform portable RuntimeIdentifier based on the one of your machine. This happens implicitly with properties that require a RuntimeIdentifier, such as SelfContained, PublishAot, PublishSelfContained, PublishSingleFile, and PublishReadyToRun. If the property is set to false, that implicit resolution will no longer occur.

  • --version-suffix <VERSION_SUFFIX>

    Sets the value of the $(VersionSuffix) property to use when building the project. This only works if the $(Version) property isn't set. Then, $(Version) is set to the $(VersionPrefix) combined with the $(VersionSuffix), separated by a dash.

Examples

  • Build a project and its dependencies:

    dotnet build
  • Build a project and its dependencies using Release configuration:

    dotnet build --configuration Release
  • Build a project and its dependencies for a specific runtime (in this example, Ubuntu 18.04):

    dotnet build --runtime ubuntu.18.04-x64
  • Build the project and use the specified NuGet package source during the restore operation:

    (Video) Dotnet CLI - Build, Test and Publish projects

    dotnet build --source c:\packages\mypackages
  • Build the project and set version 1.2.3.4 as a build parameter using the -p MSBuild option:

    dotnet build -p:Version=1.2.3.4

FAQs

What is .NET CLI command? ›

The . NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing . NET applications.

What does dotnet build command do? ›

The dotnet build command builds the project and its dependencies into a set of binaries. The binaries include the project's code in Intermediate Language (IL) files with a . dll extension.

How do you pass command line arguments in dotnet run? ›

To pass arguments to your application, you need to pass a -- argument, and then the arguments to your application. As per the . NET Core CLI documentation, -- delimits arguments to dotnet run from arguments for the application being run. All arguments after this one are passed to the application run.

What is Mvn build command? ›

To build a Maven project via the command line, you use the mvn command from the command line. The command must be executed in the directory which contains the relevant pom file. You pass the build life cycle, phase or goal as parameter to this command.

How do CLI commands work? ›

Through the CLI, users interact with a system or application by typing in text (commands). The command is typed on a specific line following a visual prompt from the computer. The system responds to the text, and the user may then type on the next command line that appears.

What is the use of build command? ›

The build command is used to build an image from a Dockerfile, but the command has to be run in the same directory as the Dockerfile. When an image is built, the commands specified in the Dockerfile are executed. The operating system is installed​ along with all the packages required in the Docker container.

How do I pass a command line argument in terminal? ›

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

Why do we use Maven build? ›

The Need for Maven

Maven is chiefly used for Java-based projects, helping to download dependencies, which refers to the libraries or JAR files. The tool helps get the right JAR files for each project as there may be different versions of separate packages.

Is .NET Core difficult? ›

NET Core apps is much easier compared to . NET Framework apps. It supports side-by-side versioning, which means you can have multiple versions of . NET Core on the same machine without any conflicts.

What is CLI example? ›

The MS-DOS operating system and the command shell in the Windows operating system are examples of command-line interfaces. In addition, programming language development platforms such as Python can support command-line interfaces.

What are the three 3 CLI modes? ›

- [Instructor] The Cisco Inner Operating System, the IOS has three command line modes, User EXEC mode, or user mode, Privileged EXEC mode, or privileged mode, and then the Global Configuration mode.

What are two 3 common methods to access the CLI? ›

Generally, you can access the CLI through a direct connection to the console port, or remotely using Telnet or SSH command.

What is CLI code? ›

Visual Studio Code has a powerful command-line interface built-in that lets you control how you launch the editor. You can open files, install extensions, change the display language, and output diagnostics through command-line options (switches).

Why is CLI difficult? ›

CLI - Due to a higher degree of memorization and familiarity needed for operation and navigation, new users find operating a command line interface more difficult than a GUI. GUI - Because a GUI is visually intuitive, users learn how to use a GUI faster than a CLI.

What is a CLI for dummies? ›

CLI is a command line program that accepts text input to execute operating system functions. In the 1960s, using only computer terminals, this was the only way to interact with computers. In the 1970s an 1980s, command line input was commonly used by Unix systems and PC systems like MS-DOS and Apple DOS.

Why do people still use CLI? ›

One of the main advantages of a command line interface is that it allows users to type in commands that can produce immediate results. Those familiar with command-lines find this method quick and simple. Tasks can also be automated using a batch file, for example. A CLI can also enhance security.

How to run .NET core Web API from command line? ›

Type dotnet new webapi –name MyFirstWebAPIProject command and press enter in the command prompt as shown in the below image. Once you type dotnet new webapi –name MyFirstWebAPIProject and press enter, you will get the below output saying that the template ASP.NET Core Web API was created successfully.

How do I access .NET CLI in Visual Studio? ›

You can run the generated C# code projects with Visual Studio by pressing the F5 key or with dotnet run (. NET CLI).

Can you use += in C#? ›

You can also use the + and += operators to append a string to the value of a string variable. This is illustrated in the last two examples in this figure. Notice that when you use the + operator, you include the string variable in the expression that you're assigning to this variable.

Can you do += in C#? ›

+= operator adds the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left. -= operator subtracts the value of the variable on the left with the value on the right, which is (result) then assigned to the variable that is on the left.

Is it possible to pass command line arguments to the test execution? ›

It is possible to pass custom command line arguments to the test module.

How to pass value to shell script? ›

Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2 , and so on. The $0 will contain the script name.

Is it possible to pass command line arguments to a test? ›

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.

What is command line argument example? ›

Command line arguments allow the user to affect the operation of an application. For example, an application might allow the user to specify verbose mode--that is, specify that the application display a lot of trace information--with the command line argument -verbose .

How do you use command line arguments in a script? ›

This is the shell script which we plan to use for this purpose. #!/bin/sh echo "Script Name: $0" echo "First Parameter of the script is $1" echo "The second Parameter is $2" echo "The complete list of arguments is $@" echo "Total Number of Parameters: $#" echo "The process ID is $$" echo "Exit code for the script: $?"

Can you do Internet speed test from cmd? ›

In Windows, click the start menu and type "CMD" to access the command prompt. Type "ping google.com" and choose "enter" to execute the function and retrieve a list of results. The results have several lines of data that show the speed along with other metrics.

How to see all IP addresses on network using cmd? ›

First, click on your Start Menu and type cmd in the search box and press enter. A black and white window will open where you will type ipconfig /all and press enter. There is a space between the command ipconfig and the switch of /all.

Is cmd ping test accurate? ›

Ping Test is fast and accurate tool for quality measurements of the Internet connection. It checks delays in millisecond between your computer and selecter remote server. The ping value strongly depends on the distance to the server - the bigger distance the ping value is higher.

What is make CLI? ›

Super easy declarative CLI framework with a single configuration object and a single function call. There are so many command line interface libraries around that it's hard to find the right one for your needs.

Can I use cmd as coding? ›

No CMD can't be consider as programming language. CMD can be use to run program created in programming languages.

What is make command in cmd? ›

The make command assists in maintaining a set of programs, usually pertaining to a particular software project, by building up-to-date versions of programs. The make command is most useful for medium-sized programming projects.

Videos

1. Install and Use a NuGet Package with the .NET CLI | NuGet 101 [3 of 5]
(dotnet)
2. Using System.CommandLine to Build CLI Apps
(Muhammad Rehan Saeed)
3. Create a console application using .NET command line / CLI
(Learn Now!)
4. .NET Core Command Line Interface (CLI) | Learn the Basics
(CodeOpinion)
5. Create .NET Core Projects with the Command Line
(Microsoft Visual Studio)
6. Create and Publish a NuGet Package with the .NET CLI | NuGet 101 [5 of 5]
(dotnet)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated: 23/06/2023

Views: 6218

Rating: 4.7 / 5 (77 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.