MSBuild Validating Properties
- by Brian Gillespie
I'm working on a reusable MSBuild Target that will be consumed by several other tasks. This target requires that several properties be defined. What's the best way to validate that properties are defined, throwing an Error if the are not?
Two attempts that I almost like:
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="3.5" DefaultTarget="Release" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Release">
<Error
Text="Property PropA required"
Condition="'$(PropA)' == ''"/>
<Error
Text="Property PropB required"
Condition="'$(PropB)' == ''"/>
<!-- The body of the task -->
</Target>
</Project>
Here's an attempt at batching. It's ugly because of the extra "Name" parameter. Is it possible to use the Include attribute instead?
<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="3.5" DefaultTarget="Release" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Release">
<!-- MSBuild BuildInParallel="true" Projects="@(ProjectsToBuild)"/ -->
<ItemGroup>
<RequiredProperty Include="PropA"><Name>PropA</Name></RequiredProperty>
<RequiredProperty Include="PropB"><Name>PropB</Name></RequiredProperty>
<RequiredProperty Include="PropC"><Name>PropC</Name></RequiredProperty>
</ItemGroup>
<Error
Text="Property %(RequiredProperty.Name) required"
Condition="'$(%(RequiredProperty.Name))' == ''" />
</Target>
</Project>