输出MSBuild变量时出现问题 [英] Trouble with outputting MSBuild variables

查看:84
本文介绍了输出MSBuild变量时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量从一个目标输出到启动它的父目标中.例如,

I'm trying to output the variable from one target, into the parent target which started it. For example,

目标1只是调用文件2中的任务,并且应该能够使用其中的变量集.但是,我似乎无法使其正常运行(也许语法错误?).目标1看起来像这样:

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>

目标2是读取文本文件的值并将其设置为属性,并将变量'OutputVar'设置为匹配的位置.应该将其退还给父母.

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>

我对MSBuild任务还很陌生,所以很明显.我要做的就是在一个任务中设置一个变量,然后在调用它的父任务中将其设置为可用.

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.

推荐答案

Julien为您提供了正确的答案,但没有解释为什么它是正确的.

Julien has given you the right answer, but not explained why it is correct.

当您不熟悉MSBuild任务时,我将解释为什么Julien的答案是正确的.

As you're new to MSBuild tasks, I'll explain why Julien's answer is correct.

MSBuild中的所有任务都有参数-您将把它们作为您放置在任务上的属性.通过在其中放置Output元素,可以读出这些参数中的任何一个. Output元素具有三个可以使用的属性:

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. The Output element has three attributes that can be used:

  • TaskParameter-这是您要获取的任务的属性/参数的名称
  • ItemName-这是将参数值放入其中的项目组
  • PropertyName-这是将参数值放入其中的属性的名称

在您的原始脚本中,您正在相互调用.第二个脚本将在不同的上下文中执行,因此它设置的任何属性或项目组仅存在于该上下文中.因此,当第二个脚本完成时,除非您指定了某些Output元素来捕获值,否则它们将被丢弃.

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.

请注意,您可以在一个任务下放置多个Output元素来捕获多个参数,或仅将相同的值设置为多个属性/项目组.

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变量时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆