假设我有属性$(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)
因此,$(Qux)
(直接定义为$(Bar)
)会正确展开,但$(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
答案 0 :(得分:7)
你想模拟像$($(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>