MSBuild - 将ItemGroup保存在单独的文件中

时间:2012-01-24 15:02:06

标签: msbuild

我有以下MSBuild目标。

<Target Name="MyTarget">
    <ItemGroup>
        <ExcludeList Include="$(ProjectPath)\**\.svn\**"/>
        <ExcludeList Include="$(ProjectPath)\**\obj\**"/>
        <ExcludeList Include="$(ProjectPath)\**\*.config"/>
        <ExcludeList Include="$(ProjectPath)\**\*.cs"/>
        <ExcludeList Include="$(ProjectPath)\**\*.csproj"/>
        <ExcludeList Include="$(ProjectPath)\**\*.user"/>
    </ItemGroup>

    <ItemGroup>
        <ZipFiles Include="$(ProjectPath)\**\*.*" Exclude="@(ExcludeList)" />
    </ItemGroup>

    <Zip Files="@(ZipFiles)"
         WorkingDirectory="$(ProjectPath)"
         ZipFileName="$(PackageDirectory)\$(ProjectName).package.zip"
         ZipLevel="9" />
</Target>

我想将ExcludeList ItemGroup存储在一个单独的文件中,因为我将在单独的文件中有多个msbuild目标,这些目标都需要使用该列表,我不想重新创建它并维护多个副本。 / p>

外部化ItemGroup并将其加载到多个msbuild脚本中的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

在单独的msbuild文件中创建您的ItemGroup,然后您可以使用Import Element语句包含它。

Make.targets

<Project DefaultTargets = "Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
    <ItemGroup Condition="'$(ProjectPath)' != ''">
        <ExcludeList Include="$(ProjectPath)\**\.svn\**"/>
        <ExcludeList Include="$(ProjectPath)\**\obj\**"/>
        <ExcludeList Include="$(ProjectPath)\**\*.config"/>
        <ExcludeList Include="$(ProjectPath)\**\*.cs"/>
        <ExcludeList Include="$(ProjectPath)\**\*.csproj"/>
        <ExcludeList Include="$(ProjectPath)\**\*.user"/>
        <ExcludeList Include="$(ProjectPath)\**\*.proj"/>
    </ItemGroup>
</Project>

Make.proj

<Project DefaultTargets = "Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

    <PropertyGroup>
        <ProjectPath>D:\Temp</ProjectPath>
    </PropertyGroup>

    <Import Project=".\Make.targets"  Condition="'$(ProjectPath)' != ''" />

    <Target Name = "Build">
        <Message Text="Exclude = @(ExcludeList)" />
    </Target>
</Project>

当我从D:\ temp运行msbuild时(带有两个文件,否则为空)我得到:

Build started 24-01-2012 16:50:33.
Project "D:\Temp\Make.proj" on node 1 (default targets).
Build:
  Exclude = D:\Temp\Make.proj
Done Building Project "D:\Temp\Make.proj" (default targets).


Build succeeded.
    0 Warning(s)
    0 Error(s)