2

假设我有 property $(Foo),它被定义为某个函数的结果,它返回字符串 value $(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                                          
4

1 回答 1

8

你想模拟像 $($(Foo)) 这样的东西,它是 msbuild 的无效语法。但是您可以通过使用项目和在目标中动态创建项目来模拟这种行为。由于Property 和 Item 评估顺序,您不能在“全局范围”内执行此操作。

所以你必须在一些目标上这样做。这是通过 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>

如果您可以将项目用于值而不是属性,则有更优雅的方法(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>
于 2013-09-22T10:54:36.113 回答