gpt4 book ai didi

msbuild - 展开包含其他属性名称的 MSBuild 属性

转载 作者:行者123 更新时间:2023-12-02 18:52:14 25 4
gpt4 key购买 nike

假设我有属性 $(Foo),它被定义为某个函数的结果,返回字符串值 $(Bar)。是否可以以某种方式扩展它,以便将 $(Foo) 扩展为 $(Bar) 的值?

给定示例项目:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Bar>Bar Value</Bar>
<Foo>$([System.String]::Concat("$(","Bar",")"))</Foo>
<Baz>$(Foo)</Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>
<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>
</Project>

这是我所拥有的:

S:\>msbuild Test.proj /t:Test /nologo                 
Build started 18.09.2013 17:52:14.
Project "S:\Test.proj" on node 1 (Test target(s)).
Test:
Foo == $(Bar)
Baz == $(Bar)
Qux == Bar Value
Done Building Project "S:\Test.proj" (Test target(s)).


Build succeeded.
0 Warning(s)
0 Error(s)

因此,直接定义为 $(Bar)$(Qux) 可以正确展开,但 $(Foo)$(Baz) 则不然。是否也可以扩展它们?

S:\>msbuild /version                                     
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.18052]
Copyright (C) Microsoft Corporation. All rights reserved.

4.0.30319.17929

最佳答案

您想要模拟 $($(Foo)) 之类的内容,这对 MsBuild 来说是无效语法。但是您可以通过使用项目并在目标中动态创建项目来模拟此行为。由于Property and Item evaluation order,您无法在“全局范围”中执行此操作.

所以你必须在某些目标上这样做。以下是通过 InitialTargets 设置属性的示例。

<?xml version="1.0" encoding="utf-8"?>
<Project InitialTargets="MyPropertiesSetup" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<Bar>Bar Value</Bar>
<!-- A value of Foo property specifies the name of the property it takes value from. -->
<Foo>Bar</Foo>
<Baz></Baz>
<Qux>$(Bar)</Qux>
</PropertyGroup>

<Target Name="Test">
<Message Text="Foo == $(Foo)" />
<Message Text="Baz == $(Baz)" />
<Message Text="Qux == $(Qux)" />
</Target>

<Target Name="MyPropertiesSetup">
<ItemGroup>
<_Foo Include="$(Foo)" />
<_Baz Include="%(_Foo.Identity)" />
</ItemGroup>
<PropertyGroup>
<Foo>$(%(_Foo.Identity))</Foo>
<Baz>$(%(_Baz.Identity))</Baz>
</PropertyGroup>
</Target>

</Project>

如果您可以使用 item 作为值而不是属性,则有更优雅的方法( MSBuild Trickery #68 - @($(CanYouDoThis)):

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<Foo>Bar</Foo>
<Foo2>Bar2</Foo2>
</PropertyGroup>

<ItemGroup>
<Bar Include="Bar Value" />
<Bar2 Include="Bar2 Value" />
</ItemGroup>

<Target Name="Test">
<Message Text="Foo == '@($(Foo))'" />
<Message Text="Foo2 == '@($(Foo2))'" />
</Target>

</Project>

关于msbuild - 展开包含其他属性名称的 MSBuild 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18875846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com