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/how-to-build-incrementally.md
+39-31Lines changed: 39 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
---
2
-
title: Build incrementally out-of-date components
3
-
description: Explore how to use MSBuild to build incrementally, so previously built components that are still up-to-date aren't rebuilt.
4
-
ms.date: 05/16/2022
5
-
ms.topic: how-to
2
+
title: Incremental Builds for New or Stale Targets
3
+
description: Explore how to use MSBuild to run incremental builds for new or stale targets only, and not for targets that are up-to-date.
4
+
ms.date: 06/26/2025
5
+
ms.topic: concept-article
6
6
helpviewer_keywords:
7
7
- MSBuild, incremental builds
8
8
- incremental builds
@@ -11,26 +11,32 @@ author: ghogen
11
11
ms.author: ghogen
12
12
manager: mijacobs
13
13
ms.subservice: msbuild
14
+
#customer intent: As a developer, I want to run incremental builds with MSBuild, so I can build only new targets or out-of-date targets.
14
15
---
15
-
# Build incrementally
16
16
17
-
When you build a large project, it is important that previously built components that are still up-to-date are not rebuilt. If all targets are built every time, each build will take a long time to complete. To enable incremental builds (builds in which only those targets that have not been built before or targets that are out of date, are rebuilt), the Microsoft Build Engine (MSBuild) can compare the timestamps of the input files with the timestamps of the output files and determine whether to skip, build, or partially rebuild a target. However, there must be a one-to-one mapping between inputs and outputs. You can use transforms to enable targets to identify this direct mapping. For more information on transforms, see [Transforms](../msbuild/msbuild-transforms.md).
17
+
# MSBuild incremental builds for new or stale targets
18
18
19
-
## Specify inputs and outputs
19
+
When you build a large project, it's important that built targets that are still up-to-date don't rebuild. If all targets build every time, each build can take a long time to complete.
20
20
21
-
A target can be built incrementally if the inputs and outputs are specified in the project file.
21
+
In an incremental build, only the unbuilt targets of the project or stale (out-of-date) targets build. The Microsoft Build Engine (MSBuild) compares the timestamps of the input files with the timestamps of the output files. MSBuild determines whether to skip, build, or partially rebuild each target.
22
22
23
-
#### To specify inputs and outputs for a target
23
+
The incremental build process requires a one-to-one mapping between inputs and outputs for targets. You can use _transforms_ to enable targets to identify the input-to-ouput mapping. For more information, see [Transforms](msbuild-transforms.md).
24
24
25
-
- Use the `Inputs`and `Outputs` attributes of the `Target` element. For example:
25
+
## Specify inputs and outputs
26
26
27
-
```xml
28
-
<TargetName="Build"
29
-
Inputs="@(CSFile)"
30
-
Outputs="hello.exe">
31
-
```
27
+
MSBuild can build a target incrementally if the inputs and outputs in the target are specified in the project file. You specify the values with the `Inputs` and `Outputs` attributes of the `Target` element.
32
28
33
-
MSBuild can compare the timestamps of the input files with the timestamps of the output files and determine whether to skip, build, or partially rebuild a target. In the following example, if any file in the `@(CSFile)` item list is newer than the *hello.exe* file, MSBuild will run the target; otherwise it will be skipped:
29
+
The following example specifies the `@(CSFile)` item list for the `Inputs` and the *hello.exe* file for the `Outputs` of a target:
30
+
31
+
```xml
32
+
<TargetName="Build"
33
+
Inputs="@(CSFile)"
34
+
Outputs="hello.exe">
35
+
...
36
+
</Target>
37
+
```
38
+
39
+
MSBuild compares the timestamps of the inputs and the outputs for the target. In the example, if any file in the `@(CSFile)` item list is newer than the *hello.exe* file, MSBuild builds the target; otherwise, the target is skipped:
34
40
35
41
```xml
36
42
<TargetName="Build"
@@ -43,27 +49,31 @@ MSBuild can compare the timestamps of the input files with the timestamps of the
43
49
</Target>
44
50
```
45
51
46
-
When inputs and outputs are specified in a target, either each output can map to only one input or there can be no direct mapping between the outputs and inputs. In the previous [Csc task](../msbuild/csc-task.md), for example, the output, *hello.exe*, cannot be mapped to any single input - it depends on all of them.
52
+
## Compare one-to-one mapping versus no direct mapping
47
53
48
-
> [!NOTE]
49
-
> A target in which there is no direct mapping between the inputs and outputs will always build more often than a target in which each output can map to only one input because MSBuild cannot determine which outputs need to be rebuilt if some of the inputs have changed.
54
+
When you specify inputs and outputs in a target, either each output maps to one input directly, or no direct mapping exists between the outputs and inputs. In the example, the [Csc task](csc-task.md) specifies an output assembly that doesn't map to a single input. For this task, the output depends on all of the inputs.
50
55
51
-
Tasks in which you can identify a direct mapping between the outputs and inputs, such as the [LC task](../msbuild/lc-task.md), are most suitable for incremental builds, unlike tasks such as [Csc](../msbuild/csc-task.md) and [Vbc](../msbuild/vbc-task.md), which produce one output assembly from a number of inputs.
56
+
Here are some considerations about one-to-one mapping versus no direct mapping:
52
57
53
-
## Example
58
+
- A target with no direct mapping between inputs and outputs always builds more often than a target where each output maps to a single input. If a target has no direct mapping, MSBuild can't determine the specific outputs to rebuild when only some inputs change.
54
59
55
-
The following example uses a project that builds Help files for a hypothetical Help system. The project works by converting source *.txt* files into intermediate *.content* files, which then are combined with XML metadata files to produce the final *.help* file used by the Help system. The project uses the following hypothetical tasks:
60
+
- Tasks that can identify a direct mapping between the outputs and inputs are most suitable for incremental builds. An example is the [LC task](lc-task.md) task, which generates a *.license* file.
56
61
57
-
-`GenerateContentFiles`: Converts *.txt* files into *.content* files.
62
+
-Tasks that produce a single output assembly from multiple inputs aren't suitable for incremental builds. Examples include the [Csc task](csc-task.md) that wraps the *csc.exe* file and produces executables, libraries, and modules, and the [Vbc task](vbc-task.md) that wraps the *vbc.exe* file.
58
63
64
+
## Use transforms to create a one-to-one mapping
65
+
66
+
The following example defines a project that builds content files for a Help system. The project works by converting source *.txt* files into intermediate *.content* files, which are combined with XML metadata files to produce the final *.help* file used by the system. The project includes the following tasks:
67
+
68
+
-`GenerateContentFiles`: Converts *.txt* files into *.content* files.
59
69
-`BuildHelp`: Combines *.content* files and XML metadata files to build the final *.help* file.
60
70
61
-
The project uses transforms to create a one-to-one mapping between inputs and outputs in the `GenerateContentFiles` task. For more information, see [Transforms](../msbuild/msbuild-transforms.md). Also, the`Output` element is set to automatically use the outputs from the `GenerateContentFiles` task as the inputs for the `BuildHelp` task.
71
+
The project uses [transforms](msbuild-transforms.md) to create a one-to-one mapping between inputs and outputs in the `GenerateContentFiles` task. The`Output` element is set to automatically use the outputs from the `GenerateContentFiles` task as the inputs for the `BuildHelp` task.
62
72
63
-
This project file contains both the `Convert` and `Build` targets. The `GenerateContentFiles` and `BuildHelp` tasks are placed in the `Convert` and `Build` targets respectively so that each target can be built incrementally. By using the `Output` element, the outputs of the `GenerateContentFiles` task are placed in the `ContentFile` item list, where they can be used as inputs for the `BuildHelp` task. Using the `Output` element in this way automatically provides the outputs from one task as the inputs for another task so that you do not have to list the individual items or item lists manually in each task.
73
+
The project file contains `Convert` and `Build` targets. The `GenerateContentFiles` and `BuildHelp` tasks are placed in the `Convert` and `Build` targets respectively, so each target can build incrementally. The `Output` element definition places the outputs of the `GenerateContentFiles` task in the `ContentFile` item list, to use as inputs for the `BuildHelp` task. This approach automatically provides the outputs from one task as the inputs for another task. You don't have to list the individual items or item lists manually in each task.
64
74
65
75
> [!NOTE]
66
-
> Although the `Convert` target can build incrementally, all outputs from that target always are required as inputs for the `Build` target. MSBuild automatically provides all the outputs from one target as inputs for another target when you use the `Output` element.
76
+
> Although the `Convert` target can build incrementally, all outputs from that target are always required as inputs for the `Build` target. MSBuild automatically provides all the outputs from one target as inputs for another target when you use the `Output` element.
67
77
68
78
```xml
69
79
<ProjectDefaultTargets="Build"
@@ -99,8 +109,6 @@ This project file contains both the `Convert` and `Build` targets. The `Generate
99
109
100
110
## Related content
101
111
102
-
-[Targets](../msbuild/msbuild-targets.md)
103
-
-[Target element (MSBuild)](../msbuild/target-element-msbuild.md)
104
-
-[Transforms](../msbuild/msbuild-transforms.md)
105
-
-[Csc task](../msbuild/csc-task.md)
106
-
-[Vbc task](../msbuild/vbc-task.md)
112
+
-[Targets](msbuild-targets.md)
113
+
-[Target element (MSBuild)](target-element-msbuild.md)
0 commit comments