5

How do I create a property in msbuild so that I can use it in a CallTarget directive?

Essentially I am trying to call a target 'subroutine' where the properties act as parameters.

I tried making a toy csproj file which attempts to create a property, and then calls a target which echos it. It echos null.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Test">
    <CreateProperty Value="AAA">
        <Output TaskParameter="Value" PropertyName="Foo" />
    </CreateProperty>
    <CallTarget Targets="Test2" />
  </Target>
  <Target Name="Test2">
    <Message Text="Target Test2: Foo=$(Foo)" />
  </Target>
</Project>

Running msbuild TestProj.csproj /t:Test outputs:

Test:
  Target Test: Foo=AAA
Test2:
  Target Test2: Foo=

I guess the problem is I'm thinking about msbuild in an imperative fashion (which is apparently a common mistake), so I'm hoping someone can correct what appears to be a very fundamental misunderstanding in how msbuild works.

fostandy
  • 4,282
  • 4
  • 37
  • 41

2 Answers2

2

You can use the target property DependsOnTarget to get the property passed from task to task.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="Test">
    <CreateProperty Value="AAA">
        <Output TaskParameter="Value" PropertyName="Foo" />
    </CreateProperty>
  </Target>
  <Target Name="Test2" DependsOnTargets="Test">
    <Message Text="Target Test2: Foo=$(Foo)" />
  </Target>
</Project>

The just call the second target.

James Woolfenden
  • 6,498
  • 33
  • 53
  • Thanks for the suggestion. Unfortunately that subtarget can be called by a few other targets so it is uncertain which target actually called it. This is probably an artifact of me abusing msbuild again.. – fostandy May 14 '12 at 23:52
  • 1
    @fostandy a decade later, I remain unconvinced there's another way to use MSBuild except abusing it. – Prof. Falken Nov 01 '21 at 16:50
0

Holy crap. This is apparently a bug in msbuild?

Overwrite properties with MSBuild

http://weblogs.asp.net/bhouse/archive/2006/03/20/440648.aspx

edit: Or this is a feature? https://stackoverflow.com/a/7539455

Community
  • 1
  • 1
fostandy
  • 4,282
  • 4
  • 37
  • 41