EditDescription
This recipe checks the <TargetFrameworkVersion> of a Visual Studio 2008 project file. It fails the build if the version does not match the allowed version.
This is useful if your destination environment is .NET 2.0 only and you want to prevent people from building solutions that target .NET 3.0 and 3.5.
EditUsage
Add to TFSBuild.proj
EditSource
Grant Holliday - TFS2008: Restrict Target Framework Version Task
EditScript
<PropertyGroup>
<AllowedTargetFrameworkVersion>v2.0</AllowedTargetFrameworkVersion>
</PropertyGroup>
<Target Name="AfterGet" >
<CallTarget Targets="CheckTargetFrameworkVersions" />
</Target>
<UsingTask AssemblyFile="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.dll" TaskName="MSBuild.Community.Tasks.Xml.XmlQuery" />
<Target Name="CheckTargetFrameworkVersions" Condition=" '$(AllowedTargetFrameworkVersion)' != '' ">
<Message Text="Allowed Target Framework Version is: $(AllowedTargetFrameworkVersion)" Importance="high" />
<CreateItem Include="$(SolutionRoot)\**\*.csproj">
<Output TaskParameter="Include" ItemName="ProjectFiles" />
</CreateItem>
<Xml.XmlQuery
NamespaceDefinitions="n=http://schemas.microsoft.com/developer/msbuild/2003"
XmlFileName="%(ProjectFiles.FullPath)"
XPath="/n:Project/n:PropertyGroup/n:TargetFrameworkVersion">
<Output TaskParameter="Values" ItemName="TargetFrameworkVersions" />
</Xml.XmlQuery>
<Message Text="Found Target Framework Version: %(TargetFrameworkVersions._value)" Importance="low" />
<Error Condition="'%(TargetFrameworkVersions._value)' != '$(AllowedTargetFrameworkVersion)'"
Text="Target Framework Version is not allowed: %(TargetFrameworkVersions._value). Allowed version: $(AllowedTargetFrameworkVersion)" />
</Target>
EditNotes
- The build will fail if any of the projects within the build don’t match the that has been set.
- You will need at least the v1.2.0.306 MSBuild Community Tasks installed/available.
- Currently it matches all *.csproj files recursively, regardless of whether they are in the build configuration or not.
- If you want to actually enforce this, make sure that developers can’t change the file. It might be useful to store it on the server and use an .