输出 MSBuild 变量时出现问题
我正在尝试将变量从一个目标输出到启动它的父目标中。例如,
目标 1 只是调用文件 2 中的任务,并且应该能够使用其中设置的变量。然而,我似乎无法让它工作(也许语法错误?)。目标 1 如下所示:
<Target Name="RetrieveParameter">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="ObtainOutput" />
<Message Text="Output = $(OutputVar)" />
</Target>
目标 2 是读取文本文件的值并将其设置为属性并将变量“OutputVar”设置为匹配的位置。这应该返回给父母。
<Target Name="ObtainOutput" Outputs="$(OutputVar)">
<ReadLinesFromFile File="output.txt">
<Output TaskParameter="Lines"
PropertyName="OutputVar" />
</ReadLinesFromFile>
</Target>
我对 MSBuild 任务还很陌生,所以这很可能是显而易见的事情。我想做的就是在一个任务中设置一个变量,然后在调用它的父任务中使用该变量。
I'm trying to output the variable from one target, into the parent target which started it. For example,
Target 1 simply calls the task in file 2 and is supposed to be able to use the variable set within that. However, I just can't seem to get it to work (wrong syntax perhaps?). Target 1 looks like this:
<Target Name="RetrieveParameter">
<MSBuild Projects="$(MSBuildProjectFile)" Targets="ObtainOutput" />
<Message Text="Output = $(OutputVar)" />
</Target>
Target 2 is where it reads in the value of the text file and sets it to the property and sets the variable 'OutputVar' to match. This is supposed to be returned to the parent.
<Target Name="ObtainOutput" Outputs="$(OutputVar)">
<ReadLinesFromFile File="output.txt">
<Output TaskParameter="Lines"
PropertyName="OutputVar" />
</ReadLinesFromFile>
</Target>
I'm quite new to MSBuild tasks, so it could well be something obvious. All I want to do is set a variable in one task, and then have that available in the parent task which called it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
朱利安给了你正确的答案,但没有解释为什么正确的。
由于您是 MSBuild 任务的新手,我将解释为什么 Julien 的答案是正确的。
MSBuild 中的所有任务都有参数 - 您将把它们视为您放置在任务上的属性。这些参数中的任何一个都可以通过在其中放置
Output
元素来读回。Output
元素具有三个可用的属性:在您的原始脚本中,您正在从另一个调用一个。第二个脚本将在不同的上下文中执行,因此它设置的任何属性或项目组仅存在于该上下文中。因此,当第二个脚本完成时,除非您指定了一些
Output
元素来捕获值,否则它们将被丢弃。请注意,您可以在一项任务下放置多个
Output
元素来捕获多个参数,或者只是为多个属性/项目组设置相同的值。Julien has given you the right answer, but not explained why it is correct.
As you're new to MSBuild tasks, I'll explain why Julien's answer is correct.
All tasks in MSBuild have parameters - you will know them as the attributes that you put on the task. Any of these parameters can be read back out by placing an
Output
element within it. TheOutput
element has three attributes that can be used:In your original scripts, you were invoking one from the other. The second script will execute in a different context, so any property or itemgroup it sets only exists in that context. Therefore when the second script completes, unless you have specified some
Output
elements to capture values they will be discarded.Note that you can put more than one
Output
element under a task to capture multiple parameters or just set the same value to multiple properties/itemgroups.您必须使用
MSBuild
任务的TargetOutputs
:(有关 MSBuild 任务。)
You have to use
TargetOutputs
of theMSBuild
task:(More information on MSBuild task.)