我有一个要复制到多个位置的目录。
说我有
- 主页.aspx
我想把它复制到
- abc/home.aspx
- def/home.aspx
- ghi/home.aspx
所以我有两个问题:
- 如何定义列表 abc、def、ghi?
- 如何使用此列表的每个元素执行我的复制任务?
我有一个要复制到多个位置的目录。
说我有
我想把它复制到
所以我有两个问题:
这是我放在一起的一个实际示例,它显示了您正在寻找的内容:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test" ToolsVersion="3.5">
<!--Declare an ItemGroup that points to your file you want to copy.-->
<ItemGroup>
<ItemToCopy Include=".\Home.aspx" />
</ItemGroup>
<!--Declare an ItemGroup that points to your destination Locations-->
<ItemGroup>
<DestLocations Include=".\abc\home.aspx" />
<DestLocations Include=".\def\home.aspx" />
<DestLocations Include=".\ghi\home.aspx" />
</ItemGroup>
<Target Name="CopyFiles">
<!--Run the copy command to copy the item to your dest locations-->
<!--This is where the magic happens. The % sign before the DestLocations reference says to use
Batching. So Copy will be run for each unique FullPath MetaData in the DestLocations ItemGroup.-->
<Copy SourceFiles="@(ItemToCopy)" DestinationFolder="%(DestLocations.FullPath)" />
</Target>
</Project>
您应该感兴趣的概念称为Batching。
我已经在我的博客http://www.sedodream.com/PermaLink,guid,5f1e0445-ce3d-4052-ba80-42fd19512d42.aspx中介绍了这个确切的场景
这是该博客条目的文本,您可以在上面的链接中下载提到的文件。
今天有人告诉我一位同事遇到了 MSBuild 问题。他告诉我他正试图将一组文件复制到一组不同的服务器上。但问题是他不知道如何在不执行多个复制任务调用的情况下实现这一点。我告诉他,他可以使用 MSBuild Batching 实现这一目标。批处理是一次对一组项目(批次)执行任务(或目标)的过程。一个批次还可以包括单个项目。所以在这种情况下,我们需要为他想要部署到的每台服务器执行一次复制。我创建了一个简单的 msbuild 文件,它以两种不同的方式演示了这一点。第一种方式使用任务批处理,可以在 Test 目标中看到。另一个使用目标批处理,可以在 DoItCore 目标中看到。我还创建了一个干净的目标,
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Test">
<ItemGroup>
<SourceFiles Include="*.txt"/>
<Dest Include="One;Two;Three;Four;Five"/>
</ItemGroup>
<Target Name="Test">
<Copy SourceFiles ="@(SourceFiles)" DestinationFolder="%(Dest.FullPath)"/>
<Message Text="Fullpath: %(Dest.FullPath)"/>
</Target>
<!-- These targets demonstrate target batching -->
<Target Name="DoIt" DependsOnTargets="DoItCore"/>
<Target Name="DoItCore" Inputs="@(SourceFiles)" Outputs="%(Dest.FullPath)">
<Copy SourceFiles="@(SourceFiles)" DestinationFolder="%(Dest.FullPath)"/>
</Target>
<!-- This will clean up the files -->
<Target Name="Clean">
<CreateItem Include="%(Dest.FullPath)\**\*">
<Output ItemName="FilesToDelete" TaskParameter="Include"/>
</CreateItem>
<Delete Files="@(FilesToDelete)"/>
</Target>
</Project>
批处理是 MSBuild 的高级主题,肯定会被忽略。我不得不承认我自己没有写足够的东西而感到内疚。有一些很好的批处理资源,它们在下面列出。
这是我发布的其他一些与批处理相关的博客条目。
谢谢,赛义德·易卜拉欣·哈希米
我的书:Microsoft Build Engine 内部:使用 MSBuild 和 Team Foundation Build
你真的最好把它当作一个学习练习,而不是把 MSBUILD 当作一个魔法盒子。 Patrick Smacchia 的这篇文章为您提供了所涉及的大部分技术。
有一个项目组,您可以在其中建立此目的地列表(“<Destination>abc</Destination>...等)。然后使用此列表(@Destination)调用复制任务。
如果您搜索它,我相信您会找到很多示例。http://keithhill.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3dMSBuild