Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create a property in msbuild to be used in a calltarget subtarget

Tags:

msbuild

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.

like image 337
fostandy Avatar asked Nov 05 '22 00:11

fostandy


1 Answers

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.

like image 157
James Woolfenden Avatar answered Dec 19 '22 21:12

James Woolfenden