test if an MSBuild property is defined?

Stato Machino picture Stato Machino · Feb 10, 2011 · Viewed 16.5k times · Source

In MsBuild, is it possible to create an MSBuild condition (or another situation) that will evaluate whether a Property is 'defined' (presuming that this is previous to assigning the property a value somewhere)?

The following seems a little too clumsy to be reliable:

<PropertyGroup Label="Undefined State">
     <Defined></Defined>
</PropertyGroup>

<Choose>
   <When Condition="('$(Defined)' == '' OR '$(Defined)' != '')">
        <Message Text="Defined is probably/likely/assuredly defined"/>
    </When>
    <Otherwise>
       <Message Text="Defined is reportedly/maybe/possibly not defined"/>
    </Otherwise>
<Choose>

Answer

Sergio Rykov picture Sergio Rykov · Feb 10, 2011

There exists common method for overriding properties.

Sample from C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets

   <PropertyGroup>
       <TargetFrameworkIdentifier Condition="'$(TargetFrameworkIdentifier)' == ''">.NETFramework</TargetFrameworkIdentifier>
       <TargetFrameworkVersion Condition=" '$(TargetFrameworkVersion)' == '' ">v4.0</TargetFrameworkVersion>
   </PropertyGroup>

If you will try to get value from $(NeverDefinedProperty) you just get an empty string. Can you describe the problem you want to solve?