5

I'm trying to understand some MSBuild concept (I'm familiar with NAnt).

I try to initialise some property in a target and then used it in another. Here is an example:

<propertygroup>
    <MyProp>X</MyProp>
</propertygroup>

<target name="Main">
    <message text="$(MyProp)"/> <!-- Display 'X' -->
    <CallTarget Target="Sub">
        <Output TaskParameter="localProp" PropertyName="MyProp"/>
    </CallTarget>
    <message text="$(MyProp)"/> <!-- should display 'Y' -->
</target>

<target name="Sub" Outputs=$(localProp)>
    <propertygroup>
        <localProp>Y</localProp>
    </propertygroup>
</target>

And it of course does not work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pitming_Reloaded
  • 541
  • 2
  • 5
  • 12

2 Answers2

12

Aside from some minor syntax errors in element case (i.e. target->Target), there are 2 main things that need to be fixed to make it work:
1) The TaskParameter attribute should be set to "TargetOutputs"
2) The Outputs attribute of the Sub target need to be surrounded in quotes

This is a working example:

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

    <PropertyGroup>
        <MyProp>X</MyProp>
    </PropertyGroup>

    <Target Name="Main">
        <Message text="$(MyProp)"/> <!--display 'X'-->
        <CallTarget Targets="Sub">
            <Output TaskParameter="TargetOutputs" PropertyName="MyProp"/>
        </CallTarget>
        <Message text="$(MyProp)"/> <!-- should display 'Y'-->
    </Target>

    <Target Name="Sub" Outputs="$(localProp)">
        <PropertyGroup>
          <localProp>Y</localProp>
        </PropertyGroup>
    </Target>
</Project>

The above outputs:

Microsoft (R) Build Engine version 4.6.1055.0
[Microsoft .NET Framework, version 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 5/6/2016 9:51:37 AM.
Project "C:\workspace\dev\msbuild\temp.msbuild" on node 1 (default targets).
Main:
  X
  Y
Done Building Project "C:\workspace\dev\msbuild\temp.msbuild" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.07
sebagomez
  • 9,501
  • 7
  • 51
  • 89
pixman20
  • 183
  • 1
  • 6
8

You are confusing the Outputs defined on a Target with the Output parameters of a Task.

The Outputs for a Target are used in dependency analysis:

MSBuild Target Element

MSBuild Transforms - Dependency Analysis

The Output parameters of a Task are used to return data:

Simple Example Here

Community
  • 1
  • 1
Brian Walker
  • 8,658
  • 2
  • 33
  • 35
  • 1
    thx, I already read all that documentation. But I don't understand any words in it... Maybee you can give me an exemple of what I'm trying to do ? – Pitming_Reloaded Feb 22 '11 at 16:11
  • What are you trying to do? If you want to call something that will set some properties for you then you can write a target that sets the properties directly or write a custom task that sets outputs that are copied into your properties using the Output element under the task like the Simple Example link in the answer. – Brian Walker Feb 22 '11 at 16:34