【问题标题】:In MsBuild, what's the difference between PropertyGroup and ItemGroup在 MsBuild 中,PropertyGroup 和 ItemGroup 有什么区别
【发布时间】:2016-02-16 17:14:07
【问题描述】:

我可以编译PropertyGroup引用的.cs文件:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
        <Compile>helloConfig.cs</Compile>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="$(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>        
</Project>

或者使用 ItemGroup 做同样的事情:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">    
    <ItemGroup>
        <Compile Include="helloConfig.cs" />
    </ItemGroup>

    <PropertyGroup>
        <AssemblyName>MSBuildSample</AssemblyName>
        <OutputPath>Bin\</OutputPath>
    </PropertyGroup>

    <Target Name="Build">
        <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
        <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe"/>
    </Target>  
</Project>

我知道使用ItemGroup 应该是首选方法,但我应该在什么时候使用这些属性中的每一个?

【问题讨论】:

    标签: c# visual-studio msbuild


    【解决方案1】:

    将属性组视为单个变量的集合,属性只能保存一个值。

    而 itemgroup 类似于可以保存零个、一个或多个值的数组或集合。您还可以迭代项目组,这在您想要对多个项目执行相同任务时通常很有用。一个常见的例子是编译许多文件。

    【讨论】:

    • ItemGroup 值也可以有多个属性,包括定义的“模式”。属性是严格的单个字符串值。
    • @David 很好地解释了差异。我觉得我可以将 ItemGroup 用于我的用例“将文件/目录从不同的源路径复制到具有相同子目录的部署路径”,即。将 xyz*.dll 复制到 deploy\xyz*.dll;将 abc\images*.jpg 复制到 deploy\abc\images*.jpg (在这里,我想使用一个任务来执行我的 100 个这样的副本)你能告诉我它是如何工作的吗? (详细的reqd解释在stackoverflow.com/questions/53544528/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 2017-08-18
    • 2010-11-07
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多