Monday, June 23, 2008

Copy Multiple Files via TFS build

bI have recently been doing a lot of work with TFS and want to write about a few things I had to figure out how to do. As part of my build process I had a number of dlls I needed to copy out to my staging environment. To do this I overrode the AfterCompile target. In order to override this Target you have to import the Microsoft.Common.targets file.

<Import Project="$(MSBuildBinPath)\Microsoft.Common.targets" />

In MSBuild you can call Copy which will move a file from one location to another location. The key word here is that it will move "a file", it will not move "files." So you can call something like this:
<Copy SourceFiles="$(DeployStagePath)\my.dll"
          DestinationFolder="$(DeployPath)" />
But you cannot use code like this to copy all dll files:
<Copy SourceFiles="$(DeployStagePath)\*.dll"
          DestinationFolder="$(DeployPath)" />

To copy all dlls in a folder you need to create a new item. Here is the code I used to do this.

<Target Name="AfterCompile" DependsOnTargets="CopyFiles"> 
    <Message Text="The after build event sets up the compiled code for ClickOnce Deployment" Importance="high"/>

    <!-- Set up the items for doing a copy of a collection of files -->
    <CreateItem Include="$(DeployStagePath)\*.dll">
      <Output TaskParameter="Include" ItemName="DLLsToCopy"/>
    </CreateItem>

    <Copy SourceFiles="@(DLLsToCopy)" DestinationFolder="$(DeployPath)" />
</Target>