在 MSBuild 中,如何将属性更改传播到依赖属性? [英] In MSBuild, how to propagate a property change to a dependent property?

查看:31
本文介绍了在 MSBuild 中,如何将属性更改传播到依赖属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MSBuild 中,我试图动态生成一个属性,该属性依赖于另一个属性的值.这似乎与 How get exec task output with msbuild在msbuild 用于 calltarget 子目标

In MSBuild, I am trying to dynamically generate a property, one that is dependent on the value of another property. This seems related to but not identical with How get exec task output with msbuild and Dynamically create a property in msbuild to be used in a calltarget subtarget

请参阅下面的示例.它正在尝试生成属性 WixVariable,该属性依赖于属性 MsiVersion.(仅供参考,虽然与此问题无关,但此代码与准备 WixVariables 相关,WixVariables 将传递给 wix.exe 以生成 MS 安装程序.)

Please see the sample below. It is trying generate property WixVariable, which is dependent on property MsiVersion. (FYI, Though it is not really relevant to this problem, this code relates to preparing WixVariables, which is passed to wix.exe to generate a MS installer.)

在 (1) 属性 MsiVersion 被分配值未知"

At (1) property MsiVersion is assigned the value "unknown"

在 (2) 属性 WixVariables 分配值MSI_VERSION=unknown;OTHER_VARIABLE=OtherValue"

At (2) property WixVariables is assigned the value "MSI_VERSION=unknown;OTHER_VARIABLE=OtherValue"

在 (3) 处,运行 powershell 脚本以生成实际版本号.为了保持示例简单,它只返回4.3".

At (3) a powershell script is run to generate the actual version number. To keep the sample simple, it just returns "4.3".

在 (4) 属性 MsiVersion 应该分配新值4.3"

At (4) property MsiVersion is supposed to be assigned the new value "4.3"

这应该更新属性 WixVariables 使其值为MSI_VERSION=4.3;OTHER_VARIABLE=OtherValue"

And this should update property WixVariables to have the value "MSI_VERSION=4.3;OTHER_VARIABLE=OtherValue"

但这行不通.WixVariables 保持不变.

But that doesn't work. WixVariables remains unchanged.

我已经试验了几个小时,但没有得到我需要的结果:WixVariables 来反映 MsiVersion 的生成值.

I've been experimenting with this for several hours but cannot get the result I need: WixVariables to reflect the generated value of MsiVersion.

        <?xml version="1.0" encoding="utf-8"?>
        <Project ToolsVersion="4.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

          <PropertyGroup>
            <AProperty>SomeValue</AProperty>
(1)         <MsiVersion>unknown</MsiVersion>
          </PropertyGroup>

          <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
            ... snip
(2)         <WixVariables>MSI_VERSION=$(MsiVersion);OTHER_VARIABLE=OtherValue</WixVariables>
          </PropertyGroup>

          <Target Name="BeforeBuild" />
(3)         <Exec ConsoleToMSBuild="true" Command="powershell -NoProfile -ExecutionPolicy ByPass -Command &quot; Write-Output 4.3 &quot;">
(4)           <Output TaskParameter="ConsoleOutput" PropertyName="MsiVersion" />
            </Exec> 
          </Target>

        </Project>

推荐答案

好的,经过多次尝试,我对我的问题有了答案,并且对 MSBuild 有了更好的理解.

Ok, after much experimenting I have an answer to my question and a better understanding of MSBuild.

我不明白的是,一旦为属性分配了一个值(参见上面示例中的 (1) 和 (2)),该属性的值就会固定.

What I did not understand is that once a property is assigned a value [see (1) and (2) in the sample above) the property's value is fixed.

(4) 处的代码更新 MsiVersion 的值,但不会更新 WixVariables 的值.换句话说当属性 MsiVersion 更改时,不会重新评估 (2) 处的行.

The code at (4) does update the value of MsiVersion but not update the value of WixVariables. In other words the line at (2) is NOT re-evaluated when property MsiVersion changes.

要更新 WixVariables 的值,必须像下面 (5) 处的代码那样显式设置它.

To update the value of WixVariables, it must be set explicitly as the code below at (5) does.

工作示例如下所示.与问题示例不同,最初创建 MsiVersion=unknown 没有意义或在 WixVariables 前面加上MSI_VERSION=unknown".

A working sample is shown below. Unlike the question sample, there is no point in initially creating MsiVersion=unknown or prepending WixVariables with "MSI_VERSION=unknown".

        <?xml version="1.0" encoding="utf-8"?>
        <Project ToolsVersion="4.0" DefaultTargets="Build" InitialTargets="EnsureWixToolsetInstalled" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

          <PropertyGroup>
            <AProperty>SomeValue</AProperty>
          </PropertyGroup>

          <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
            ... snip
            <WixVariables>OTHER_VARIABLE=OtherValue</WixVariables>
          </PropertyGroup>

          <Target Name="BeforeBuild" />
            <Exec ConsoleToMSBuild="true" Command="powershell -NoProfile -ExecutionPolicy ByPass -Command &quot; Write-Output 4.3 &quot;">
              <Output TaskParameter="ConsoleOutput" PropertyName="MsiVersion" />
            </Exec> 
(5)         <CreateProperty Value="MSI_VERSION=$(MsiVersion);$(WixVariables)">
                <Output TaskParameter="Value" PropertyName="WixVariables" />
            </CreateProperty>
          </Target>

        </Project>

这篇关于在 MSBuild 中,如何将属性更改传播到依赖属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆