首页 > 解决方案 > 在 Azure Devops 构建管道中执行的 Msbuild 中定义自定义变量

问题描述

我有一个 MsBuild (csproj) 文件,我在其中执行一些自定义条件项和目标。对于一种配置,一切运行良好,但对于从原始配置克隆的其他配置,我收到错误消息。该错误来自于未设置正则表达式中的自定义变量这一事实。

以下是 MsBuild 脚本的主要摘录:

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="N1 Specifics" Condition="$(MySite) == 'N1'">
    <SharedResPath>..\..\Resources\N1.1</PathPilotageKX>
  </PropertyGroup>

  <PropertyGroup Label="N2 Specifics" Condition="$(MySite) == 'N2'">
    <SharedResPath>..\..\Resources\N2.0</PathPilotageKX>
  </PropertyGroup>

  <Target Name="My Validations" BeforeTargets="BeforeBuild">
    <Message Text="DefinedConstants: [$(DefineConstants)]" />
    <Message Text="Site: [$(MySite)]" />
    <Message Text="Shared Res Path: [$(SharedResPath)]" />
    <Message Text="Configuration: $(Configuration)" />
    <Error Text="Site code (SITE_$(MySite)) is not supported by csproj (MSBuild)." Condition="$(SharedResPath) == ''" />
  </Target>

验证任务结果为:

   My Validations:
     DefinedConstants: [TRACE;SITE_N1]
     Site: []
     Shared Res Path: []
     Configuration: N1
##[error]MyProject.csproj(352,5): Error : Site code (SITE_) is not supported by csproj (MSBuild).

编辑1

经过一番摆弄,它似乎与 Azure DevOps 无关,因为我能够在开发机器上重现该问题;所以我删除了它的每一个提及。此外,它似乎与 DefineConstants 的值无关,因为将值从有效的值复制到无效的值,反之亦然不会改变任何东西。

编辑 2

因为元素的顺序毕竟很重要,所以我编辑了摘录以代表我项目中的真实情况。抱歉误导,我没有意识到它的重要性,因为我的注意力集中在其他地方。

标签: msbuild

解决方案


我希望最初的问题不会太误导,尽管我不确定罪魁祸首是否可以按照最初提出的方式确定。(我修改了问题以代表真实情况)。

所以问题是,PropertyGroup 块出现的顺序很重要。我修改的 csproj 是使用 Visual Studio 创建的,并且新配置(在这种情况下为 N2)被添加到文件的末尾,但我将自定义属性(MySite)设置在靠近原始属性组(此处为 N1)的顶部.

具体来说,如果我有

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

当正则表达式尝试提取值时,尚未设置DefineConstants 。相反,如果我用

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N1|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N1</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'N2|AnyCPU' ">
    <DefineConstants>TRACE;DEBUG;SITE_N2</DefineConstants>
  </PropertyGroup>

  <PropertyGroup Label="Site Extractor">
    <MySite>$([System.Text.RegularExpressions.Regex]::Match($(DefineConstants), '(?&lt;=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
  </PropertyGroup>

现在一切正常。


推荐阅读