Skip to content

Commit 0a5f98c

Browse files
committed
Various improvements
1 parent 1d85c3f commit 0a5f98c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

docs/msbuild/customize-builds-for-generated-files.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Handle generated files in a build
33
description: Customize MSBuild project files in Visual Studio so that files generated during the build process are included in build output.
4-
ms.date: 02/28/2023
4+
ms.date: 8/5/2025
55
ms.topic: how-to
66
helpviewer_keywords:
77
- MSBuild, transforms
@@ -22,6 +22,13 @@ Files generated during execution don't exist during the evaluation phase, theref
2222

2323
<!-- Some logic that generates your file goes here -->
2424
<!-- Generated files should be placed in $(IntermediateOutputPath) -->
25+
<WriteLinesToFile
26+
File="$(IntermediateOutputPath)GeneratedFile.cs"
27+
Lines='enum MyEnum { A, B }'
28+
Overwrite="true" />
29+
<ItemGroup>
30+
<Compile Include="$(IntermediateOutputPath)GeneratedFile.cs" />
31+
</ItemGroup>
2532

2633
<ItemGroup>
2734
<!-- If your generated file was placed in `obj\` -->
@@ -34,10 +41,18 @@ Files generated during execution don't exist during the evaluation phase, theref
3441
<None Include="some\specific\path\*.*" CopyToOutputDirectory="PreserveNewest"/>
3542
</ItemGroup>
3643
</Target>
44+
45+
<Target Name="CleanGeneratedCode" AfterTargets="CoreClean">
46+
<Delete Files="$(IntermediateOutputPath)GeneratedFile.cs" />
47+
</Target>
3748
```
3849

3950
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).
4051

52+
## Next steps
53+
54+
This example could be improved to support more realistic use cases. For example, to support incremental builds when the generated code depends on an input file, `Inputs` and `Outputs` should be provided to the target. Such a target would only regenerate the file if the date of the input file or files is more recent than the output file. Often when customizing for code generation, it's recommended to create a custom task. See [Create a custom task for code generation](./tutorial-custom-task-code-generation.md).
55+
4156
## Related content
4257

4358
- [Customize your build](customize-your-build.md).

0 commit comments

Comments
 (0)