【问题标题】:Different ways to pass variables in MSBuild在 MSBuild 中传递变量的不同方法
【发布时间】:2011-02-18 08:32:50
【问题描述】:

我对 MS Build 比较陌生,并且一直在查看 Visual Studio 附带的许多内置目标文件。我已经看到变量通过几种不同的方式传递,但不太确定它们之间的区别:

$(...)
@(...)
%(...)

【问题讨论】:

    标签: msbuild


    【解决方案1】:
    • $(...)用于访问Property值(更多信息请参见Property element

      <PropertyGroup>
        <Configuration>Debug</Configuration>
      </PropertyGroup>
      
      <Message Text="Configuration = $(Configuration)"/>
      
    • @(...) 用于访问Item 值(有关Item element 的更多信息)

      <ItemGroup>
        <Reference Include="System.Data"/>
        <Reference Include="System.Web.*"/>
      </ItemGroup>
      
      <Message Text="References = @(Reference)"/>
      
    • %(...) 用于访问Item Metadata 值(有关Item Metadata 的更多信息)。也用来做batching

      <ItemGroup>
        <Compile Include="Account\ChangePassword.aspx.cs">
          <DependentUpon>ChangePassword.aspx</DependentUpon>
          <SubType>ASPXCodeBehind</SubType>
        <Compile/>
      </ItemGroup>
      
      <Message Text="Element @(Compile) of subtype %(SubType) and depend of %(DependentUpon)"/>
      

    【讨论】:

      【解决方案2】:

      Dollar - $(MyProp): 允许您引用在 PropertyGroups 中指定的值。

      At Sign - @(CodeFile): 允许您引用在 ItemGroups 中指定的项目列表。

      Percent - %(CodeFile.BatchNum): 允许您使用元数据引用批处理的 ItemGroup 值。这有点复杂,因此请务必查看文档以获取更多信息。

      查看每个链接以获取有关如何使用它们的更多详细信息。祝你好运——希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        在 %(item metadata)上做了一点扩展,还有 Well-known item metadata: https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-well-known-item-metadata?view=vs-2017

        例如修改时间:

          <ItemGroup>
            <IntermediateAssembly Include="$(IntermediateOutputPath)$(TargetName)$(TargetExt)"/>
          </ItemGroup>
        
        <PropertyGroup>
          <_AssemblyTimestampBeforeCompile>%(IntermediateAssembly.ModifiedTime)</_AssemblyTimestampBeforeCompile>
        </PropertyGroup>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-11
          • 2021-12-25
          • 2017-09-10
          • 1970-01-01
          • 2013-10-30
          相关资源
          最近更新 更多