MSBuild:如何使用Ant中的嵌套属性

时间:2013-06-17 14:19:17

标签: ant msbuild

我的MSBuild配置文件中有一批属性

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <USERNAME1_DropboxPublic>e:\path\to\Dropbox\for\first\user</USERNAME1_DropboxPublic>
        <USERNAME2_DropboxPublic>e:\path\to\Dropbox\for\second\user</USERNAME2_DropboxPublic>
        <USERNAME3_DropboxPublic>e:\path\to\Dropbox\for\third\user</USERNAME3_DropboxPublic>
    </PropertyGroup>
    ....
</Project>

我希望获得的财产取决于当前的用户名。

我也知道ant使用nested properties这样${${user.name}_DropboxPublic}解决了这个问题

如何使用MSBuild工具解决此问题?

1 个答案:

答案 0 :(得分:1)

以下是一些“技巧”。

将以下xml保存到名为“MSBuild_Conditionals.xml”

的文件中
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapper">
    <!--  -->

    <PropertyGroup>
        <MyFavoriteFoodComplement Condition="'$(FavoriteFood)'=='PeanutButter'">Jelly</MyFavoriteFoodComplement>
        <MyFavoriteFoodComplement Condition="'$(FavoriteFood)'=='Steak'">Potatoes</MyFavoriteFoodComplement>

        <!-- Check to see if a match was given (by seeing if the variable value is an empty string), if no match (aka, an empty string), do a default -->
        <MyFavoriteFoodComplement Condition="'$(MyFavoriteFoodComplement)'==''">CookiesTheDefaultFavoriteFoodComplement</MyFavoriteFoodComplement>
    </PropertyGroup>


    <Choose>
        <When Condition=" '$(Configuration)'=='Debug' ">
            <PropertyGroup>
                <MyChooseVariable001>DebugSuffix001</MyChooseVariable001>
                <MyChooseVariable002>DebugSuffix002</MyChooseVariable002>
                <MyChooseVariable003>DebugSuffix003</MyChooseVariable003>
            </PropertyGroup>
        </When>
        <When Condition=" '$(Configuration)'=='Release' ">
            <PropertyGroup>
                <MyChooseVariable001>ReleaseSuffix001</MyChooseVariable001>
                <MyChooseVariable002>ReleaseSuffix002</MyChooseVariable002>
                <MyChooseVariable003>ReleaseSuffix003</MyChooseVariable003>             
            </PropertyGroup>
        </When>

        <Otherwise>
            <PropertyGroup>
                <MyChooseVariable001>DefaultValue001</MyChooseVariable001>
                <MyChooseVariable002>DefaultValue002</MyChooseVariable002>
                <MyChooseVariable003>DefaultValue003</MyChooseVariable003>                      
            </PropertyGroup>
        </Otherwise>        


    </Choose>

    <!--  -->

    <Target Name="TestCreateProperty">

        <CreateProperty Value="Here is a created property using another property : $(MyFavoriteFoodComplement)">
            <Output TaskParameter="Value" PropertyName="MyCreateProperty" />
        </CreateProperty>

        <Message Text=" MyCreateProperty = $(MyCreateProperty)" />

        <Message Text="    " />

        <CreateProperty Condition="'$(FavoriteFood)'=='PeanutButterX'" Value="Conditional Create Property : $(MyFavoriteFoodComplement)">
            <Output TaskParameter="Value" PropertyName="MyCreatePropertyWithCondition" />
        </CreateProperty>

        <CreateProperty Condition="'$(MyCreatePropertyWithCondition)'==''" Value="Conditional Create Property  : DEFAULT">
            <Output TaskParameter="Value" PropertyName="MyCreatePropertyWithCondition" />
        </CreateProperty>       

        <Message Text=" MyCreatePropertyWithCondition = $(MyCreatePropertyWithCondition)" />






    </Target>   

    <!--  -->


    <Target Name="ShowVariables">

        <Message Text="Configuration = $(Configuration)" />
        <Message Text="FavoriteFood = $(FavoriteFood)" />

        <Message Text="    " />

        <Message Text="MyFavoriteFoodComplement = $(MyFavoriteFoodComplement)" />

        <Message Text="MyChooseVariable001 = $(MyChooseVariable001)" />
        <Message Text="MyChooseVariable002 = $(MyChooseVariable002)" />
        <Message Text="MyChooseVariable003 = $(MyChooseVariable003)" />

    </Target>
    <!--  -->
    <!--  -->
    <!--  -->
    <Target Name="AllTargetsWrapper">
        <!--  -->
        <CallTarget Targets="ShowVariables" />
        <!--  -->
        <CallTarget Targets="TestCreateProperty" />
        <!--  -->       
    </Target>
    <!--  -->
</Project>

将其保存为.bat文件。

set msBuildDir=%WINDIR%\Microsoft.NET\Framework\v3.5
call %msBuildDir%\msbuild.exe MSBuild_Conditionals.xml /p:Configuration=Release;FavoriteFood=PeanutButter /l:FileLogger,Microsoft.Build.Engine;logfile=MSBuild_Conditionals_LOG.log
set msBuildDir=

输出(上面的.bat文件中的参数)

Build started 6/01/2013 11:33:33 PM.
__________________________________________________
Project ".\MSBuild_Conditionals.xml" (default targets):

Target AllTargetsWrapper:
    Target ShowVariables:
        Configuration = Release
        FavoriteFood = PeanutButter

        MyFavoriteFoodComplement = Jelly
        MyChooseVariable001 = ReleaseSuffix001
        MyChooseVariable002 = ReleaseSuffix002
        MyChooseVariable003 = ReleaseSuffix003
    Target TestCreateProperty:
         MyCreateProperty = Here is a created property using another property : Jelly

         MyCreatePropertyWithCondition = Conditional Create Property  : DEFAULT

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

Time Elapsed 00:00:00.01