代码之家  ›  专栏  ›  技术社区  ›  Wallace Breza

在msbuild中传递变量的不同方法

  •  63
  • Wallace Breza  · 技术社区  · 16 年前

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

    $(...)
    @(...)
    %(...)
    
    3 回复  |  直到 16 年前
        1
  •  81
  •   Julien Hoarau    16 年前
    • $(...) 用于访问 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
  •  15
  •   Indolent Coder    16 年前

    Dollar - $(MyProp): 允许您引用属性组中指定的值。

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

    Percent - %(CodeFile.BatchNum): 允许您使用元数据引用批处理的项组值。这有点复杂,所以一定要查看文档了解更多信息。

    请查看每个链接,以获取有关如何使用这些链接的详细信息。祝你好运——希望这有帮助!

        3
  •  0
  •   selalerer    7 年前

    在%(item metadata)上有一点扩展,还有众所周知的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>