test if an MSBuild property is defined?
Asked Answered
N

1

32

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>
Nasal answered 10/2, 2011 at 0:6 Comment(0)
I
58

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?

Iodic answered 10/2, 2011 at 9:34 Comment(2)
Not the answer I wanted, but I admit it is correct and therefore useful.Nasal
Just thought I'd note that I've had this style of check fail because an undefined property (DevEnvDir) was set as *Undefined* rather than simply being empty. I don't know enough about MSBuild and the surrounding technologies to know what introduced the *Undefined* value, and if this is a common occurrence or a quirk of a tool we used, but I figure it's worth my mentioningHazen

© 2022 - 2024 — McMap. All rights reserved.