posts - 3 , comments - 3 , trackbacks - 0

My Links

News

Our new streamlined development process is composed of three stages -
  1. 1. Get it done
  2. 2. Just get it done
  3. 3. Is it done yet?

Article Categories

Archives

Post Categories

Property overwrite behaviour

I thought it worth sharing about property overwrite behaviour because i found it confusing at first in the hope of preventing some learning pain for the uninitiated with MSBuild :-)

The confusion for me came because of the redundancy of using a Condition statement in a _project_ level property to test that a property has not been previously set. What i mean is that the following two statements are always identical in behaviour, regardless if the property has been supplied on the command line -

  <PropertyGroup>
    <PropA Condition=" '$(PropA)' == '' ">PropA set at project level</PropA>
  </PropertyGroup>

has the same behaviour regardless of command line override as -

  <PropertyGroup>
    <PropA>PropA set at project level</PropA>
  </PropertyGroup>

 i.e. the two above property declarations have the same result whether the property is overridden on the command line or not.

To prove this experiment with the following .proj file -

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <PropA Condition=" '$(PropA)' == '' ">PropA set at project level</PropA>
  </PropertyGroup>

  <Target Name="Target1">
    <Message Text="PropA: $(PropA)"/>
  </Target>

  <Target Name="Target2">
    <PropertyGroup>
      <PropA>PropA set in Target2</PropA>
    </PropertyGroup>
    <Message Text="PropA: $(PropA)"/>
  </Target>

  <Target Name="Target3">
    <PropertyGroup>
      <PropA Condition=" '$(PropA)' == '' ">PropA set in Target3</PropA>
    </PropertyGroup>
    <Message Text="PropA: $(PropA)"/>
  </Target>

  <Target Name="Target4">
    <PropertyGroup>
      <PropA Condition=" '$(PropA)' != '' ">PropA set in Target4</PropA>
    </PropertyGroup>
    <Message Text="PropA: $(PropA)"/>
  </Target>

</Project>

Try invoking it using both of the following invocations and observe its output -

1)
>msbuild blog.proj /t:Target1;Target2;Target3;Target4

2)
>msbuild blog.proj /t:Target1;Target2;Target3;Target4 "/p:PropA=PropA set on command line"

Then try those two invocations with the following three variations of specifying PropA at the project level -

1)
  <PropertyGroup>
    <PropA Condition=" '$(PropA)' == '' ">PropA set at project level</PropA>
  </PropertyGroup>

2)
  <PropertyGroup>
    <PropA>PropA set at project level</PropA>
  </PropertyGroup>

3)
  <PropertyGroup>
    <PropA Condition=" '$(PropA)' != '' ">PropA set at project level</PropA>
  </PropertyGroup>

Print | posted on Wednesday, August 22, 2012 4:06 PM | Filed Under [ MSBuild ]

Feedback

Gravatar

# re: Property overwrite behaviour

Sorry for being a necromancer. Got here from google and it might be good to clear this.

If you change the condition to:
Condition="$(PropA) != ''"

it should behave as expected (please note the missing '' around $(PropA) )
10/23/2013 4:08 PM | ila
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: