6

When running MSBuild, anything passed in on the command line using /p:MyProp=value is accessible from every MSBuild script invoked (via the MSBuild task) from the main script. How can I define a property that is similarly accessible from every script, in a task?

For example:

Script1.proj:
[...]
<Target Name="Test">
  <MSBuild Projects="Script2.proj"/>
  <Message Text="Script1, GlobalProp1=$(GlobalProp1)"/>
  <Message Text="Script1, GlobalProp2=$(GlobalProp2)"/>
</Target>

Script2.proj:
[...]
<Target Name="Test">
  <!-- ??? Set GlobalProp2 = VALUE2 ??? -->
  <Message Text="Script2, GlobalProp1=$(GlobalProp1)"/>
  <Message Text="Script2, GlobalProp2=$(GlobalProp2)"/>
</Target>

If run like this: msbuild Script1.proj /p:GlobalProp1=VALUE1 the above scripts produce the following output:

Script2, GlobalProp1=VALUE1
Script2, GlobalProp2=
Script1, GlobalProp1=VALUE1
Script1, GlobalProp2=

I'd like Script2.proj to set GlobalProp2 somehow to produce the following output:

Script2, GlobalProp1=VALUE1
Script2, GlobalProp2=VALUE2
Script1, GlobalProp1=VALUE1
Script1, GlobalProp2=VALUE2

Is this possible, and if so, how?

I've seen suggestions to use environment variables, but it seems that the only situation in which that helps is when the variable is set in the parent script, and the child script is invoked using the Exec task (otherwise the env.variable change has no effect).

Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
  • btw environment variables also take effect when invoking VCBuild task, not just Exec task – stijn Dec 25 '09 at 11:40

2 Answers2

7

Since you are using the MSBuild task you have to pass the desired properties into the call using the Properties attribute so you should change you example to.

<Target Name="Test">  
    <MSBuild Projects="Script2.proj" 
        Properties="GlobalProp1=$(GlobalProp1);GlobalProp2=$(GlobalProp2)"
    />  
    <Message Text="Script1, GlobalProp1=$(GlobalProp1)"/>  
    <Message Text="Script1, GlobalProp2=$(GlobalProp2)"/>
</Target>

The properties must be explicitly passed, this is by design.

If you want a tighter integration then instead of using the MSBuild task you should just import the file(s) which will create 1 logical script.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
5

You may use rsp file to define global properties. These properties are visible to child projects.

yunus demiray
  • 51
  • 1
  • 1
  • I can't see how my "sub-script" (`Script2.proj`) will be able to change them. Is it possible to do what I describe in the question using .rsp files? – Roman Starkov Nov 07 '12 at 23:35