msbuild - 在 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), '(?<=(?:^|;)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
因为元素的顺序毕竟很重要,所以我编辑了摘录以代表我项目中的真实情况。抱歉误导,我没有意识到它的重要性,因为我的注意力集中在其他地方。
解决方案
我希望最初的问题不会太误导,尽管我不确定罪魁祸首是否可以按照最初提出的方式确定。(我修改了问题以代表真实情况)。
所以问题是,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), '(?<=(?:^|;)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), '(?<=(?:^|;)SITE_)([^;$]+)(?=;|$)'))</MySite>
</PropertyGroup>
现在一切正常。
推荐阅读
- algorithm - gcd的最大总和
- elasticsearch - 通过脚本访问文本关键字字段
- php - Php 彩色单词,居中和多行文本
- python - anaconda spyder 使用旧参数重新启动
- javascript - 在我的具体情况下,就效率和浏览器支持而言,DOMNodeInserted 或 ajaxStop 哪个更好?
- android - android socket Io冻结recyclerview滚动
- mysql - 我想使用 sequelize nodejs 在四个不同的表中添加数据
- mongodb - 如何删除字符串 Mongodb 的子集短语
- c# - 修改 TextBox 上下文菜单/MenuFlyout
- python - 在 python 代码中运行 bash for 循环时出错