2012-01-24 28 views
1

我有以下MSBuild目標工作。MSBuild - 將ItemGroup保存在單獨的文件中

<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的目標,我不想重新創建和維護多份。

外部化ItemGroup並將其加載到多個msbuild腳本中的最佳方式是什麼?

回答

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的MSBuild:\ TEMP(與兩個文件,否則爲空)我得到:

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) 
+0

@Hussom我試過。當我將ItemGroup元素移動到單獨的文件,然後使用導入時,ExcludeList屬性從原始目標中爲空。你能在你的答案中包括一個工作樣本嗎? – RationalGeek

+0

@Hussom謝謝。這個樣本對我有用。現在要弄清楚爲什麼它在我的實際腳本中不起作用... – RationalGeek

+0

是否在Project屬性中限定了正確的路徑? – Huusom

相關問題