Skip to content

Commit a611b9e

Browse files
Merge pull request #13205 from ghogen/msbuild-walkthrough-freshness
Review and update
2 parents 8a29adc + 6163d0c commit a611b9e

File tree

1 file changed

+25
-29
lines changed

1 file changed

+25
-29
lines changed

docs/msbuild/walkthrough-using-msbuild.md

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "MSBuild Tutorial: Install and create a project"
33
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
55
ms.topic: tutorial
66
helpviewer_keywords:
77
- MSBuild, tutorial
@@ -40,7 +40,7 @@ If you have Visual Studio, then you already have MSBuild installed. With Visual
4040

4141
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.
4242

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).
4444

4545
::: moniker-end
4646

@@ -79,12 +79,7 @@ To install MSBuild on a system that doesn't have Visual Studio, go to Build Tool
7979

8080
Project files are XML-formatted files with the root node [Project](../msbuild/project-element-msbuild.md).
8181

82-
```xml
83-
<?xml version="1.0" encoding="utf-8"?>
84-
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
85-
```
86-
87-
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.
8883

8984
```xml
9085
<Project Sdk="Microsoft.NET.Sdk">
@@ -129,21 +124,22 @@ MSBuild keeps track of the targets of a build, and guarantees that each target i
129124

130125
```xml
131126
<Target Name="HelloWorld">
132-
<Message Text="Hello"></Message> <Message Text="World"></Message>
127+
<Message Text="Hello"></Message>
128+
<Message Text="World"></Message>
133129
</Target>
134130
```
135131

136132
3. Save the project file.
137133

138134
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).
139135

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."
141137

142138
## Build the target
143139

144140
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.
145141

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.
147143

148144
> [!NOTE]
149145
> 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
152148

153149
1. Open the **Command Window**.
154150

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.
156152

157153
If you need to find it manually, the file is *LaunchDevCmd.bat* in the *{Visual Studio installation folder}\Common7\Tools* folder.
158154

@@ -194,18 +190,18 @@ Run MSBuild from the **Developer Command Prompt** for Visual Studio to build the
194190
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,
195191

196192
```xml
197-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
193+
<TargetFrameworkVersion>net8.0</TargetFrameworkVersion>
198194
```
199195

200-
defines the property named TargetFrameworkVersion, giving it the string value "v4.5."
196+
defines the property named `TargetFrameworkVersion`, giving it the string value "net8.0"
201197

202198
Build properties can be redefined at any time. If
203199

204200
```xml
205-
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
201+
<TargetFrameworkVersion>net6.0</TargetFrameworkVersion>
206202
```
207203

208-
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"
209205

210206
## Examine a property value
211207

@@ -261,24 +257,24 @@ Use this syntax to examine some of the properties in the project file.
261257
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,
262258

263259
```xml
264-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
260+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
265261
```
266262

267263
means "If the Configuration property hasn't been defined yet, define it and give it the value 'Debug'."
268264

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).
270266

271267
### Reserved properties
272268

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).
274270

275271
### Environment variables
276272

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).
278274

279275
## Set properties from the command line
280276

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.
282278

283279
**To set a property value from the command line:**
284280

@@ -298,7 +294,7 @@ MSBuild creates the Configuration property and gives it the value "Release."
298294
299295
## Special characters
300296
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.
302298
303299
Change the Message task to show the value of the Configuration property with special characters to make it more readable.
304300
@@ -396,7 +392,7 @@ To change the separator of an item type, use the following syntax, where ItemTyp
396392
@(ItemType, Separator)
397393
```
398394

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.
400396

401397
**To display item type values one per line**
402398

@@ -427,7 +423,7 @@ Change the Message task to use carriage returns and line feeds (%0A%0D) to displ
427423

428424
### Include, Exclude, and wildcards
429425

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,
431427

432428
```xml
433429
<Photos Include="images\*.jpeg" />
@@ -460,9 +456,9 @@ Change the Message task to use carriage returns and line feeds (%0A%0D) to displ
460456
<Compile Include="*.cs" Exclude="*Designer*">
461457
```
462458

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).
464460

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,
466462

467463
```xml
468464
<Compile Include="*.cs" />
@@ -550,7 +546,7 @@ Notice how the phrase "Compile.DependentUpon" appears several times. The use of
550546

551547
### Well-known metadata
552548

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).
554550

555551
**To examine well-known metadata:**
556552

@@ -579,11 +575,11 @@ Notice how the phrase "Compile.DependentUpon" appears several times. The use of
579575
Compile Filename: Settings.Designer
580576
```
581577

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.
583579

584580
### Metadata transformations
585581

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:
587583

588584
```xml
589585
@(ItemType -> '%(MetadataName)')

0 commit comments

Comments
 (0)