Msbuild 根据目标参数列表复制到多个位置?

发布于 2024-07-21 23:11:39 字数 258 浏览 9 评论 0原文

我有一个目录想要复制到多个位置。

假设我有

  • home.aspx

我想将其复制到

  • abc/home.aspx
  • def/home.aspx
  • ghi/home.aspx

那么我有两个问题:

  • 如何定义列表 abc、def、ghi?
  • 如何对此列表中的每个元素执行复制任务?

I got a directory I want to copy to a number of locations.

Say I have

  • home.aspx

I want to copy it to

  • abc/home.aspx
  • def/home.aspx
  • ghi/home.aspx

so two questions for me:

  • How do I define the list abc, def, ghi?
  • How do I execute my Copy task with each element of this list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2024-07-28 23:11:39

这是我整理的一个实际示例,显示了您正在寻找的内容:

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

Here is an actual example that I put together that shows what you were looking for:

<?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>
深海少女心 2024-07-28 23:11:39

您应该感兴趣的概念是 批处理< /a>.

我在我的博客上介绍了这个确切的场景 http ://www.sedodream.com/PermaLink,guid,5f1e0445-ce3d-4052-ba80-42fd19512d42.aspx

这是该博客文章的文本,您可以在上面的链接下载提到的文件。


今天有人告诉我,一位同事在使用 MSBuild 时遇到了问题。 他告诉我,他正在尝试将一组文件复制到一组不同的服务器。 但问题是他不知道如何在不执行多次复制任务调用的情况下实现这一目标。 我告诉他,他可以使用 MSBuild Batching 来实现这一目标。 批处理是一次对一组项目(批次)执行任务(或目标)的过程。 批次也可以包含单个项目。 因此,在这种情况下,我们需要为他想要部署到的每台服务器执行一次复制。 我创建了一个简单的 msbuild 文件,它以两种不同的方式演示了这一点。 第一种方法使用任务批处理,这可以在测试目标中看到。 另一个使用目标批处理,可以在 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 的高级主题,肯定会被忽视。 我必须承认我自己对这件事写得不够多而感到内疚。 下面列出了一些很好的批处理资源。


以下是我发布的其他一些与批处理相关的博客文章。

谢谢,
Sayed Ibrahim Hashimi

我的书:Microsoft 构建引擎内部:使用 MSBuild 和 Team Foundation Build

The concept that you should be interested in is known as Batching.

I've covered this exact scenario on my blog at http://www.sedodream.com/PermaLink,guid,5f1e0445-ce3d-4052-ba80-42fd19512d42.aspx

Here is the text of that blog entry, you can download the mentioned files at the link above.


Today someone was telling me about a co-worker who was having issues with MSBuild. He told me that he was trying to copy a set of files to a set of different servers. But the issue was that he didn’t know how to achieve this without performing multiple Copy task invocations. I told him that he could achieve this using MSBuild Batching. Batching is a process of performing a task (or target) on a set of items (batches) at a time. A batch can also include a single item. So in this scenario we need to perform the copy one time for each server that he wanted to deploy to. I’ve created a simple msbuild file which demonstrates this in two different ways. The first way uses task batching, which can bee seen in the Test target. And the other uses Target batching which can be seen in the DoItCore target. I've also created a clean target, which has nothing to do with batching.

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

Batching is an advanced topic of MSBuild, and is defintely neglected. I have to admit I’m guilty of not writing about it enough myself. There are some good batching resources, they are listed below.


Here are some other batching related blog entries that I've posted.

Thanks,
Sayed Ibrahim Hashimi

My Book: Inside the Microsoft Build Engine : Using MSBuild and Team Foundation Build

红玫瑰 2024-07-28 23:11:39

有一个项目组,您可以在其中构建此目的地列表(“abc...等)。然后使用此列表调用复制任务(@Destination)。

我相信您会发现如果你搜索它,就会有很多例子。 //keithhill.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3dMSBuild

Have an itemgroup where you build up this list of destinations ("<Destination>abc</Destionation>..., etc). Then invoke the copy task with this list (@Destination).

I'm sure you'll find plenty of examples if you search for it. http://keithhill.spaces.live.com/?_c11_BlogPart_BlogPart=blogview&_c=BlogPart&partqs=cat%3dMSBuild

风流物 2024-07-28 23:11:39

您最好自己将其作为学习练习,而不是将 MSBUILD 视为一个魔术盒。 Patrick Smacchia 的这篇文章 为您提供了所涉及的大部分技术。

You really are best off doing this yourself as a learning exercise, rather than treating MSBUILD as a magic box. This article from Patrick Smacchia gives you most of the techniques involved.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文