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

时间:2010-05-11 21:03:09

标签: msbuild

我是MS Build的新手,并且一直在审查Visual Studio附带的许多内置目标文件。我看到变量通过了几种不同的方式,我不太确定它们之间的区别:

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

3 个答案:

答案 0 :(得分:82)

  • $(...)用于访问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)"/>
    

答案 1 :(得分:16)

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

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

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

查看每个链接,了解有关如何使用这些链接的详细信息。祝你好运 - 希望这有帮助!

答案 2 :(得分:0)

%(项目元数据)的扩展,还有众所周知的项目元数据: 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>