Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this MSBuild script set the property the way I expect?

Tags:

msbuild

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?

like image 420
scobi Avatar asked Jan 25 '10 23:01

scobi


2 Answers

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
like image 92
Nick Craver Avatar answered Nov 13 '22 11:11

Nick Craver


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>
like image 44
Sayed Ibrahim Hashimi Avatar answered Nov 13 '22 09:11

Sayed Ibrahim Hashimi