在 MsBuild 中,PropertyGroup 和 ItemGroup 有什么区别
In MsBuild, what's the difference between PropertyGroup and ItemGroup
我可以编译 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
应该是首选方法,但我应该在什么时候使用这些属性中的每一个?
将 属性 组视为单个变量的集合,属性只能包含一个值。
而 itemgroup 类似于可以包含零个、一个或多个值的数组或集合。您还可以迭代项目组,这在您要对多个项目执行相同任务时通常很有用。一个常见的例子是编译许多文件。
我可以编译 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
应该是首选方法,但我应该在什么时候使用这些属性中的每一个?
将 属性 组视为单个变量的集合,属性只能包含一个值。
而 itemgroup 类似于可以包含零个、一个或多个值的数组或集合。您还可以迭代项目组,这在您要对多个项目执行相同任务时通常很有用。一个常见的例子是编译许多文件。