如何在批处理任务中使用共享ItemGroup元素

时间:2011-09-26 19:17:57

标签: msbuild msbuild-task msbuildcommunitytasks msbuild-4.0

我正在尝试使用MSbuild自动创建两个不同平台的Firefox插件: 我有共享文件集,对于Mac和Windows都是相同的,并且具有特定于平台的文件。

我想通过平台批量制作XPI(这只是一个重命名的Zip文件)的任务,但我找不到添加平台无关(共享)文件作为Zip任务输入的正确方法。目前,我的解决方案是使用平台窗口和平台mac复制共享文件项,然后通过Platform参数批量Zip任务。我觉得我的解决方案不是最优的。也许社区可以提出更好的解决方案。以下是我用评论创建的简化解决方案:

    <?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ProjectMSBuildToolsPath Condition=" '$(ProjectMSBuildToolsPath)' == '' ">MSBuild</ProjectMSBuildToolsPath>
  </PropertyGroup>
    <!-- Required Import to use MSBuild Community Tasks -->
  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

  <!-- Default platform type is shared-->
  <ItemDefinitionGroup>
    <ZipFiles>
      <Platform>Shared</Platform>
    </ZipFiles>
  </ItemDefinitionGroup>

  <ItemGroup>
    <ZipFiles Include="chrome\overlay.js" />     

    <ZipFiles Include="chrome\Win\methodContainer.js">
      <Platform>Win</Platform>
    </ZipFiles>

    <ZipFiles Include="chrome\Mac\dataContainer.js">
      <Platform>Mac</Platform>
    </ZipFiles>       

  </ItemGroup>

  <Target Name="_PrepareItemsForZip" Outputs="$(Platform)">           
    <ItemGroup>
      <!-- Merge Shared and Windows specific files -->
      <ZipFilesToWin Include="@(ZipFiles)" Condition="('%(ZipFiles.Platform)' == 'Shared') Or ('%(ZipFiles.Platform)' == 'Win')" >
        <Platform>Win</Platform>
      </ZipFilesToWin>     
      <!-- Merge Shared and Mac specific files -->
      <ZipFilesToMac Include="@(ZipFiles)" Condition="('%(ZipFiles.Platform)' == 'Shared') Or ('%(ZipFiles.Platform)' == 'Mac')" >
        <Platform>Mac</Platform>
      </ZipFilesToMac>
    </ItemGroup>
        <!-- Merge Mac and Windows files set -->
    <ItemGroup>
      <_ZipFiles Include="@(ZipFilesToWin);@(ZipFilesToMac)" />
    </ItemGroup> 

  </Target>
  <!-- batch zipping files based on input platform -->
  <Target Name="MakeXPI" DependsOnTargets="_PrepareItemsForZip"  Inputs="@(_ZipFiles)" Outputs="%(Platform)" >
    <Message Text="Zipped files: @(_ZipFiles) %(Platform)"   Importance="high"/>
    <Zip Files="@(_ZipFiles)" WorkingDirectory="" ZipFileName="CoolAddon-%(Platform).xpi" ZipLevel="9" />    
  </Target>
</Project>

1 个答案:

答案 0 :(得分:0)

将它们提取到SharedProperties.properties

之类的文件中
<?xml version="1.0" encoding="utf-8"?>

<Project ToolsVersion="4.0"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
   <ZipFiles>       
      <Platform>Shared</Platform>     
   </ZipFiles>   
<PropertyGroup>
</Project>

然后只需导入目标/脚本就需要它们:

<Project ... >
     <Import Project="SharedProperties.properties" />