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?
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With