T4 Tutorial: Execute templates at build time

This is not so hard to do actually. It ensures your transformations are executed when you build your solution, when in some cases you did not click ‘Build > Transform all T4 Templates’ when changes are made to your .tt files.

If one of your projects uses T4 to generate code, and you want it to execute at build time, consider the next steps:

Unload your project

Righ-click on the project in your solution explorer and click ‘unload’.

Open the .csproj of your project

Right-click again and select ‘edit projectname.csproj’

Edit the .csproj file

Add following PropertyGroup at the beginning of your csproj file (for VS2017):

<PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <TransformOnBuild>true</TransformOnBuild>
   <OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
    <TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>

The TransformOnBuild property set tot true ensures the templates to be transformed when you build your project (false by default).

OverwriteReadOnlyOutputFiles forces overwriting of readonly output files.

Set TransformOutOfDateOnly to false to transform files even if the output is up to date.

At the end of your csproj file, add following import:

<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />

Now you can reload your project and you are done. All T4 Templates within this project will be executed on each build.

2 thoughts on “T4 Tutorial: Execute templates at build time

  1. This can also be done from the command line (or pre-build event):
    “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\TextTransform.exe” YOUR_TEMPLATE.tt
    Note that the default namespace is provided by VS, so you may need to use the -u option.

    I found this to be the simplest way to trigger rebuilding a simple T4 based version number scheme, but only for release builds. Next to AssemblyInfo.cs, I have AssemblyVersion.tt

    using System.Reflection;

    [assembly: AssemblyCopyright(“Copyright © YOUR_NAME_HERE “)]
    [assembly: AssemblyVersion(“”)]

    Like

  2. This has an update to AssemblyVersion.tt

    This can also be done from the command line (or pre-build event):
    “C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\TextTransform.exe” YOUR_TEMPLATE.tt
    Note that the default namespace is provided by VS, so you may need to use the -u option.

    I found this to be the simplest way to trigger rebuilding a simple T4 based version number scheme, but only for release builds. Next to AssemblyInfo.cs, I have AssemblyVersion.tt

    using System.Reflection;

    [assembly: AssemblyCopyright(“Copyright © YOUR_NAME_HERE “)]
    [assembly: AssemblyVersion(“”)]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s