如何重新评估 MSBuild 属性
我可以创建 MSBuild 属性,其中包含未扩展的属性引用。以下是示例:
文本文件 property.txt 包含单行
$(一些财产)
它可以用 msbuild 脚本进行处理,如下所示:
<ItemGroup>
<PropertyFile Include="property.txt"/>
</ItemGroup>
<!-- Standart task for file reading -->
<ReadLinesFromFile File="@(PropertyFile)" >
<Output
TaskParameter="Lines"
ItemName="ItemsFromFile"/>
</ReadLinesFromFile>
<!-- Property LastLine now has value $(SomeProperty) -->
<PropertyGroup>
<LastLine>%(ItemsFromFile.Identity)</LastLine>
</PropertyGroup>
<Message Text="$(LastLine)"/>
现在我想扩展包含在 CurrentLine 中的属性引用。是否可以?
I can create MSBuild property, which contain unexpanded property reference. Here is example of it:
Text file property.txt contains single line
$(SomeProperty)
It can be processed with msbuild script like this:
<ItemGroup>
<PropertyFile Include="property.txt"/>
</ItemGroup>
<!-- Standart task for file reading -->
<ReadLinesFromFile File="@(PropertyFile)" >
<Output
TaskParameter="Lines"
ItemName="ItemsFromFile"/>
</ReadLinesFromFile>
<!-- Property LastLine now has value $(SomeProperty) -->
<PropertyGroup>
<LastLine>%(ItemsFromFile.Identity)</LastLine>
</PropertyGroup>
<Message Text="$(LastLine)"/>
Now I want to expand property reference, which contains in CurrentLine. Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您事先知道作为文件中列出的单个属性的候选属性的所有属性,则可以通过项目过滤来实现此目的。
注意事项:
1) 仅指定文件中的 PropertyName,而不是 $(PropertyName),除非您想使用属性函数解析字符串。
2)使用下面的方法,文件中只能有一行,对于多行,您需要额外的批处理级别。
上面的构造建立了两个候选属性:$(SomeProperty) 和$(SomeOtherProperty)。对于此示例,property.txt 的内容是一行...
...与值为 1 的 $(SomeProperty) 相关。
上面是下面主目标的两个依赖目标。他们使用依赖目标来读取文件,以便将其发布到项目组中,以便与 @(ChooseProperty) 项目组上的目标批处理一起使用。需要注意的关键是创建 @(_FilteredItems) 项目组的条件,该组将包含单个成员,因为......
请注意,批处理的项目元数据被传输到这些临时属性为了使条件起作用(这就是为什么文件只能包含一行,因此只有一个项目)。
...最后 $(LastLine) 属性是从 @(_FilteredItems) 中的单个项目中提取的。结果输出如下:
更改 property.txt 以包含“SomeOtherProperty”会导致以下结果:
If you know in advance all of the properties that are candidates for being the single property listed in the file there is a way to do this with item filtering.
Caveats:
1) Specify only the PropertyName in the file, not $(PropertyName), unless you want to parse the string using a property function.
2) There can only be a single line in the file with the approach below, for multiple lines you'll need an extra level of batching.
The constructs above establish two candidate properties, $(SomeProperty) and $(SomeOtherProperty). For this example the contents of property.txt was a single line...
...which correlates to $(SomeProperty) which has a value of 1
Above are the two dependent targets for the main target below. They use a dependent target to read the file so that it is published into an item group to be used with target batching on the @(ChooseProperty) item group. The key thing to note is the condition on the creation of the @(_FilteredItems) item group, which will contain a single member, due to this...
...notice that the batched item meta data is transferred to these temporary properties in order to make the condition work (and this is why the file can only contain a single line, so that there is only a single item).
...finally the $(LastLine) Property is pulled from the single item in @(_FilteredItems). The resulting output is below:
Changing the property.txt to contain 'SomeOtherProperty' results in this:
您不能以这种方式创建新属性。您无法动态创建属性名称。您可以修改构建过程。例子
You can't create new properties in such a way. You can't create property names dynamically. You can modify build process. Example