RationalGeek
RationalGeek

Reputation: 9599

MSBuild - keep ItemGroup in separate file

I have the following MSBuild target working.

<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>

I'd like to store the ExcludeList ItemGroup in a separate file, because I will have multiple msbuild targets in separate files that all need to use that list, and I don't want to recreate it and maintain multiple copies.

What is the best way to externalize an ItemGroup and load it into multiple msbuild scripts?

Upvotes: 2

Views: 3442

Answers (1)

Huusom
Huusom

Reputation: 5912

Create your ItemGroup in a separate msbuild file, then you can include it with Import Element statement.

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>

When I run msbuild from D:\temp (with the two files, otherwise empty) i get:

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)

Upvotes: 2

Related Questions