MSBuild support for T4 templates in Visual Studio 2017 RTM

孤者浪人 提交于 2019-11-28 09:06:57

I found the right solution.

Turns out that the T4 SDK is now included as part of Visual Studio 2017 (and not part of the separate Modeling SDK as it has been in the past), BUT you have to install it via the Visual Studio extension development toolset in the VS2017 installer (Text Template Transformation feature).

Once this is installed, you can use MSBuild to transform templates by importing the relevant targets into the MSBuild project:

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

<!-- add AFTER import for $(MSBuildToolsPath)\Microsoft.CSharp.targets -->
<Import Project="$(VSToolsPath)\TextTemplating\Microsoft.TextTemplating.targets" />

This solved my problem and also removes the need for the separate unofficial NuGet package.

I had a similar problem. My T4 wouldn't generate on build, but would on save. This was bizarre as I didn't get an error but upon reading @Sam's answer I figured something is wrong with my VS installation. And I was right. VS 2017 15.9.4 installs in it's own install directory, but doesn't copy Tools to the VSToolsPath folder. Instead it just leaves them where they are. So, for me, the correct solution was to use this <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(VsInstallRoot)\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />

T4Executer does this, and you can set which templates to excecute before build or after build, or set which templates should not be run on build. VS2017-19

I've had a similar issue where my Hosted Agent on Visual Studio Team Service did not generate the template output thus breaking my build server since it was missing the generated CS files.

The CS template output is generated just fine when building from Visual Studio 2015 on my development machine.

Looking at various solutions such as the one above it became clear to me that the more expedient fix was simply to commit the generated files to my source control system. That has the added advantage that I can code review any changes in the output and not just the template.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!