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/customize-your-build.md
+33-13Lines changed: 33 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ The location of the solution file is irrelevant to *Directory.Build.props*.
70
70
71
71
Properties that are set in *Directory.Build.props* can be overridden elsewhere in the project file or in imported files, so you should think of the settings in *Directory.Build.props* as specifying the defaults for your projects.
72
72
73
-
*Directory.Build.targets* is imported from *Microsoft.Common.targets* after importing *.targets* files from NuGet packages. So, it can override properties and targets defined in most of the build logic, or set properties for all your projects regardless of what the individual projects set.
73
+
*Directory.Build.targets* is imported from *Microsoft.Common.targets* after importing `.targets` files from NuGet packages. So, it can override properties and targets defined in most of the build logic, or set properties for all your projects regardless of what the individual projects set.
74
74
75
75
When you need to set a property or define a target for an individual project that overrides any prior settings, put that logic in the project file after the final import. In order to do this in an SDK-style project, you first have to replace the SDK-style attribute with the equivalent imports. See [How to use MSBuild project SDKs](how-to-use-project-sdk.md).
76
76
@@ -114,29 +114,29 @@ Or more simply: the first *Directory.Build.props* that doesn't import anything i
114
114
115
115
MSBuild is import-order dependent, and the last definition of a property (or a `UsingTask` or target) is the definition used.
116
116
117
-
When using explicit imports, you can import from a *.props* or *.targets* file at any point. Here is the widely used convention:
117
+
When using explicit imports, you can import from a `.props` or `.targets` file at any point. Here is the widely used convention:
118
118
119
-
-*.props* files are imported early in the import order.
119
+
-`.props` files are imported early in the import order.
120
120
121
-
-*.targets* files are imported late in the build order.
121
+
-`.targets` files are imported late in the build order.
122
122
123
123
This convention is enforced by `<Project Sdk="SdkName">` imports (that is, the import of *Sdk.props* comes first, before all of the contents of the file, then *Sdk.targets* comes last, after all of the contents of the file).
124
124
125
125
When deciding where to put the properties, use the following general guidelines:
126
126
127
127
- For many properties, it doesn't matter where they're defined, because they're not overwritten and will be read only at execution time.
128
128
129
-
- For behavior that might be customized in an individual project, set defaults in *.props* files.
129
+
- For behavior that might be customized in an individual project, set defaults in `.props` files.
130
130
131
-
- Avoid setting dependent properties in *.props* files by reading the value of a possibly customized property, because the customization won't happen until MSBuild reads the user's project.
131
+
- Avoid setting dependent properties in `.props` files by reading the value of a possibly customized property, because the customization won't happen until MSBuild reads the user's project.
132
132
133
-
- Set dependent properties in *.targets* files, because they'll pick up customizations from individual projects.
133
+
- Set dependent properties in `.targets` files, because they'll pick up customizations from individual projects.
134
134
135
-
- If you need to override properties, do it in a *.targets* file, after all user-project customizations have had a chance to take effect. Be cautious when using derived properties; derived properties may need to be overridden as well.
135
+
- If you need to override properties, do it in a `.targets` file, after all user-project customizations have had a chance to take effect. Be cautious when using derived properties; derived properties may need to be overridden as well.
136
136
137
-
- Include items in *.props* files (conditioned on a property). All properties are considered before any item, so user-project property customizations get picked up, and this gives the user's project the opportunity to `Remove` or `Update` any item brought in by the import.
137
+
- Include items in `.props` files (conditioned on a property). All properties are considered before any item, so user-project property customizations get picked up, and this gives the user's project the opportunity to `Remove` or `Update` any item brought in by the import.
138
138
139
-
- Define targets in *.targets* files. However, if the *.targets* file is imported by an SDK, remember that this scenario makes overriding the target more difficult because the user's project doesn't have a place to override it by default.
139
+
- Define targets in `.targets` files. However, if the `.targets` file is imported by an SDK, remember that this scenario makes overriding the target more difficult because the user's project doesn't have a place to override it by default.
140
140
141
141
- If possible, prefer customizing properties at evaluation time over changing properties inside a target. This guideline makes it easier to load a project and understand what it's doing.
142
142
@@ -190,6 +190,26 @@ If you need different behaviors depending on the .NET language (C#, Visual Basic
190
190
</PropertyGroup>
191
191
```
192
192
193
+
## Handle generated files
194
+
195
+
In any given build, files that get generated during the build behave differently from static files (such as source files). For this reason, it's important to understand [How MSBuild Builds Projects](build-process-overview.md). The two phases are the [evaluation phase](build-process-overview.md#evaluation-phase) and the [execution phase](build-process-overview.md#execution-phase). During the evaluation phase, MSBuild reads your project, imports everything, creates properties, expands globs for items, and sets up the build process. During the execution phase, MSBuild performs the build by running targets and tasks with the data it parsed during the evaluation phase.
196
+
197
+
Files generated during execution don't exist during the evaluation phase, therefore they aren't included in the build process. To solve this problem, you must manually add the generated files into the build process. The recommended way to do this is by adding the new file to the `Content` or `None` items before the `BeforeBuild` target, as in the following example:
Adding your generated file to `None` or `Content` is sufficient for the build process to see it. You also want to ensure it gets added at the right time. Ideally, your target runs before `BeforeBuild`. `AssignTargetPaths` is another possible target, as it is the final opportunity to modify `None` and `Content` items (among others) before they are transformed into new items. See [Common Item Types](common-msbuild-project-items.md).
212
+
193
213
## Customize the solution build
194
214
195
215
> [!IMPORTANT]
@@ -245,17 +265,17 @@ If you have a dedicated build server and want to ensure that certain targets alw
245
265
246
266
## Customize C++ builds
247
267
248
-
For C++ projects, the previously mentioned custom *.targets* and *.props* files cannot be used in the same way to override default settings. *Directory.Build.props* is imported by *Microsoft.Common.props*, which is imported in `Microsoft.Cpp.Default.props` while most of the defaults are defined in *Microsoft.Cpp.props* and for a number of properties a "if not yet defined" condition cannot be used, as the property is already defined, but the default needs to be different for particular project properties defined in `PropertyGroup` with `Label="Configuration"` (see [.vcxproj and .props file structure](/cpp/build/reference/vcxproj-file-structure)).
268
+
For C++ projects, the previously mentioned custom `.targets` and `.props` files cannot be used in the same way to override default settings. *Directory.Build.props* is imported by *Microsoft.Common.props*, which is imported in `Microsoft.Cpp.Default.props` while most of the defaults are defined in *Microsoft.Cpp.props* and for a number of properties a "if not yet defined" condition cannot be used, as the property is already defined, but the default needs to be different for particular project properties defined in `PropertyGroup` with `Label="Configuration"` (see [.vcxproj and .props file structure](/cpp/build/reference/vcxproj-file-structure)).
249
269
250
-
But, you can use the following properties to specify *.props* file(s) to be automatically imported before/after *Microsoft.Cpp.\** files:
270
+
But, you can use the following properties to specify `.props` file(s) to be automatically imported before/after *Microsoft.Cpp.\** files:
251
271
252
272
- ForceImportAfterCppDefaultProps
253
273
- ForceImportBeforeCppProps
254
274
- ForceImportAfterCppProps
255
275
- ForceImportBeforeCppTargets
256
276
- ForceImportAfterCppTargets
257
277
258
-
To customize the default values of properties for all C++ builds, create another *.props* file (say, *MyProps.props*), and define the `ForceImportAfterCppProps` property in `Directory.Build.props` pointing to it:
278
+
To customize the default values of properties for all C++ builds, create another `.props` file (say, *MyProps.props*), and define the `ForceImportAfterCppProps` property in `Directory.Build.props` pointing to it:
0 commit comments