tags:

views:

43

answers:

2

I'm trying to set a default value for an MSBuild property. Say I start with this:

<Choose>
    <When Condition="..something..">
        <PropertyGroup>
            ...
            <MySetting>true</MySetting>
        <PropertyGroup>
    </When>
    ...
</Choose>

If the condition is not true, then MySetting will be ''. So shouldn't this set it to false?

<PropertyGroup>
    <MySetting Condition="'$(MySetting)'==''">false</MySetting>
</PropertyGroup>

Later on, I'd like to use MySetting in a conditional without having to test for =='true', like this:

<PropertyGroup Condition="$(MySetting)">
    ...
</PropertyGroup>

Yet when I load this project into Visual Studio it complains that the specified condition "$(MySetting)" evaluates to "" instead of a boolean.

So it appears that either my condition that checks for '' to assign the property to false is incorrect. What am I doing wrong?

+1  A: 

In MSBuild, you're dealing with strings so you get the '' instead of false...if you want to default it to 'false' and override via the command line, just declare a property group above your existing condition block in the script:

<PropertyGroup>
    <MySetting>false</MySetting>
</PropertyGroup>

Your condition block below can set this to true, or you could also set it via the command line, like this:

MSBuild.exe MyMSBuildFile.csproj /p:MySetting=true
Nick Craver
This is a better way to set the default. I'll do that instead, thanks. But I still don't understand why the middle block of code in the original question doesn't set the property to false. I am testing against '' to set the property to false. Why doesn't that run?
Scott Bilas
@Scott Bilas: Are you saying after the `<MySetting Condition="'$(MySetting)'==''">false</MySetting>` runs, the value is still `''`?
Nick Craver
Yup, according to the compiler. It's as if that property set never happens.
Scott Bilas
@Scott Bilas: Just curious, try this at the top: `<PropertyGroup><MySetting Condition="'$(MySetting)'==''">false</MySetting></PropertyGroup>`
Nick Craver
Ok, I tried that and no change. I also tried adding above your suggested change various blocks defining MySetting like <PropertyGroup><MySetting/></PropertyGroup> but still no dice.
Scott Bilas
Whoa, I take it all back. The problem was that I needed to restart Visual Studio and not just close/reopen the SLN to make the target file changes take place. Odd, because every other change I make takes effect on a SLN reload. So there is no problem after all, though I am glad that I got your better way of setting this default. Thanks. :)
Scott Bilas
@Scott Bilas: Glad it's working for you...took me a while to setup our build properly as well since I had to learn MSBuild along the way, definitely a unique beast.
Nick Craver
A: 

If you want to declare defaults for properties better then using Chose is to do it on the property as:

<PropertyGroup>
    <MySetting Condition=" '$(MySetting)'=='' ">true</MySetting>
</PropertyGroup>

Also for conditions always wrap the left and right side in '', even if you are dealing with what should be bool values. So change your second property group to look like:

<PropertyGroup Condition=" '$(MySetting)'=='true' ">
</PropertyGroup>
Sayed Ibrahim Hashimi