Skip to content

Commit 6e34ef8

Browse files
committed
some changes
1 parent c81981d commit 6e34ef8

File tree

4 files changed

+100
-132
lines changed

4 files changed

+100
-132
lines changed

docs/msbuild/msbuild-items.md

Lines changed: 39 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,12 @@ manager: douge
1313
ms.workload:
1414
- "multiple"
1515
---
16-
# MSBuild Items
16+
# MSBuild items
1717
MSBuild items are inputs into the build system, and they typically represent files. Items are grouped into item types based on their element names. Item types are named lists of items that can be used as parameters for tasks. The tasks use the item values to perform the steps of the build process.
1818

1919
Because items are named by the item type to which they belong, the terms "item" and "item value" can be used interchangeably.
2020

21-
**In this topic**
22-
23-
- [Creating Items in a Project File](#BKMK_Creating1)
24-
25-
- [Creating Items During Execution](#BKMK_Creating2)
26-
27-
- [Referencing Items in a Project File](#BKMK_ReferencingItems)
28-
29-
- [Using Wildcards to Specify Items](#BKMK_Wildcards)
30-
31-
- [Using the Exclude Attribute](#BKMK_ExcludeAttribute)
32-
33-
- [Item Metadata](#BKMK_ItemMetadata)
34-
35-
- [Referencing Item Metadata in a Project File](#BKMK_ReferencingItemMetadata)
36-
37-
- [Well-known Item Metadata](#BKMK_WellKnownItemMetadata)
38-
39-
- [Transforming Item Types By Using Metadata](#BKMK_Transforming)
40-
41-
- [Item Definitions](#BKMK_ItemDefinitions)
42-
43-
- [Attributes for Items in an ItemGroup of a Target](#BKMK_AttributesWithinTargets)
44-
45-
- [Remove Attribute](#BKMK_RemoveAttribute)
46-
47-
- [KeepMetadata Attribute](#BKMK_KeepMetadata)
48-
49-
- [RemoveMetadata Attribute](#BKMK_RemoveMetadata)
50-
51-
- [KeepDuplicates Attribute](#BKMK_KeepDuplicates)
52-
53-
## <a name="BKMK_Creating1"></a> Creating Items in a Project File
21+
## Create items in a project file
5422
You declare items in the project file as child elements of an [ItemGroup](../msbuild/itemgroup-element-msbuild.md) element. The name of the child element is the type of the item. The `Include` attribute of the element specifies the items (files) to be included with that item type. For example, the following XML creates an item type that's named `Compile`, which includes two files.
5523

5624
```xml
@@ -60,7 +28,7 @@ MSBuild items are inputs into the build system, and they typically represent fil
6028
</ItemGroup>
6129
```
6230

63-
The item "file2.cs" doesn't replace the item "file1.cs"; instead, the file name is appended to the list of values for the `Compile` item type. You can't remove an item from an item type during the evaluation phase of a build.
31+
The item *file2.cs* doesn't replace the item *file1.cs*; instead, the file name is appended to the list of values for the `Compile` item type. You can't remove an item from an item type during the evaluation phase of a build.
6432

6533
The following XML creates the same item type by declaring both files in one `Include` attribute. Notice that the file names are separated by a semicolon.
6634

@@ -70,7 +38,7 @@ MSBuild items are inputs into the build system, and they typically represent fil
7038
</ItemGroup>
7139
```
7240

73-
## <a name="BKMK_Creating2"></a> Creating Items During Execution
41+
## Create items during execution
7442
Items that are outside [Target](../msbuild/target-element-msbuild.md) elements are assigned values during the evaluation phase of a build. During the subsequent execution phase, items can be created or modified in the following ways:
7543

7644
- Any task can emit an item. To emit an item, the [Task](../msbuild/task-element-msbuild.md) element must have a child [Output](../msbuild/output-element-msbuild.md) element that has an `ItemName` attribute.
@@ -79,12 +47,12 @@ MSBuild items are inputs into the build system, and they typically represent fil
7947

8048
- Starting in the .NET Framework 3.5, `Target` elements may contain [ItemGroup](../msbuild/itemgroup-element-msbuild.md) elements that may contain item elements.
8149

82-
## <a name="BKMK_ReferencingItems"></a> Referencing Items in a Project File
83-
To reference item types throughout the project file, you use the syntax @(`ItemType`). For example, you would reference the item type in the previous example by using `@(Compile)`. By using this syntax, you can pass items to tasks by specifying the item type as a parameter of that task. For more information, see [How to: Select the Files to Build](../msbuild/how-to-select-the-files-to-build.md).
50+
## Reference items in a project file
51+
To reference item types throughout the project file, you use the syntax @(\<ItemType>). For example, you would reference the item type in the previous example by using `@(Compile)`. By using this syntax, you can pass items to tasks by specifying the item type as a parameter of that task. For more information, see [How to: Select the files to build](../msbuild/how-to-select-the-files-to-build.md).
8452

85-
By default, the items of an item type are separated by semicolons (;) when it's expanded. You can use the syntax @(*ItemType*, '*separator*') to specify a separator other than the default. For more information, see [How to: Display an Item List Separated with Commas](../msbuild/how-to-display-an-item-list-separated-with-commas.md).
53+
By default, the items of an item type are separated by semicolons (;) when it's expanded. You can use the syntax @(\<ItemType>, '\<separator>') to specify a separator other than the default. For more information, see [How to: Display an item list separated with commas](../msbuild/how-to-display-an-item-list-separated-with-commas.md).
8654

87-
## <a name="BKMK_Wildcards"></a> Using Wildcards to Specify Items
55+
## Use wildcards to specify items
8856
You can use the **, \*, and ? wildcard characters to specify a group of files as inputs for a build instead of listing each file separately.
8957

9058
- The ? wildcard character matches a single character.
@@ -93,44 +61,44 @@ MSBuild items are inputs into the build system, and they typically represent fil
9361

9462
- The ** wildcard character sequence matches a partial path.
9563

96-
For example, you can specify all the .cs files in the directory that contains the project file by using the following element in your project file.
64+
For example, you can specify all the *.cs* files in the directory that contains the project file by using the following element in your project file.
9765

9866
```xml
9967
<CSFile Include="*.cs"/>
10068
```
10169

102-
The following element selects all .vb files on the D: drive:
70+
The following element selects all *.vb* files on the *D:* drive:
10371

10472
```xml
10573
<VBFile Include="D:/**/*.vb"/>
10674
```
10775

108-
For more information about wildcard characters, see [How to: Select the Files to Build](../msbuild/how-to-select-the-files-to-build.md).
76+
For more information about wildcard characters, see [How to: Select the files to build](../msbuild/how-to-select-the-files-to-build.md).
10977

110-
## <a name="BKMK_ExcludeAttribute"></a> Using the Exclude Attribute
111-
Item elements can contain the `Exclude` attribute, which excludes specific items (files) from the item type. The `Exclude` attribute is typically used together with wildcard characters. For example, the following XML adds every .cs file in the directory to the CSFile item type, except the `DoNotBuild.cs` file.
78+
## Use the Exclude attribute
79+
Item elements can contain the `Exclude` attribute, which excludes specific items (files) from the item type. The `Exclude` attribute is typically used together with wildcard characters. For example, the following XML adds every *.cs* file in the directory to the CSFile item type, except the *DoNotBuild.cs* file.
11280

11381
```xml
11482
<ItemGroup>
11583
<CSFile Include="*.cs" Exclude="DoNotBuild.cs"/>
11684
</ItemGroup>
11785
```
11886

119-
The `Exclude` attribute affects only the items that are added by the `Include` attribute in the item element that contains them both. The following example wouldn't exclude the file Form1.cs, which was added in the preceding item element.
87+
The `Exclude` attribute affects only the items that are added by the `Include` attribute in the item element that contains them both. The following example wouldn't exclude the file *Form1.cs*, which was added in the preceding item element.
12088

12189
```xml
12290
<Compile Include="*.cs" />
12391
<Compile Include="*.res" Exclude="Form1.cs">
12492
```
12593

126-
For more information, see [How to: Exclude Files from the Build](../msbuild/how-to-exclude-files-from-the-build.md).
94+
For more information, see [How to: Exclude files from the build](../msbuild/how-to-exclude-files-from-the-build.md).
12795

128-
## <a name="BKMK_ItemMetadata"></a> Item Metadata
96+
## Item metadata
12997
Items may contain metadata in addition to the information in the `Include` and `Exclude` attributes. This metadata can be used by tasks that require more information about the items or to batch tasks and targets. For more information, see [Batching](../msbuild/msbuild-batching.md).
13098

13199
Metadata is a collection of key-value pairs that are declared in the project file as child elements of an item element. The name of the child element is the name of the metadata, and the value of the child element is the value of the metadata.
132100

133-
The metadata is associated with the item element that contains it. For example, the following XML adds `Culture` metadata that has the value `Fr` to both the "one.cs" and the "two.cs" items of the CSFile item type.
101+
The metadata is associated with the item element that contains it. For example, the following XML adds `Culture` metadata that has the value `Fr` to both the *one.cs* and the *two.cs* items of the CSFile item type.
134102

135103
```xml
136104
<ItemGroup>
@@ -142,8 +110,8 @@ MSBuild items are inputs into the build system, and they typically represent fil
142110

143111
An item can have zero or more metadata values. You can change metadata values at any time. If you set metadata to an empty value, you effectively remove it from the build.
144112

145-
### <a name="BKMK_ReferencingItemMetadata"></a> Referencing Item Metadata in a Project File
146-
You can reference item metadata throughout the project file by using the syntax %(`ItemMetadataName`). If ambiguity exists, you can qualify a reference by using the name of the item type. For example, you can specify %(*ItemType.ItemMetaDataName*).The following example uses the Display metadata to batch the Message task. For more information about how to use item metadata for batching, see [Item Metadata in Task Batching](../msbuild/item-metadata-in-task-batching.md).
113+
### <a name="BKMK_ReferencingItemMetadata"></a> Reference item metadata in a project file
114+
You can reference item metadata throughout the project file by using the syntax %(\<ItemMetadataName>). If ambiguity exists, you can qualify a reference by using the name of the item type. For example, you can specify %(\<ItemType.ItemMetaDataName>).The following example uses the Display metadata to batch the Message task. For more information about how to use item metadata for batching, see [Item metadata in task batching](../msbuild/item-metadata-in-task-batching.md).
147115

148116
```xml
149117
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
@@ -161,11 +129,11 @@ MSBuild items are inputs into the build system, and they typically represent fil
161129
</Project>
162130
```
163131

164-
### <a name="BKMK_WellKnownItemMetadata"></a> Well-known Item Metadata
165-
When an item is added to an item type, that item is assigned some well-known metadata. For example, all items have the well-known metadata `%(Filename)`, whose value is the file name of the item. For more information, see [Well-known Item Metadata](../msbuild/msbuild-well-known-item-metadata.md).
132+
### <a name="BKMK_WellKnownItemMetadata"></a> Well-known item metadata
133+
When an item is added to an item type, that item is assigned some well-known metadata. For example, all items have the well-known metadata %(\<Filename>), whose value is the file name of the item. For more information, see [Well-known item metadata](../msbuild/msbuild-well-known-item-metadata.md).
166134

167-
### <a name="BKMK_Transforming"></a> Transforming Item Types By Using Metadata
168-
You can transform item lists into new item lists by using metadata. For example, you can transform an item type `CppFiles` that has items that represent .cpp files into a corresponding list of .obj files by using the expression `@(CppFiles -> '%(Filename).obj')`.
135+
### <a name="BKMK_Transforming"></a> Transform item types by using metadata
136+
You can transform item lists into new item lists by using metadata. For example, you can transform an item type `CppFiles` that has items that represent *.cpp* files into a corresponding list of *.obj* files by using the expression `@(CppFiles -> '%(Filename).obj')`.
169137

170138
The following code creates a `CultureResource` item type that contains copies of all `EmbeddedResource` items with `Culture` metadata. The `Culture` metadata value becomes the value of the new metadata `CultureResource.TargetDirectory`.
171139

@@ -182,8 +150,8 @@ MSBuild items are inputs into the build system, and they typically represent fil
182150

183151
For more information, see [Transforms](../msbuild/msbuild-transforms.md).
184152

185-
## <a name="BKMK_ItemDefinitions"></a> Item Definitions
186-
Starting in the .NET Framework 3.5, you can add default metadata to any item type by using the [ItemDefinitionGroup element](../msbuild/itemdefinitiongroup-element-msbuild.md). Like well-known metadata, the default metadata is associated with all items of the item type that you specify. You can explicitly override default metadata in an item definition. For example, the following XML gives the `Compile` items "one.cs" and "three.cs" the metadata `BuildDay` with the value "Monday". The code gives the item "two.cs" the metadata `BuildDay` with the value "Tuesday".
153+
## Item definitions
154+
Starting in the .NET Framework 3.5, you can add default metadata to any item type by using the [ItemDefinitionGroup element](../msbuild/itemdefinitiongroup-element-msbuild.md). Like well-known metadata, the default metadata is associated with all items of the item type that you specify. You can explicitly override default metadata in an item definition. For example, the following XML gives the `Compile` items *one.cs* and *three.cs* the metadata `BuildDay` with the value "Monday". The code gives the item *two.cs* the metadata `BuildDay` with the value "Tuesday".
187155

188156
```xml
189157
<ItemDefinitionGroup>
@@ -199,15 +167,15 @@ MSBuild items are inputs into the build system, and they typically represent fil
199167
</ItemGroup>
200168
```
201169

202-
For more information, see [Item Definitions](../msbuild/item-definitions.md).
170+
For more information, see [Item definitions](../msbuild/item-definitions.md).
203171

204-
## <a name="BKMK_AttributesWithinTargets"></a> Attributes for Items in an ItemGroup of a Target
172+
## Attributes for items in an ItemGroup of a Target
205173
Starting in the .NET Framework 3.5, `Target` elements may contain [ItemGroup](../msbuild/itemgroup-element-msbuild.md) elements that may contain item elements. The attributes in this section are valid when they are specified for an item in an `ItemGroup` that's in a `Target`.
206174

207-
### <a name="BKMK_RemoveAttribute"></a> Remove Attribute
175+
### <a name="BKMK_RemoveAttribute"></a> Remove attribute
208176
Items in an `ItemGroup` of a target may contain the `Remove` attribute, which removes specific items (files) from the item type. This attribute was introduced in the .NET Framework 3.5.
209177

210-
The following example removes every .config file from the Compile item type.
178+
The following example removes every *.config* file from the Compile item type.
211179

212180
```xml
213181
<Target>
@@ -217,7 +185,7 @@ MSBuild items are inputs into the build system, and they typically represent fil
217185
</Target>
218186
```
219187

220-
### <a name="BKMK_KeepMetadata"></a> KeepMetadata Attribute
188+
### <a name="BKMK_KeepMetadata"></a> KeepMetadata attribute
221189
If an item is generated within a target, the item element can contain the `KeepMetadata` attribute. If this attribute is specified, only the metadata that is specified in the semicolon-delimited list of names will be transferred from the source item to the target item. An empty value for this attribute is equivalent to not specifying it. The `KeepMetadata` attribute was introduced in the .NET Framework 4.5.
222190

223191
The following example illustrates how to use the `KeepMetadata` attribute.
@@ -259,7 +227,7 @@ Output:
259227
-->
260228
```
261229

262-
### <a name="BKMK_RemoveMetadata"></a> RemoveMetadata Attribute
230+
### <a name="BKMK_RemoveMetadata"></a> RemoveMetadata attribute
263231
If an item is generated within a target, the item element can contain the `RemoveMetadata` attribute. If this attribute is specified, all metadata is transferred from the source item to the target item except metadata whose names are contained in the semicolon-delimited list of names. An empty value for this attribute is equivalent to not specifying it. The `RemoveMetadata` attribute was introduced in the .NET Framework 4.5.
264232

265233
The following example illustrates how to use the `RemoveMetadata` attribute.
@@ -308,7 +276,7 @@ Output:
308276
-->
309277
```
310278

311-
### <a name="BKMK_KeepDuplicates"></a> KeepDuplicates Attribute
279+
### <a name="BKMK_KeepDuplicates"></a> KeepDuplicates attribute
312280
If an item is generated within a target, the item element can contain the `KeepDuplicates` attribute. `KeepDuplicates` is a `Boolean` attribute that specifies whether an item should be added to the target group if the item is an exact duplicate of an existing item.
313281

314282
If the source and target item have the same Include value but different metadata, the item is added even if `KeepDuplicates` is set to `false`. An empty value for this attribute is equivalent to not specifying it. The `KeepDuplicates` attribute was introduced in the .NET Framework 4.5.
@@ -347,12 +315,12 @@ Output:
347315
-->
348316
```
349317

350-
## See Also
351-
[MSBuild Concepts](../msbuild/msbuild-concepts.md)
318+
## See also
319+
[MSBuild concepts](../msbuild/msbuild-concepts.md)
352320
[MSBuild](../msbuild/msbuild.md)
353-
[How to: Select the Files to Build](../msbuild/how-to-select-the-files-to-build.md)
354-
[How to: Exclude Files from the Build](../msbuild/how-to-exclude-files-from-the-build.md)
355-
[How to: Display an Item List Separated with Commas](../msbuild/how-to-display-an-item-list-separated-with-commas.md)
356-
[Item Definitions](../msbuild/item-definitions.md)
321+
[How to: Select the files to build](../msbuild/how-to-select-the-files-to-build.md)
322+
[How to: Exclude files from the build](../msbuild/how-to-exclude-files-from-the-build.md)
323+
[How to: Display an item list separated with commas](../msbuild/how-to-display-an-item-list-separated-with-commas.md)
324+
[Item definitions](../msbuild/item-definitions.md)
357325
[Batching](../msbuild/msbuild-batching.md)
358-
[Item Element (MSBuild)](../msbuild/item-element-msbuild.md)
326+
[Item element (MSBuild)](../msbuild/item-element-msbuild.md)

0 commit comments

Comments
 (0)