How to use the "Exit" option from a target in MSBuild

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

I try to initialize some property in the target and then use 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 that of course does not work.

+4
source share
2 answers

You are misleading the outputs defined in Target with the output parameters of the task.

The outputs for the purpose are used in the analysis of dependencies:

MSBuild Target

MSBuild Conversion - Dependency Analysis

The output parameters of the task are used to return data:

Simple example here

+6
source

In addition to some minor syntax errors in the element (i.e. target-> Target), there are two main things that need to be fixed to make it work: 1) The TaskParameter attribute must be set to "TargetOutputs" 2) The Outputs attribute for the Sub object must be surrounded by quotation marks

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 
+6
source

Source: https://habr.com/ru/post/1340793/


All Articles