Sorry for not returning to this question for a while. Initially, I thought that to solve this problem we would need to bend MSBuild in a very unusual way (the plan for today was to write a complex built-in task that would do regex-replace in an external file using the Msbuild properties as tokens). But I think this can be solved more easily using the CDATA section, which is valid in the property definition:
Here is the main script:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty> <Version>1.0.1b</Version> <ProjectName>MSBuild Rox</ProjectName> <Author>Alexey Shcherbak</Author> </PropertyGroup> <Target Name="Build"> <ItemGroup> <PropsToPass Include="MyOtherProperty=$(MyOtherProperty)" /> <PropsToPass Include="Version=$(Version)" /> <PropsToPass Include="ProjectName=$(ProjectName)" /> <PropsToPass Include="Author=$(Author)" /> </ItemGroup> <MSBuild Projects="TransformHTML.Template.proj" Properties="@(PropsToPass)" /> </Target> </Project>
And here is your template. This is not pure html, it is still an msbuild file, but at least without ugly encoding for html tags in xml. This is just a block in CDATA
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Transform"> <PropertyGroup> <HtmlProperty><![CDATA[ <body> <div>$(MyOtherProperty)</div> <div>$(Version)</div> <div>$(ProjectName)</div> <div>$(Author)</div> </body> ]]></HtmlProperty> </PropertyGroup> <Target Name="Transform"> <Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" /> </Target> </Project>
It may not be very elegant (I personally do not like the section with @PropsToPass), but it will do the job. You can put all the built-in in a single file, and then you do not need to pass properties to the MSBuild task. I do not like the massive html encoding from the proposed "this solution", but I would prefer to save the HTML template in the same script where it will be converted, only in a good html format, without encoding.
Single file example:
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <MyOtherProperty>$([System.DateTime]::Now)</MyOtherProperty> <Version>1.0.1b</Version> <ProjectName>MSBuild Rox</ProjectName> <Author>Alexey Shcherbak</Author> </PropertyGroup> <Target Name="Build"> <PropertyGroup> <HtmlProperty><![CDATA[ <body> <div>$(MyOtherProperty)</div> <div>$(Version)</div> <div>$(ProjectName)</div> <div>$(Author)</div> </body> ]]></HtmlProperty> </PropertyGroup> <Message Text="HtmlProperty: $(HtmlProperty)" Importance="High" /> </Target> </Project>
You can also download these files here.
source share