Files that get generated during the build behave differently from static files (eg. source files). For this reason, it's important to understand How MSBuild Builds Projects. I'll cover the main two phases here at a high level.
- MSBuild reads your project, imports everything, creates Properties, expands globs for Items outside of Targets, and sets up the build process.
- MSBuild runs Targets & Tasks with the provided Properties & Items in order to perform the build.
Key Takeaway: Files generated during execution don't exist during evaluation, therefore they aren't included in the build process.
The solution? When the files are generated, manually add them 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.
Here's a sample target that does this:
<Target Name="Foo" BeforeTargets="BeforeBuild">
<!-- Some logic that generates your file goes here -->
<!-- Note: We recommend generating your files into $(IntermediateOutputPath) -->
<ItemGroup>
<!-- If your generated file was placed in `obj\` -->
<None Include="$(IntermediateOutputPath)my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- If you know exactly where that file is going to be, you can hard code the path. -->
<None Include="some\specific\path\my-generated-file.xyz" CopyToOutputDirectory="PreserveNewest"/>
<!-- If you want to capture "all files of a certain type", you can glob like so. -->
<None Include="some\specific\path\*.xyz" CopyToOutputDirectory="PreserveNewest"/>
<None Include="some\specific\path\*.*" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>
</Target>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 option, as it is the "final stop" before None and Content items (among others) are transformed into new items.
Relevant Links:
Thank you for creating this. I tried it with .NET MAUI, I generated a CSS file during build time and it was copied in the "bin" but not in both "obj" and published apps. Any suggestions on how to automatically copy the file on .NET MAUI?