You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/msbuild/walkthrough-using-msbuild.md
+25-29Lines changed: 25 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "MSBuild Tutorial: Install and create a project"
3
3
description: Explore the various parts of an MSBuild project file, including items, item metadata, properties, targets, and build tasks.
4
-
ms.date: 10/17/2023
4
+
ms.date: 10/3/2024
5
5
ms.topic: tutorial
6
6
helpviewer_keywords:
7
7
- MSBuild, tutorial
@@ -40,7 +40,7 @@ If you have Visual Studio, then you already have MSBuild installed. With Visual
40
40
41
41
In the Visual Studio installer, navigate to **Individual Components**, and locate the checkbox for **MSBuild**. It's automatically selected when you choose any of the other workloads to install.
42
42
43
-
To install MSBuild on a system that doesn't have Visual Studio, go to Build Tools for Visual Studio 2022 on the [downloads page](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta). Another way of getting MSBuild is to install the [.NET SDK](/dotnet/core/sdk#acquiring-the-net-sdk).
43
+
To install MSBuild on a system that doesn't have Visual Studio, go to **Build Tools for Visual Studio 2022** on the [downloads page](https://visualstudio.microsoft.com/downloads/?cid=learn-onpage-download-cta). Another way of getting MSBuild is to install the [.NET SDK](/dotnet/core/sdk#acquiring-the-net-sdk).
44
44
45
45
::: moniker-end
46
46
@@ -79,12 +79,7 @@ To install MSBuild on a system that doesn't have Visual Studio, go to Build Tool
79
79
80
80
Project files are XML-formatted files with the root node [Project](../msbuild/project-element-msbuild.md).
Most .NET projects have an `Sdk` attribute. These projects are called SDK-style projects.
82
+
Most .NET projects have an `Sdk` attribute. These projects are called SDK-style projects. Referencing an SDK means MSBuild imports a set of files that provide the build infrastructure for that SDK. If you don't reference any SDK, you can still use MSBuild, you just won't automatically have all the SDK-specific properties and targets available to you.
88
83
89
84
```xml
90
85
<ProjectSdk="Microsoft.NET.Sdk">
@@ -129,21 +124,22 @@ MSBuild keeps track of the targets of a build, and guarantees that each target i
The `Message` task is one of the many tasks that ships with MSBuild. For a complete list of available tasks and usage information, see [Task reference](../msbuild/msbuild-task-reference.md).
139
135
140
-
The `Message` task takes the string value of the Text attribute as input and displays it on the output device (or writes it to one or more logs, if applicable). The HelloWorld target executes the Message task twice: first to display "Hello", and then to display "World."
136
+
The `Message` task takes the string value of the `Text` attribute as input and displays it on the output device (or writes it to one or more logs, if applicable). The `HelloWorld` target executes the Message task twice: first to display "Hello", and then to display "World."
141
137
142
138
## Build the target
143
139
144
140
If you try to build this project from Visual Studio, it doesn't build the target you defined. That's because Visual Studio chooses the default target, which is still the one in the imported `.targets` file.
145
141
146
-
Run MSBuild from the **Developer Command Prompt** for Visual Studio to build the HelloWorld target defined previously. Use the -target or -t command-line switch to select the target.
142
+
Run MSBuild from the **Developer Command Prompt** for Visual Studio to build the HelloWorld target defined previously. Use the `-target` or `-t` command-line switch to select the target.
147
143
148
144
> [!NOTE]
149
145
> We will refer to the **Developer Command Prompt** as the **Command Window** in the following sections.
@@ -152,7 +148,7 @@ Run MSBuild from the **Developer Command Prompt** for Visual Studio to build the
152
148
153
149
1. Open the **Command Window**.
154
150
155
-
In the search box on the taskbar, start typing the name of the tool, such as `dev` or `developer command prompt`. This brings up a list of installed apps that match your search pattern.
151
+
In the search box on the taskbar, start typing the name of the tool, such as `dev` or `developer command prompt`. A list of installed apps that match your search pattern appears.
156
152
157
153
If you need to find it manually, the file is *LaunchDevCmd.bat* in the *{Visual Studio installation folder}\Common7\Tools* folder.
158
154
@@ -194,18 +190,18 @@ Run MSBuild from the **Developer Command Prompt** for Visual Studio to build the
194
190
All properties are child elements of PropertyGroup elements. The name of the property is the name of the child element, and the value of the property is the text element of the child element. For example,
appears later in the project file, or in a file imported later in the project file, then TargetFrameworkVersion takes the new value "v3.5."
204
+
appears later in the project file, or in a file imported later in the project file, then `TargetFrameworkVersion` takes the new value "net6.0"
209
205
210
206
## Examine a property value
211
207
@@ -261,24 +257,24 @@ Use this syntax to examine some of the properties in the project file.
261
257
Many properties like `Configuration` are defined conditionally, that is, the `Condition` attribute appears in the property element. Conditional properties are defined or redefined only if the condition evaluates to "true." Undefined properties are given the default value of an empty string. For example,
means "If the Configuration property hasn't been defined yet, define it and give it the value 'Debug'."
268
264
269
-
Almost all MSBuild elements can have a Condition attribute. For more discussion about using the Condition attribute, see [Conditions](../msbuild/msbuild-conditions.md).
265
+
Almost all MSBuild elements can have a `Condition` attribute. For more discussion about using the `Condition` attribute, see [Conditions](../msbuild/msbuild-conditions.md).
270
266
271
267
### Reserved properties
272
268
273
-
MSBuild reserves some property names to store information about the project file and the MSBuild binaries. MSBuildToolsPath is an example of a reserved property. Reserved properties are referenced with the $ notation like any other property. For more information, see [How to: Reference the name or location of the project file](../msbuild/how-to-reference-the-name-or-location-of-the-project-file.md) and [MSBuild reserved and well-known properties](../msbuild/msbuild-reserved-and-well-known-properties.md).
269
+
MSBuild reserves some property names to store information about the project file and the MSBuild binaries. MSBuildToolsPath is an example of a reserved property. Reserved properties are referenced with the `$` notation like any other property. For more information, see [How to: Reference the name or location of the project file](../msbuild/how-to-reference-the-name-or-location-of-the-project-file.md) and [MSBuild reserved and well-known properties](../msbuild/msbuild-reserved-and-well-known-properties.md).
274
270
275
271
### Environment variables
276
272
277
-
You can reference environment variables in project files the same way as build properties. For example, to use the PATH environment variable in your project file, use $(Path). If the project contains a property definition that has the same name as an environment variable, the property in the project overrides the value of the environment variable. For more information, see [How to: Use environment variables in a build](../msbuild/how-to-use-environment-variables-in-a-build.md).
273
+
You can reference environment variables in project files the same way as build properties. For example, to use the `PATH` environment variable in your project file, use `$(Path`). If the project contains a property definition that has the same name as an environment variable, the property in the project overrides the value of the environment variable. For more information, see [How to: Use environment variables in a build](../msbuild/how-to-use-environment-variables-in-a-build.md).
278
274
279
275
## Set properties from the command line
280
276
281
-
Properties can be defined on the command line using the -property or -p command line switch. Property values received from the command line override property values set in the project file and environment variables.
277
+
Properties can be defined on the command line using the `-property` or `-p` command line switch. Property values received from the command line override property values set in the project file and environment variables.
282
278
283
279
**To set a property value from the command line:**
284
280
@@ -298,7 +294,7 @@ MSBuild creates the Configuration property and gives it the value "Release."
298
294
299
295
## Special characters
300
296
301
-
Certain characters have special meaning in MSBuild project files. Examples of these characters include semicolons (;) and asterisks (*). In order to use these special characters as literals in a project file, they must be specified by using the syntax %\<xx>, where \<xx> represents the ASCII hexadecimal value of the character.
297
+
Certain characters have special meaning in MSBuild project files. Examples of these characters include semicolons (`;`) and asterisks (`*`). In order to use these special characters as literals in a project file, they must be specified by using the syntax `%<xx>`, where `<xx>` represents the ASCII hexadecimal value of the character.
302
298
303
299
Change the Message task to show the value of the Configuration property with special characters to make it more readable.
304
300
@@ -396,7 +392,7 @@ To change the separator of an item type, use the following syntax, where ItemTyp
396
392
@(ItemType, Separator)
397
393
```
398
394
399
-
Change the Message task to use carriage returns and line feeds (%0A%0D) to display Compile items one per line.
395
+
Change the `Message` task to use carriage returns and line feeds (%0A%0D) to display Compile items one per line.
400
396
401
397
**To display item type values one per line**
402
398
@@ -427,7 +423,7 @@ Change the Message task to use carriage returns and line feeds (%0A%0D) to displ
427
423
428
424
### Include, Exclude, and wildcards
429
425
430
-
You can use the wildcards "*", "\*\*", and "?" with the Include attribute to add items to an item type. For example,
426
+
You can use the wildcards "*", "\*\*", and "?" with the `Include` attribute to add items to an item type. For example,
431
427
432
428
```xml
433
429
<PhotosInclude="images\*.jpeg" />
@@ -460,9 +456,9 @@ Change the Message task to use carriage returns and line feeds (%0A%0D) to displ
460
456
<CompileInclude="*.cs"Exclude="*Designer*">
461
457
```
462
458
463
-
adds all files with the file extension *.cs* to the Compile item type, except for files whose names contain the string *Designer*. For more examples, see [How to: Exclude files from the build](../msbuild/how-to-exclude-files-from-the-build.md).
459
+
adds all files with the file extension *.cs* to the `Compile` item type, except for files whose names contain the string *Designer*. For more examples, see [How to: Exclude files from the build](../msbuild/how-to-exclude-files-from-the-build.md).
464
460
465
-
The `Exclude` attribute only affects the items added by the Include attribute in the item element that contains them both. For example,
461
+
The `Exclude` attribute only affects the items added by the `Include` attribute in the item element that contains them both. For example,
466
462
467
463
```xml
468
464
<CompileInclude="*.cs" />
@@ -550,7 +546,7 @@ Notice how the phrase "Compile.DependentUpon" appears several times. The use of
550
546
551
547
### Well-known metadata
552
548
553
-
Whenever an item is added to an item list, that item is assigned some well-known metadata. For example, %(Filename) returns the file name of any item. For a complete list of well-known metadata, see [Well-known item metadata](../msbuild/msbuild-well-known-item-metadata.md).
549
+
Whenever an item is added to an item list, that item is assigned some well-known metadata. For example, `%(Filename)` returns the file name of any item. For a complete list of well-known metadata, see [Well-known item metadata](../msbuild/msbuild-well-known-item-metadata.md).
554
550
555
551
**To examine well-known metadata:**
556
552
@@ -579,11 +575,11 @@ Notice how the phrase "Compile.DependentUpon" appears several times. The use of
579
575
Compile Filename: Settings.Designer
580
576
```
581
577
582
-
By comparing the preceding two examples, you can see that while not every item in the Compile item type has DependentUpon metadata, all items have the well-known Filename metadata.
578
+
By comparing the preceding two examples, you can see that while not every item in the `Compile` item type has DependentUpon metadata, all items have the well-known Filename metadata.
583
579
584
580
### Metadata transformations
585
581
586
-
Item lists can be transformed into new item lists. To transform an item list, use the following syntax, where \<ItemType> is the name of the item type and \<MetadataName> is the name of the metadata:
582
+
Item lists can be transformed into new item lists. To transform an item list, use the following syntax, where `<ItemType>` is the name of the item type and `<MetadataName>` is the name of the metadata:
0 commit comments