I'm adding a custom .tt
template generation target to my project to run before CoreBuild
, and there appear to be 2 ways of doing it:
<Project...>
<Target Name="TransformOnBuild" AfterTargets="BeforeBuild">
</Project>
and
<Project...>
<Target Name="TransformOnBuild" BeforeTargets="CoreBuild">
</Project>
If my target should run before my project is built, as the project relies on it, would it be better for me to use the latter? I've seen the former used to do things like generate text templates, but it seems like an unreliable way to do it because it might get run after CoreBuild
, which is too late. Or is there some reason why AfterTargets="BeforeBuild"
is still guaranteed to run before the core build?
I've also seen BeforeTargets="BeforeBuild"
which will build even earlier. Is this a better place to put a `.tt text generation target?
Building on @stjin's answer, a good solution seems to be using
BeforeTargets="CoreCompile" DependsOnTargets="PrepareForBuild"
which is what the .net sdk (new-style csproj for .net core / standard projects) is doing for automatic AssemblyInfo.cs generation.
It uses the following comment to explain why:
Note that this must run before every invocation of CoreCompile to ensure that all compiler runs see the generated assembly info. There is at least one scenario involving Xaml where CoreCompile is invoked without other potential hooks such as Compile or CoreBuild, etc., so we hook directly on to CoreCompile. Furthermore, we must run after PrepareForBuild to ensure that the intermediate directory has been created.
Note that the "intermediate directory" (obj/[TargetFramework]
in this case) is where the output .cs
file is placed in this case which may also be what you might want to do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With