【问题标题】:Do "if" type statements exist when inside Property Groups in MSBuild?MSBuild 的属性组中是否存在“if”类​​型语句?
【发布时间】:2013-07-31 19:48:07
【问题描述】:

我目前必须有两个单独的属性组,它们之间只有两个区别,根据条件设置为具有一个或另一个触发器。这是我所拥有的:

<!--CAME FROM TEAMBUILD-->
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' != 'Debug|AnyCPU' AND '$(Configuration)|$(Platform)' != 'Release|AnyCPU' AND '$(BuildingInsideVisualStudio)' != 'true' ">
    <PreBuildEvent>
    </PreBuildEvent>
    <PostBuildEvent>
      set MAGE="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"
      set APPFILE=$(TargetDir)$(TargetName).application
      set MANIFEST=$(TargetPath).manifest
      set CERT=$(ProjectDir)$(TargetName).pfx
      set PROJECTNAME=$(TargetName)
      set CONFIGURATION=$(ConfigurationName)
      set TARGETDIR=$(TargetDir)
      set TEAMBUILD=$True
      Powershell -File "$(ProjectDir)POSTBUILD.ps1"
    </PostBuildEvent>
  </PropertyGroup>

  <!--CAME FROM PUBLISH COMMAND-->
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' != 'Debug|AnyCPU' AND '$(Configuration)|$(Platform)' != 'Release|AnyCPU' AND '$(BuildingInsideVisualStudio)' == 'true' ">
    <PreBuildEvent>
    </PreBuildEvent>
    <PostBuildEvent>
      set MAGE="C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe"
      set APPFILE=$(TargetDir)$(TargetName).application
      set MANIFEST=$(TargetPath).manifest
      set CERT=$(ProjectDir)$(TargetName).pfx
      set PROJECTNAME=$(TargetName)
      set CONFIGURATION=$(ConfigurationName)
      set TARGETDIR=$(TargetDir)
      set TEAMBUILD=$False
      Powershell -File "$(ProjectDir)POSTBUILD.ps1"
    </PostBuildEvent>
  </PropertyGroup>

有没有办法根据构建后事件中的 $(BuildingInsideVisualStudio) 值设置团队构建值? 像

如果 ($(BuildingInsideVisualStudio) == 'true') 设置团队构建 = $True

或 甚至像 set TEAMBUILD = $$(BuildingInsideVisualStudio)?

【问题讨论】:

  • This 或许?
  • 嗯...可能。我得调查一下。感谢您的建议。

标签: msbuild cmd tfsbuild post-build-event


【解决方案1】:

您已经在使用它:Condition。您只需提取一个额外的步骤来创建将用作 TEAMBUILD 值的属性。例如:

<PropertyGroup Condition='$(BuildingInsideVisualStudio)' != 'true' ">
  <TeamBuildValue>FALSE</TeamBuildValue>
</PropertyGroup>

<PropertyGroup Condition='$(BuildingInsideVisualStudio)' == 'true' ">
  <TeamBuildValue>TRUE</TeamBuildValue>
</PropertyGroup>

<PropertyGroup>
  <PreBuildEvent>
  </PreBuildEvent>
  <PostBuildEvent>
    ...
    set TEAMBUILD=$(TeamBuildValue)
    ...
  </PostBuildEvent>
</PropertyGroup>

【讨论】:

  • 噢噢噢,我不知道我可以做那样的事情。我对 MSBuild 还是很陌生。非常感谢,这正是我需要的!
【解决方案2】:

我可能会尝试选择/否则.......而不是 == != 只是一种偏好。 因为有一天.....你可能会有第三种选择。

“否则”子句(又名,使用一些默认值)更加明确。

<Choose>

    <When Condition=" '$(Computername)'=='MySuperComputer01' ">               
        <PropertyGroup>
            <FavoriteFood>Peanuts</FavoriteFood>
            <FavoriteColor>Red</FavoriteColor>
        </PropertyGroup>
    </When>

    <When Condition=" '$(Computername)'=='MySuperComputer02' ">
        <PropertyGroup>
            <FavoriteFood>Apples</FavoriteFood>
            <FavoriteColor>Yellow</FavoriteColor>
        </PropertyGroup>
    </When>

    <Otherwise>
        <PropertyGroup>
            <FavoriteFood>CrackersDefault</FavoriteFood>
            <FavoriteColor>OrangeDefault</FavoriteColor>
        </PropertyGroup>        
    </Otherwise>
    
</Choose>

【讨论】:

  • 完美!使我免于在 csproj 中创建多个 ItemGroup 条目。
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 2010-12-31
  • 2021-02-23
  • 2018-05-15
  • 2016-07-17
  • 2010-12-09
  • 1970-01-01
相关资源
最近更新 更多