在msbuild中动态创建要在calltarget子目标中使用的属性 [英] Dynamically create a property in msbuild to be used in a calltarget subtarget
问题描述
如何在msbuild中创建属性,以便可以在CallTarget指令中使用它?
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.
我尝试制作一个玩具csproj文件,该文件试图创建一个属性,然后调用一个与之相呼应的目标.它回显null.
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>
运行msbuild TestProj.csproj /t:Test
输出:
Test:
Target Test: Foo=AAA
Test2:
Target Test2: Foo=
我想问题是我正在以强制性的方式考虑msbuild(这显然是一个常见错误),所以我希望有人可以纠正似乎是msbuild的工作方式中一个非常根本的误解.
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.
推荐答案
您可以使用目标属性DependsOnTarget来获取从一个任务传递到另一个任务的属性.
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>
刚刚调用第二个目标.
这篇关于在msbuild中动态创建要在calltarget子目标中使用的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!